Allow to configure the currency to get prices in for the LCSCProvider

This commit is contained in:
Jan Böhmer 2024-02-22 23:57:57 +01:00
parent 015b69e601
commit 20be83c345
3 changed files with 25 additions and 2 deletions

2
.env
View file

@ -174,6 +174,8 @@ PROVIDER_MOUSER_SEARCH_WITH_SIGNUP_LANGUAGE='true'
# We dont require an API key for LCSC, just set this to 1 to enable LCSC support # We dont require an API key for LCSC, just set this to 1 to enable LCSC support
PROVIDER_LCSC_ENABLED=0 PROVIDER_LCSC_ENABLED=0
# The currency to get prices in (e.g. EUR, USD, etc.)
PROVIDER_LCSC_CURRENCY=EUR
################################################################################## ##################################################################################
# EDA integration related settings # EDA integration related settings

View file

@ -310,6 +310,7 @@ services:
App\Services\InfoProviderSystem\Providers\LCSCProvider: App\Services\InfoProviderSystem\Providers\LCSCProvider:
arguments: arguments:
$enabled: '%env(bool:PROVIDER_LCSC_ENABLED)%' $enabled: '%env(bool:PROVIDER_LCSC_ENABLED)%'
$currency: '%env(string:PROVIDER_LCSC_CURRENCY)%'
#################################################################################################################### ####################################################################################################################
# API system # API system

View file

@ -29,6 +29,8 @@ use App\Services\InfoProviderSystem\DTOs\ParameterDTO;
use App\Services\InfoProviderSystem\DTOs\PartDetailDTO; 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 Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\Intl\Currencies;
use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\HttpClientInterface;
class LCSCProvider implements InfoProviderInterface class LCSCProvider implements InfoProviderInterface
@ -38,7 +40,7 @@ class LCSCProvider implements InfoProviderInterface
public const DISTRIBUTOR_NAME = 'LCSC'; public const DISTRIBUTOR_NAME = 'LCSC';
public function __construct(private readonly HttpClientInterface $lcscClient, private bool $enabled = true) public function __construct(private readonly HttpClientInterface $lcscClient, private string $currency, private bool $enabled = true)
{ {
} }
@ -71,6 +73,9 @@ class LCSCProvider implements InfoProviderInterface
private function queryDetail(string $id): PartDetailDTO private function queryDetail(string $id): PartDetailDTO
{ {
$response = $this->lcscClient->request('GET', self::ENDPOINT_URL . "/product/detail", [ $response = $this->lcscClient->request('GET', self::ENDPOINT_URL . "/product/detail", [
'headers' => [
'Cookie' => new Cookie('currencyCode', $this->currency)
],
'query' => [ 'query' => [
'productCode' => $id, 'productCode' => $id,
], ],
@ -93,6 +98,9 @@ class LCSCProvider implements InfoProviderInterface
private function queryByTerm(string $term): array private function queryByTerm(string $term): array
{ {
$response = $this->lcscClient->request('GET', self::ENDPOINT_URL . "/search/global", [ $response = $this->lcscClient->request('GET', self::ENDPOINT_URL . "/search/global", [
'headers' => [
'Cookie' => new Cookie('currencyCode', $this->currency)
],
'query' => [ 'query' => [
'keyword' => $term, 'keyword' => $term,
], ],
@ -193,7 +201,7 @@ class LCSCProvider implements InfoProviderInterface
} }
/** /**
* Converts LCSC currency symbol to an ISO code. Have not seen LCSC provide other currencies other than USD yet. * Converts LCSC currency symbol to an ISO code.
* @param string $currency * @param string $currency
* @return string * @return string
*/ */
@ -202,6 +210,18 @@ class LCSCProvider implements InfoProviderInterface
//Decide based on the currency symbol //Decide based on the currency symbol
return match ($currency) { return match ($currency) {
'US$' => 'USD', 'US$' => 'USD',
'€' => 'EUR',
'A$' => 'AUD',
'C$' => 'CAD',
'£' => 'GBP',
'HK$' => 'HKD',
'JP¥' => 'JPY',
'RM' => 'MYR',
'S$' => 'SGD',
'₽' => 'RUB',
'kr' => 'SEK',
'kr.' => 'DKK',
'₹' => 'INR',
default => throw new \RuntimeException('Unknown currency: ' . $currency) default => throw new \RuntimeException('Unknown currency: ' . $currency)
}; };
} }