Moved TME Provider settings to settings-bundle

This commit is contained in:
Jan Böhmer 2024-05-06 00:05:58 +02:00
parent 5a563e4f8f
commit 7ad077862c
6 changed files with 83 additions and 35 deletions

View file

@ -279,18 +279,6 @@ services:
$language: '%env(string:PROVIDER_DIGIKEY_LANGUAGE)%' $language: '%env(string:PROVIDER_DIGIKEY_LANGUAGE)%'
$country: '%env(string:PROVIDER_DIGIKEY_COUNTRY)%' $country: '%env(string:PROVIDER_DIGIKEY_COUNTRY)%'
App\Services\InfoProviderSystem\Providers\TMEClient:
arguments:
$secret: '%env(string:PROVIDER_TME_SECRET)%'
$token: '%env(string:PROVIDER_TME_KEY)%'
App\Services\InfoProviderSystem\Providers\TMEProvider:
arguments:
$currency: '%env(string:PROVIDER_TME_CURRENCY)%'
$country: '%env(string:PROVIDER_TME_COUNTRY)%'
$language: '%env(string:PROVIDER_TME_LANGUAGE)%'
$get_gross_prices: '%env(bool:PROVIDER_TME_GET_GROSS_PRICES)%'
App\Services\InfoProviderSystem\Providers\OctopartProvider: App\Services\InfoProviderSystem\Providers\OctopartProvider:
arguments: arguments:
$clientId: '&env(string:PROVIDER_OCTOPART_CLIENT_ID)%' $clientId: '&env(string:PROVIDER_OCTOPART_CLIENT_ID)%'

