Allow to configure various options of the octopart provider

This commit is contained in:
Jan Böhmer 2023-07-31 00:57:33 +02:00
parent 827dd01e28
commit f7cea1100c
4 changed files with 49 additions and 15 deletions

View file

@ -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

14
.env
View file

@ -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

View file

@ -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
####################################################################################################################

View file

@ -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]);