2023-07-09 14:27:41 +02:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2023 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\Services\InfoProviderSystem\Providers;
|
|
|
|
|
|
|
|
use App\Entity\Parts\ManufacturingStatus;
|
2023-07-16 18:35:44 +02:00
|
|
|
use App\Services\InfoProviderSystem\DTOs\FileDTO;
|
2023-07-16 17:10:48 +02:00
|
|
|
use App\Services\InfoProviderSystem\DTOs\ParameterDTO;
|
2023-07-09 23:31:40 +02:00
|
|
|
use App\Services\InfoProviderSystem\DTOs\PartDetailDTO;
|
2023-07-16 17:10:48 +02:00
|
|
|
use App\Services\InfoProviderSystem\DTOs\PriceDTO;
|
|
|
|
use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO;
|
2023-07-09 14:27:41 +02:00
|
|
|
use App\Services\InfoProviderSystem\DTOs\SearchResultDTO;
|
2023-07-16 03:07:53 +02:00
|
|
|
use App\Services\OAuth\OAuthTokenManager;
|
2023-07-09 14:27:41 +02:00
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
|
|
|
|
|
|
class DigikeyProvider implements InfoProviderInterface
|
|
|
|
{
|
|
|
|
|
2023-07-16 03:07:53 +02:00
|
|
|
private const OAUTH_APP_NAME = 'ip_digikey_oauth';
|
|
|
|
|
2023-07-16 17:10:48 +02:00
|
|
|
//Sandbox:'https://sandbox-api.digikey.com'; (you need to change it in knpu/oauth2-client-bundle config too)
|
|
|
|
private const BASE_URI = 'https://api.digikey.com';
|
|
|
|
|
|
|
|
private const VENDOR_NAME = 'DigiKey';
|
|
|
|
|
2023-07-16 03:07:53 +02:00
|
|
|
private readonly HttpClientInterface $digikeyClient;
|
2023-07-09 14:27:41 +02:00
|
|
|
|
2023-07-16 17:10:48 +02:00
|
|
|
|
2023-07-17 00:19:02 +02:00
|
|
|
public function __construct(HttpClientInterface $httpClient, private readonly OAuthTokenManager $authTokenManager,
|
|
|
|
private readonly string $currency, private readonly string $clientId,
|
|
|
|
private readonly string $language, private readonly string $country)
|
2023-07-16 03:07:53 +02:00
|
|
|
{
|
|
|
|
//Create the HTTP client with some default options
|
|
|
|
$this->digikeyClient = $httpClient->withOptions([
|
2023-07-16 17:10:48 +02:00
|
|
|
"base_uri" => self::BASE_URI,
|
2023-07-16 03:07:53 +02:00
|
|
|
"headers" => [
|
|
|
|
"X-DIGIKEY-Client-Id" => $clientId,
|
2023-07-17 00:19:02 +02:00
|
|
|
"X-DIGIKEY-Locale-Site" => $this->country,
|
|
|
|
"X-DIGIKEY-Locale-Language" => $this->language,
|
2023-07-16 17:10:48 +02:00
|
|
|
"X-DIGIKEY-Locale-Currency" => $this->currency,
|
2023-07-16 03:07:53 +02:00
|
|
|
"X-DIGIKEY-Customer-Id" => 0,
|
|
|
|
]
|
|
|
|
]);
|
2023-07-09 14:27:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getProviderInfo(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'name' => 'DigiKey',
|
|
|
|
'description' => 'This provider uses the DigiKey API to search for parts.',
|
|
|
|
'url' => 'https://www.digikey.com/',
|
2023-07-16 03:18:33 +02:00
|
|
|
'oauth_app_name' => self::OAUTH_APP_NAME,
|
2023-07-17 00:19:02 +02:00
|
|
|
'disabled_help' => 'Set the PROVIDER_DIGIKEY_CLIENT_ID and PROVIDER_DIGIKEY_SECRET env option and connect OAuth to enable.'
|
2023-07-09 14:27:41 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCapabilities(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
ProviderCapabilities::BASIC,
|
|
|
|
ProviderCapabilities::FOOTPRINT,
|
|
|
|
ProviderCapabilities::PICTURE,
|
|
|
|
ProviderCapabilities::DATASHEET,
|
|
|
|
ProviderCapabilities::PRICE,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getProviderKey(): string
|
|
|
|
{
|
|
|
|
return 'digikey';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isActive(): bool
|
|
|
|
{
|
2023-07-16 03:18:33 +02:00
|
|
|
//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);
|
2023-07-09 14:27:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function searchByKeyword(string $keyword): array
|
|
|
|
{
|
|
|
|
$request = [
|
|
|
|
'Keywords' => $keyword,
|
|
|
|
'RecordCount' => 50,
|
|
|
|
'RecordStartPosition' => 0,
|
|
|
|
'ExcludeMarketPlaceProducts' => 'true',
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->digikeyClient->request('POST', '/Search/v3/Products/Keyword', [
|
|
|
|
'json' => $request,
|
2023-07-16 03:07:53 +02:00
|
|
|
'auth_bearer' => $this->authTokenManager->getAlwaysValidTokenString(self::OAUTH_APP_NAME)
|
2023-07-09 14:27:41 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$response_array = $response->toArray();
|
|
|
|
|
|
|
|
|
|
|
|
$result = [];
|
|
|
|
$products = $response_array['Products'];
|
|
|
|
foreach ($products as $product) {
|
|
|
|
$result[] = new SearchResultDTO(
|
|
|
|
provider_key: $this->getProviderKey(),
|
|
|
|
provider_id: $product['DigiKeyPartNumber'],
|
|
|
|
name: $product['ManufacturerPartNumber'],
|
2023-07-16 18:35:44 +02:00
|
|
|
description: $product['DetailedDescription'] ?? $product['ProductDescription'],
|
|
|
|
category: $this->getCategoryString($product),
|
2023-07-09 14:27:41 +02:00
|
|
|
manufacturer: $product['Manufacturer']['Value'] ?? null,
|
|
|
|
mpn: $product['ManufacturerPartNumber'],
|
|
|
|
preview_image_url: $product['PrimaryPhoto'] ?? null,
|
|
|
|
manufacturing_status: $this->productStatusToManufacturingStatus($product['ProductStatus']),
|
2023-07-16 17:10:48 +02:00
|
|
|
provider_url: $product['ProductUrl'],
|
2023-07-09 14:27:41 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2023-07-16 17:10:48 +02:00
|
|
|
public function getDetails(string $id): PartDetailDTO
|
|
|
|
{
|
2023-07-17 23:21:30 +02:00
|
|
|
$response = $this->digikeyClient->request('GET', '/Search/v3/Products/' . urlencode($id), [
|
2023-07-16 17:10:48 +02:00
|
|
|
'auth_bearer' => $this->authTokenManager->getAlwaysValidTokenString(self::OAUTH_APP_NAME)
|
|
|
|
]);
|
|
|
|
|
|
|
|
$product = $response->toArray();
|
|
|
|
|
|
|
|
$footprint = null;
|
|
|
|
$parameters = $this->parametersToDTOs($product['Parameters'] ?? [], $footprint);
|
2023-07-16 18:35:44 +02:00
|
|
|
$media = $this->mediaToDTOs($product['MediaLinks']);
|
2023-07-16 17:10:48 +02:00
|
|
|
|
|
|
|
return new PartDetailDTO(
|
|
|
|
provider_key: $this->getProviderKey(),
|
|
|
|
provider_id: $product['DigiKeyPartNumber'],
|
|
|
|
name: $product['ManufacturerPartNumber'],
|
|
|
|
description: $product['DetailedDescription'] ?? $product['ProductDescription'],
|
2023-07-16 18:35:44 +02:00
|
|
|
category: $this->getCategoryString($product),
|
2023-07-16 17:10:48 +02:00
|
|
|
manufacturer: $product['Manufacturer']['Value'] ?? null,
|
|
|
|
mpn: $product['ManufacturerPartNumber'],
|
|
|
|
preview_image_url: $product['PrimaryPhoto'] ?? null,
|
|
|
|
manufacturing_status: $this->productStatusToManufacturingStatus($product['ProductStatus']),
|
|
|
|
provider_url: $product['ProductUrl'],
|
|
|
|
footprint: $footprint,
|
2023-07-16 18:35:44 +02:00
|
|
|
datasheets: $media['datasheets'],
|
|
|
|
images: $media['images'],
|
2023-07-16 17:10:48 +02:00
|
|
|
parameters: $parameters,
|
|
|
|
vendor_infos: $this->pricingToDTOs($product['StandardPricing'] ?? [], $product['DigiKeyPartNumber'], $product['ProductUrl']),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-09 14:27:41 +02:00
|
|
|
/**
|
|
|
|
* Converts the product status from the Digikey API to the manufacturing status used in Part-DB
|
|
|
|
* @param string|null $dk_status
|
|
|
|
* @return ManufacturingStatus|null
|
|
|
|
*/
|
|
|
|
private function productStatusToManufacturingStatus(?string $dk_status): ?ManufacturingStatus
|
|
|
|
{
|
|
|
|
return match ($dk_status) {
|
|
|
|
null => null,
|
|
|
|
'Active' => ManufacturingStatus::ACTIVE,
|
|
|
|
'Obsolete' => ManufacturingStatus::DISCONTINUED,
|
|
|
|
'Discontinued at Digi-Key' => ManufacturingStatus::EOL,
|
|
|
|
'Last Time Buy' => ManufacturingStatus::EOL,
|
|
|
|
'Not For New Designs' => ManufacturingStatus::NRFND,
|
|
|
|
'Preliminary' => ManufacturingStatus::ANNOUNCED,
|
|
|
|
default => ManufacturingStatus::NOT_SET,
|
|
|
|
};
|
|
|
|
}
|
2023-07-09 23:31:40 +02:00
|
|
|
|
2023-07-16 18:35:44 +02:00
|
|
|
private function getCategoryString(array $product): string
|
|
|
|
{
|
|
|
|
$category = $product['Category']['Value'];
|
|
|
|
$sub_category = $product['Family']['Value'];
|
|
|
|
|
|
|
|
//Replace the ' - ' category separator with ' -> '
|
|
|
|
$sub_category = str_replace(' - ', ' -> ', $sub_category);
|
|
|
|
|
|
|
|
return $category . ' -> ' . $sub_category;
|
|
|
|
}
|
|
|
|
|
2023-07-16 17:10:48 +02:00
|
|
|
/**
|
|
|
|
* This function converts the "Parameters" part of the Digikey API response to an array of ParameterDTOs
|
|
|
|
* @param array $parameters
|
|
|
|
* @param string|null $footprint_name You can pass a variable by reference, where the name of the footprint will be stored
|
|
|
|
* @return ParameterDTO[]
|
|
|
|
*/
|
|
|
|
private function parametersToDTOs(array $parameters, string|null &$footprint_name = null): array
|
|
|
|
{
|
|
|
|
$results = [];
|
|
|
|
|
|
|
|
$footprint_name = null;
|
|
|
|
|
|
|
|
foreach ($parameters as $parameter) {
|
|
|
|
if ($parameter['ParameterId'] === 1291) { //Meaning "Manufacturer given footprint"
|
|
|
|
$footprint_name = $parameter['Value'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$results[] = ParameterDTO::parseValueIncludingUnit($parameter['Parameter'], $parameter['Value']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the pricing (StandardPricing field) from the Digikey API to an array of PurchaseInfoDTOs
|
|
|
|
* @param array $price_breaks
|
|
|
|
* @param string $order_number
|
|
|
|
* @param string $product_url
|
|
|
|
* @return PurchaseInfoDTO[]
|
|
|
|
*/
|
|
|
|
private function pricingToDTOs(array $price_breaks, string $order_number, string $product_url): array
|
2023-07-09 23:31:40 +02:00
|
|
|
{
|
2023-07-16 17:10:48 +02:00
|
|
|
$prices = [];
|
|
|
|
|
|
|
|
foreach ($price_breaks as $price_break) {
|
|
|
|
$prices[] = new PriceDTO(minimum_discount_amount: $price_break['BreakQuantity'], price: (string) $price_break['UnitPrice'], currency_iso_code: $this->currency);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
new PurchaseInfoDTO(distributor_name: self::VENDOR_NAME, order_number: $order_number, prices: $prices, product_url: $product_url)
|
|
|
|
];
|
2023-07-09 23:31:40 +02:00
|
|
|
}
|
2023-07-16 18:35:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $media_links
|
|
|
|
* @return FileDTO[][]
|
|
|
|
* @phpstan-return array<string, FileDTO[]>
|
|
|
|
*/
|
|
|
|
private function mediaToDTOs(array $media_links): array
|
|
|
|
{
|
|
|
|
$datasheets = [];
|
|
|
|
$images = [];
|
|
|
|
|
|
|
|
foreach ($media_links as $media_link) {
|
|
|
|
$file = new FileDTO(url: $media_link['Url'], name: $media_link['Title']);
|
|
|
|
|
|
|
|
switch ($media_link['MediaType']) {
|
|
|
|
case 'Datasheets':
|
|
|
|
$datasheets[] = $file;
|
|
|
|
break;
|
|
|
|
case 'Product Photos':
|
|
|
|
$images[] = $file;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'datasheets' => $datasheets,
|
|
|
|
'images' => $images,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-07-09 14:27:41 +02:00
|
|
|
}
|