From de5fad7326f0bd1a3271b9e12cd36709196ba3ab Mon Sep 17 00:00:00 2001 From: Marc Kreidler Date: Mon, 24 Feb 2025 09:38:42 +0100 Subject: [PATCH] Add getToken function analog to Octopart --- .../Providers/BuerklinProvider.php | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/Services/InfoProviderSystem/Providers/BuerklinProvider.php b/src/Services/InfoProviderSystem/Providers/BuerklinProvider.php index 7e265b08..9b32b613 100644 --- a/src/Services/InfoProviderSystem/Providers/BuerklinProvider.php +++ b/src/Services/InfoProviderSystem/Providers/BuerklinProvider.php @@ -30,6 +30,7 @@ use App\Services\InfoProviderSystem\DTOs\PartDetailDTO; use App\Services\InfoProviderSystem\DTOs\PriceDTO; use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO; use Symfony\Component\HttpFoundation\Cookie; +use Symfony\Component\HttpClient\HttpOptions; use Symfony\Contracts\HttpClient\HttpClientInterface; class BuerklinProvider implements InfoProviderInterface @@ -38,19 +39,43 @@ class BuerklinProvider implements InfoProviderInterface private const ENDPOINT_URL = 'https://buerklin.com/buerklinws/v2/buerklin/'; public const DISTRIBUTOR_NAME = 'Buerklin'; + private const OAUTH_APP_NAME = 'ip_buerklin_oauth'; - public function __construct(private readonly HttpClientInterface $buerklinClient, private readonly string $currency, private readonly bool $enabled = true) + public function __construct(private readonly HttpClientInterface $httpClient, + private readonly OAuthTokenManager $authTokenManager, private readonly CacheItemPoolInterface $partInfoCache, + private readonly string $clientId, private readonly string $secret, + private readonly int $username, private readonly bool $password, + private readonly string $currency, private readonly string $language) { } + /** + * Gets the latest OAuth token for the Buerklin API, or creates a new one if none is available + * @return string + */ + private function getToken(): string + { + //Check if we already have a token saved for this app, otherwise we have to retrieve one via OAuth + if (!$this->authTokenManager->hasToken(self::OAUTH_APP_NAME)) { + $this->authTokenManager->retrieveClientCredentialsToken(self::OAUTH_APP_NAME); + } + + $tmp = $this->authTokenManager->getAlwaysValidTokenString(self::OAUTH_APP_NAME); + if ($tmp === null) { + throw new \RuntimeException('Could not retrieve OAuth token for Buerklin'); + } + + return $tmp; + } + public function getProviderInfo(): array { return [ 'name' => 'Buerklin', 'description' => 'This provider uses the Buerklin API to search for parts.', 'url' => 'https://www.buerklin.com/', - 'disabled_help' => 'Set PROVIDER_BUERKLIN_ENABLED to 1 (or true) in your environment variable config.' + 'disabled_help' => 'Set the environment variables PROVIDER_BUERKLIN_CLIENT_ID, PROVIDER_BUERKLIN_SECRET, PROVIDER_BUERKLIN_USERNAME and PROVIDER_BUERKLIN_PASSWORD.' ]; } @@ -62,7 +87,8 @@ class BuerklinProvider implements InfoProviderInterface // This provider is always active public function isActive(): bool { - return $this->enabled; + //The client ID has to be set and a token has to be available (user clicked connect) + return $this->clientId !== '' && $this->secret !== '' && $this->username !== '' && $this->password !== ''; } /**