From f7cea1100cc9b4a2052eb7c834c143a159ed475b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Mon, 31 Jul 2023 00:57:33 +0200 Subject: [PATCH] Allow to configure various options of the octopart provider --- .docker/symfony.conf | 1 + .env | 14 +++++++ config/services.yaml | 9 +++++ .../Providers/OctopartProvider.php | 40 ++++++++++++------- 4 files changed, 49 insertions(+), 15 deletions(-) diff --git a/.docker/symfony.conf b/.docker/symfony.conf index a6b8d7a9..8866bcc3 100644 --- a/.docker/symfony.conf +++ b/.docker/symfony.conf @@ -39,6 +39,7 @@ PassEnv PROVIDER_DIGIKEY_CLIENT_ID PROVIDER_DIGIKEY_SECRET PROVIDER_DIGIKEY_CURRENCY PROVIDER_DIGIKEY_LANGUAGE PROVIDER_DIGIKEY_COUNTRY PassEnv PROVIDER_ELEMENT14_KEY PROVIDER_ELEMENT14_STORE_ID PassEnv PROVIDER_TME_KEY PROVIDER_TME_SECRET PROVIDER_TME_CURRENCY PROVIDER_TME_LANGUAGE PROVIDER_TME_COUNTRY PROVIDER_TME_GET_GROSS_PRICES + PassEnv PROVIDER_OCTOPART_CLIENT_ID PROVIDER_OCTOPART_SECRET PROVIDER_OCTOPART_CURRENCY PROVIDER_OCTOPART_COUNTRY PROVIDER_OCTOPART_SEARCH_LIMIT PROVIDER_OCTOPART_ONLY_AUTHORIZED_SELLERS # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to diff --git a/.env b/.env index d1a42993..cb57a3bb 100644 --- a/.env +++ b/.env @@ -125,6 +125,20 @@ PROVIDER_TME_COUNTRY=DE # Set this to 1 to get gross prices (including VAT) instead of net prices PROVIDER_TME_GET_GROSS_PRICES=1 +# Octopart / Nexar Provider: +# You can get your API key from https://nexar.com/api +PROVIDER_OCTOPART_CLIENT_ID= +PROVIDER_OCTOPART_SECRET= +# The currency and country to get prices for (you have to set both to get meaningful results) +# 3 letter ISO currency code (e.g. EUR, USD, GBP) +PROVIDER_OCTOPART_CURRENCY=EUR +# 2 letter ISO country code (e.g. DE, US, GB) +PROVIDER_OCTOPART_COUNTRY=DE +# The number of results to get from Octopart while searching (please note that this counts towards your API limits) +PROVIDER_OCTOPART_SEARCH_LIMIT=10 +# Set to false to include non authorized offers in the results +PROVIDER_OCTOPART_ONLY_AUTHORIZED_SELLERS=1 + ################################################################################### # SAML Single sign on-settings diff --git a/config/services.yaml b/config/services.yaml index d8be9967..a278ae78 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -268,6 +268,15 @@ services: $language: '%env(string:PROVIDER_TME_LANGUAGE)%' $get_gross_prices: '%env(bool:PROVIDER_TME_GET_GROSS_PRICES)%' + App\Services\InfoProviderSystem\Providers\OctopartProvider: + arguments: + $clientId: '&env(string:PROVIDER_OCTOPART_CLIENT_ID)%' + $secret: '%env(string:PROVIDER_OCTOPART_SECRET)%' + $country: '%env(string:PROVIDER_OCTOPART_COUNTRY)%' + $currency: '%env(string:PROVIDER_OCTOPART_CURRENCY)%' + $search_limit: '%env(int:PROVIDER_OCTOPART_SEARCH_LIMIT)%' + $onlyAuthorizedSellers: '%env(bool:PROVIDER_OCTOPART_ONLY_AUTHORIZED_SELLERS)%' + #################################################################################################################### # Symfony overrides #################################################################################################################### diff --git a/src/Services/InfoProviderSystem/Providers/OctopartProvider.php b/src/Services/InfoProviderSystem/Providers/OctopartProvider.php index 4adb9f47..0333a338 100644 --- a/src/Services/InfoProviderSystem/Providers/OctopartProvider.php +++ b/src/Services/InfoProviderSystem/Providers/OctopartProvider.php @@ -31,7 +31,6 @@ use App\Services\InfoProviderSystem\DTOs\PriceDTO; use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO; use App\Services\OAuth\OAuthTokenManager; use Symfony\Component\HttpClient\HttpOptions; -use Symfony\Component\HttpClient\NativeHttpClient; use Symfony\Contracts\HttpClient\HttpClientInterface; class OctopartProvider implements InfoProviderInterface @@ -67,7 +66,7 @@ class OctopartProvider implements InfoProviderInterface currency quantity } - sellers(authorizedOnly: true) { + sellers(authorizedOnly: $authorizedOnly) { company { name } @@ -105,7 +104,10 @@ class OctopartProvider implements InfoProviderInterface public function __construct(private readonly HttpClientInterface $httpClient, - private readonly OAuthTokenManager $authTokenManager) + private readonly OAuthTokenManager $authTokenManager, + private readonly string $clientId, private readonly string $secret, + private readonly string $currency, private readonly string $country, + private readonly int $search_limit, private readonly bool $onlyAuthorizedSellers) { } @@ -172,7 +174,7 @@ class OctopartProvider implements InfoProviderInterface { //The client ID has to be set and a token has to be available (user clicked connect) //return /*!empty($this->clientId) && */ $this->authTokenManager->hasToken(self::OAUTH_APP_NAME); - return true; + return !empty($this->clientId) && !empty($this->secret); } private function mapLifeCycleStatus(?string $value): ?ManufacturingStatus @@ -251,30 +253,32 @@ class OctopartProvider implements InfoProviderInterface provider_key: $this->getProviderKey(), provider_id: $part['id'], name: $part['mpn'], - description: $part['shortDescription'], - category: $part['category']['name'], - manufacturer: $part['manufacturer']['name'], + description: $part['shortDescription'] ?? null, + category: $part['category']['name'] ?? null, + manufacturer: $part['manufacturer']['name'] ?? null, mpn: $part['mpn'], - preview_image_url: $part['bestImage']['url'], + preview_image_url: $part['bestImage']['url'] ?? null, manufacturing_status: $mStatus, - provider_url: $part['octopartUrl'], + provider_url: $part['octopartUrl'] ?? null, footprint: $footprint, - datasheets: [new FileDTO($part['bestDatasheet']['url'], $part['bestDatasheet']['name'])], + datasheets: $part['bestDatasheet'] !== null ? [new FileDTO($part['bestDatasheet']['url'], $part['bestDatasheet']['name'])]: null, parameters: $parameters, vendor_infos: $orderinfos, mass: $mass, - manufacturer_product_url: $part['manufacturerUrl'], + manufacturer_product_url: $part['manufacturerUrl'] ?? null, ); } public function searchByKeyword(string $keyword): array { $graphQL = sprintf(<<<'GRAPHQL' - query partSearch($keyword: String, $limit: Int) { + query partSearch($keyword: String, $limit: Int, $currency: String!, $country: String!, $authorizedOnly: Boolean!) { supSearch( q: $keyword inStockOnly: false limit: $limit + currency: $currency + country: $country ) { hits results { @@ -288,7 +292,10 @@ class OctopartProvider implements InfoProviderInterface $result = $this->makeGraphQLCall($graphQL, [ 'keyword' => $keyword, - 'limit' => 4, + 'limit' => $this->search_limit, + 'currency' => $this->currency, + 'country' => $this->country, + 'authorizedOnly' => $this->onlyAuthorizedSellers, ]); $tmp = []; @@ -305,14 +312,17 @@ class OctopartProvider implements InfoProviderInterface public function getDetails(string $id): PartDetailDTO { $graphql = sprintf(<<<'GRAPHQL' - query partSearch($ids: [String!]!) { - supParts(ids: $ids) + query partSearch($ids: [String!]!, $currency: String!, $country: String!, $authorizedOnly: Boolean!) { + supParts(ids: $ids, currency: $currency, country: $country) %s } GRAPHQL, self::GRAPHQL_PART_SECTION); $result = $this->makeGraphQLCall($graphql, [ 'ids' => [$id], + 'currency' => $this->currency, + 'country' => $this->country, + 'authorizedOnly' => $this->onlyAuthorizedSellers, ]); return $this->partResultToDTO($result['data']['supParts'][0]);