Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Octopart v4 API #1150

Merged
merged 10 commits into from
Nov 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

Bugfixes and other features:
* Updated to the most recent Octopart API version 4 [#1150](https://github.com/partkeepr/PartKeepr/pull/1150)

## PartKeepr 1.4.0

New Features:
Expand Down
9 changes: 7 additions & 2 deletions app/config/parameters.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,16 @@ $container->setParameter('partkeepr.filesystem.data_directory', '%kernel.root_di
$container->setParameter('partkeepr.cronjob.check', true);

/**
* Specifies which API key PartKeepr should use to talk to OctoPart. You can get an API key by registering at
* https://octopart.com/api/home and then registering an application.
* Specifies which v4 API key PartKeepr should use to talk to OctoPart. You can get an API key by registering at
* https://octopart.com/api and then registering an application.
*/
$container->setParameter('partkeepr.octopart.apikey', '');

/**
* The number of returned parts from API calls is limited. Try to keep this value low
*/
$container->setParameter('partkeepr.octopart.limit', '3');

/**
* Specifies which URL contains the patreon status. If you do not wish to display the patreon status,
* set this parameter to false. Please note that we rely on your Patreon pledges to ensure further
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ Ext.define("PartKeepr.Components.OctoPart.SearchPanel", {
reader: {
type: 'json',
totalProperty: 'hits',
rootProperty: 'results'
rootProperty: 'results',
transform: this.checkForErrors
}
},
autoLoad: false
Expand Down Expand Up @@ -97,19 +98,14 @@ Ext.define("PartKeepr.Components.OctoPart.SearchPanel", {
boxLabel: i18n("Parameters")
}, {
xtype: 'checkbox',
itemId: "importDatasheets",
checked: PartKeepr.getApplication().getUserPreference("partkeepr.octopart.importDatasheets", true),
boxLabel: i18n("Datasheets")
itemId: "importBestDatasheet",
checked: PartKeepr.getApplication().getUserPreference("partkeepr.octopart.importBestDatasheet", true),
boxLabel: i18n("Best Datasheet")
}, {
xtype: 'checkbox',
itemId: "importCADModels",
checked: PartKeepr.getApplication().getUserPreference("partkeepr.octopart.importCADModels", true),
boxLabel: i18n("CAD Models")
}, {
xtype: 'checkbox',
itemId: "importComplianceDocuments",
checked: PartKeepr.getApplication().getUserPreference("partkeepr.octopart.importComplianceDocuments", true),
boxLabel: i18n("Compliance Documents")
}, {
xtype: 'checkbox',
itemId: "importReferenceDesigns",
Expand Down Expand Up @@ -158,6 +154,8 @@ Ext.define("PartKeepr.Components.OctoPart.SearchPanel", {
this.onSelectChange,
this);

this.store.on("load", this.checkForApiError, this);

this.callParent(arguments);

},
Expand Down Expand Up @@ -186,9 +184,8 @@ Ext.define("PartKeepr.Components.OctoPart.SearchPanel", {

j.setImport("distributors", this.down("#importDistributors").getValue());
j.setImport("parameters", this.down("#importParameters").getValue());
j.setImport("datasheets", this.down("#importDatasheets").getValue());
j.setImport("bestDatasheet", this.down("#importBestDatasheet").getValue());
j.setImport("cadModels", this.down("#importCADModels").getValue());
j.setImport("complianceDocuments", this.down("#importComplianceDocuments").getValue());
j.setImport("referenceDesigns", this.down("#importReferenceDesigns").getValue());
j.setImport("images", this.down("#importImages").getValue());

Expand All @@ -206,5 +203,19 @@ Ext.define("PartKeepr.Components.OctoPart.SearchPanel", {
);
this.store.load();
this.searchBar.setValue(query);
},
checkForErrors: function (data)
{
if (data.results.length == 0 && data.errors.length > 0) {
Ext.Msg.alert(i18n("Octopart Error"), data.errors.map(e => e.message).join());
}

return data;
},
checkForApiError: function (store, records, successful, eOpts )
{
if (!successful) {
Ext.Msg.alert(i18n("Octopart Error"), "PartKeepr cannot access the Octopart API");
}
}
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Ext.define("PartKeepr.Components.OctoPart.SearchWindow", {
extend: "Ext.window.Window",
title: i18n("OctoPart Search"),
title: i18n("Octopart Search"),
iconCls: "partkeepr-icon octopart",
width: 750,
height: 300,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Ext.define('PartKeepr.PartEditorWindow', {
this.editor.on("itemSaved", this.onItemSaved, this);

this.octoPartButton = Ext.create("Ext.button.Button", {
text: i18n("OctoPart…"),
text: i18n("Octopart…"),
iconCls: 'partkeepr-icon octopart',
handler: Ext.bind(this.onOctoPartClick, this)
});
Expand Down Expand Up @@ -143,7 +143,7 @@ Ext.define('PartKeepr.PartEditorWindow', {
this.octoPartQueryWindow.startSearch(this.editor.nameField.getValue());
this.octoPartQueryWindow.on("refreshData", this.onRefreshData, this);
} else {
Ext.MessageBox.alert(i18n("OctoPart is not configured"), i18n("Your administrator needs to configure the API key for OctoPart in the parameters.php file - see parameters.php.dist for instructions"));
Ext.MessageBox.alert(i18n("Octopart is not configured"), i18n("Your administrator needs to configure the API key for Octopart in the parameters.php file - see parameters.php.dist for instructions"));
}
},
onRefreshData: function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Ext.define('PartKeepr.Components.UserPreferences.Preferences.OctoPartConfigurati
},
statics: {
iconCls: 'partkeepr-icon octopart',
title: i18n('OctoPart'),
title: i18n('Octopart'),
menuPath: [{iconCls: 'fugue-icon ui-scroll-pane-image', text: i18n("User Interface")}]
}
});
31 changes: 19 additions & 12 deletions src/PartKeepr/OctoPartBundle/Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function indexAction($id)
*/
public function getPartsByQueryAction(Request $request)
{
$start = 0;
$start = 1;

$responseData = [];

Expand All @@ -50,20 +50,27 @@ public function getPartsByQueryAction(Request $request)

$data = $this->get("partkeepr.octopart_service")->getPartyByQuery($query, $start);

$errors = $data["errors"];
$data = $data["data"]["search"];

$responseData["hits"] = $data["hits"];
$responseData["results"] = [];
$responseData["errors"] = $errors;

foreach ($data["results"] as $result) {
$responseItem = [];
$responseItem["mpn"] = $result["item"]["mpn"];
$responseItem["title"] = $result["snippet"];
$responseItem["manufacturer"] = $result["item"]["manufacturer"]["name"];
$responseItem["numOffers"] = count($result["item"]["offers"]);
$responseItem["numSpecs"] = count($result["item"]["specs"]);
$responseItem["numDatasheets"] = count($result["item"]["datasheets"]);
$responseItem["url"] = $result["item"]["octopart_url"];
$responseItem["uid"] = $result["item"]["uid"];
$responseData["results"][] = $responseItem;
if ($data) {
foreach ($data["results"] as $result) {
$part = $result["part"];
$responseItem = [];
$responseItem["mpn"] = $part["mpn"];
$responseItem["title"] = $part["short_description"];
$responseItem["manufacturer"] = $part["manufacturer"]["name"];
$responseItem["numOffers"] = count($part["sellers"]);
$responseItem["numSpecs"] = count($part["specs"]);
$responseItem["numDatasheets"] = count($part["document_collections"]);
$responseItem["url"] = "https://octopart.com".$part["slug"];
$responseItem["uid"] = $part["id"];
$responseData["results"][] = $responseItem;
}
}

return $responseData;
Expand Down
1 change: 1 addition & 0 deletions src/PartKeepr/OctoPartBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<services>
<service id="partkeepr.octopart_service" class="PartKeepr\OctoPartBundle\Services\OctoPartService">
<argument type="string">%partkeepr.octopart.apikey%</argument>
<argument type="string">%partkeepr.octopart.limit%</argument>
</service>
</services>
</container>
Loading