View file

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace App\Services\InfoProviderSystem\Providers; namespace App\Services\InfoProviderSystem\Providers;
use App\Settings\InfoProviderSystem\TMESettings;
use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface; use Symfony\Contracts\HttpClient\ResponseInterface;
@ -30,15 +31,15 @@ class TMEClient
{ {
public const BASE_URI = 'https://api.tme.eu'; public const BASE_URI = 'https://api.tme.eu';
public function __construct(private readonly HttpClientInterface $tmeClient, private readonly string $token, private readonly string $secret) public function __construct(private readonly HttpClientInterface $tmeClient, private readonly TMESettings $settings)
{ {
} }
public function makeRequest(string $action, array $parameters): ResponseInterface public function makeRequest(string $action, array $parameters): ResponseInterface
{ {
$parameters['Token'] = $this->token; $parameters['Token'] = $this->settings->apiToken;
$parameters['ApiSignature'] = $this->getSignature($action, $parameters, $this->secret); $parameters['ApiSignature'] = $this->getSignature($action, $parameters, $this->settings->apiSecret);
return $this->tmeClient->request('POST', $this->getUrlForAction($action), [ return $this->tmeClient->request('POST', $this->getUrlForAction($action), [
'body' => $parameters, 'body' => $parameters,
@ -47,7 +48,7 @@ class TMEClient
public function isUsable(): bool public function isUsable(): bool
{ {
return !($this->token === '' || $this->secret === ''); return !($this->settings->apiToken === '' || $this->settings->apiSecret === '');
} }

View file

@ -30,16 +30,14 @@ use App\Services\InfoProviderSystem\DTOs\PartDetailDTO;
use App\Services\InfoProviderSystem\DTOs\PriceDTO; use App\Services\InfoProviderSystem\DTOs\PriceDTO;
use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO; use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO;
use App\Services\InfoProviderSystem\DTOs\SearchResultDTO; use App\Services\InfoProviderSystem\DTOs\SearchResultDTO;
use App\Settings\InfoProviderSystem\TMESettings;
class TMEProvider implements InfoProviderInterface class TMEProvider implements InfoProviderInterface
{ {
private const VENDOR_NAME = 'TME'; private const VENDOR_NAME = 'TME';
public function __construct(private readonly TMEClient $tmeClient, private readonly string $country, public function __construct(private readonly TMEClient $tmeClient, private readonly TMESettings $settings)
private readonly string $language, private readonly string $currency,
/** @var bool If true, the prices are gross prices. If false, the prices are net prices. */
private readonly bool $get_gross_prices)
{ {
} }
@ -67,8 +65,8 @@ class TMEProvider implements InfoProviderInterface
public function searchByKeyword(string $keyword): array public function searchByKeyword(string $keyword): array
{ {
$response = $this->tmeClient->makeRequest('Products/Search', [ $response = $this->tmeClient->makeRequest('Products/Search', [
'Country' => $this->country, 'Country' => $this->settings->country,
'Language' => $this->language, 'Language' => $this->settings->language,
'SearchPlain' => $keyword, 'SearchPlain' => $keyword,
]); ]);
@ -97,8 +95,8 @@ class TMEProvider implements InfoProviderInterface
public function getDetails(string $id): PartDetailDTO public function getDetails(string $id): PartDetailDTO
{ {
$response = $this->tmeClient->makeRequest('Products/GetProducts', [ $response = $this->tmeClient->makeRequest('Products/GetProducts', [
'Country' => $this->country, 'Country' => $this->settings->country,
'Language' => $this->language, 'Language' => $this->settings->language,
'SymbolList' => [$id], 'SymbolList' => [$id],
]); ]);
@ -142,8 +140,8 @@ class TMEProvider implements InfoProviderInterface
public function getFiles(string $id): array public function getFiles(string $id): array
{ {
$response = $this->tmeClient->makeRequest('Products/GetProductsFiles', [ $response = $this->tmeClient->makeRequest('Products/GetProductsFiles', [
'Country' => $this->country, 'Country' => $this->settings->country,
'Language' => $this->language, 'Language' => $this->settings->language,
'SymbolList' => [$id], 'SymbolList' => [$id],
]); ]);
@ -184,10 +182,10 @@ class TMEProvider implements InfoProviderInterface
public function getVendorInfo(string $id, ?string $productURL = null): PurchaseInfoDTO public function getVendorInfo(string $id, ?string $productURL = null): PurchaseInfoDTO
{ {
$response = $this->tmeClient->makeRequest('Products/GetPricesAndStocks', [ $response = $this->tmeClient->makeRequest('Products/GetPricesAndStocks', [
'Country' => $this->country, 'Country' => $this->settings->country,
'Language' => $this->language, 'Language' => $this->settings->language,
'Currency' => $this->currency, 'Currency' => $this->settings->currency,
'GrossPrices' => $this->get_gross_prices, 'GrossPrices' => $this->settings->grossPrices,
'SymbolList' => [$id], 'SymbolList' => [$id],
]); ]);
@ -227,8 +225,8 @@ class TMEProvider implements InfoProviderInterface
public function getParameters(string $id, string|null &$footprint_name = null): array public function getParameters(string $id, string|null &$footprint_name = null): array
{ {
$response = $this->tmeClient->makeRequest('Products/GetParameters', [ $response = $this->tmeClient->makeRequest('Products/GetParameters', [
'Country' => $this->country, 'Country' => $this->settings->country,
'Language' => $this->language, 'Language' => $this->settings->language,
'SymbolList' => [$id], 'SymbolList' => [$id],
]); ]);

View file

@ -34,5 +34,5 @@ class AppSettings
use SettingsTrait; use SettingsTrait;
#[EmbeddedSettings] #[EmbeddedSettings]
public InfoProviderSettings $infoProviders; public ?InfoProviderSettings $infoProviders = null;
} }

View file

@ -33,8 +33,11 @@ class InfoProviderSettings
use SettingsTrait; use SettingsTrait;
#[EmbeddedSettings] #[EmbeddedSettings]
public LCSCSettings $lcsc; public ?LCSCSettings $lcsc = null;
#[EmbeddedSettings] #[EmbeddedSettings]
public MouserSettings $mouser; public ?MouserSettings $mouser = null;
#[EmbeddedSettings]
public ?TMESettings $tme = null;
} }

View file

@ -0,0 +1,58 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Settings\InfoProviderSystem;
use Jbtronics\SettingsBundle\Settings\Settings;
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Form\Extension\Core\Type\CurrencyType;
use Symfony\Component\Form\Extension\Core\Type\LanguageType;
use Symfony\Component\Validator\Constraints as Assert;
#[Settings]
class TMESettings
{
private const SUPPORTED_CURRENCIES = ["EUR", "USD", "PLN", "GBP"];
#[SettingsParameter(envVar: "PROVIDER_TME_KEY")]
public ?string $apiToken = null;
#[SettingsParameter(envVar: "PROVIDER_TME_SECRET")]
public ?string $apiSecret = null;
#[SettingsParameter(formType: CurrencyType::class, formOptions: ["preferred_choices" => self::SUPPORTED_CURRENCIES], envVar: "PROVIDER_TME_CURRENCY")]
#[Assert\Choice(choices: self::SUPPORTED_CURRENCIES)]
public string $currency = "EUR";
#[SettingsParameter(formType: LanguageType::class, formOptions: ["preferred_choices" => ["en", "de", "fr", "pl"]], envVar: "PROVIDER_TME_LANGUAGE")]
#[Assert\Language]
public string $language = "en";
#[SettingsParameter(envVar: "PROVIDER_TME_COUNTRY", formType: CountryType::class, formOptions: ["preferred_choices" => ["DE", "PL", "GB", "FR"]])]
#[Assert\Country]
public string $country = "DE";
#[SettingsParameter(envVar: "bool:PROVIDER_TME_GET_GROSS_PRICES")]
public bool $grossPrices = true;
}