Added an very basic system to configure info providers

This commit is contained in:
Jan Böhmer 2023-07-09 14:27:41 +02:00
parent 9e3cb4d694
commit e0301f096f
12 changed files with 689 additions and 0 deletions

View file

@ -0,0 +1,121 @@
<?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;
use App\Services\InfoProviderSystem\DTOs\SearchResultDTO;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class DigikeyProvider implements InfoProviderInterface
{
public function __construct(private readonly HttpClientInterface $digikeyClient)
{
}
public function getProviderInfo(): array
{
return [
'name' => 'DigiKey',
'description' => 'This provider uses the DigiKey API to search for parts.',
'url' => 'https://www.digikey.com/',
];
}
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
{
return true;
}
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,
]);
$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'],
description: $product['ProductDescription'],
manufacturer: $product['Manufacturer']['Value'] ?? null,
mpn: $product['ManufacturerPartNumber'],
preview_image_url: $product['PrimaryPhoto'] ?? null,
manufacturing_status: $this->productStatusToManufacturingStatus($product['ProductStatus']),
provider_url: 'https://digikey.com'.$product['ProductUrl'],
);
}
return $result;
}
/**
* 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,
};
}
}

View file

@ -0,0 +1,72 @@
<?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\Services\InfoProviderSystem\DTOs\SearchResultDTO;
interface InfoProviderInterface
{
/**
* Get information about this provider
*
* @return array An associative array with the following keys (? means optional):
* - name: The (user friendly) name of the provider (e.g. "Digikey"), will be translated
* - description?: A short description of the provider (e.g. "Digikey is a ..."), will be translated
* - logo?: The logo of the provider (e.g. "digikey.png")
* - url?: The url of the provider (e.g. "https://www.digikey.com")
* - disabled_help?: A help text which is shown when the provider is disabled, explaining how to enable it
*
* @phpstan-return array{ name: string, description?: string, logo?: string, url?: string, disabled_help?: string }
*/
public function getProviderInfo(): array;
/**
* Returns a unique key for this provider, which will be saved into the database
* and used to identify the provider
* @return string A unique key for this provider (e.g. "digikey")
*/
public function getProviderKey(): string;
/**
* Checks if this provider is enabled or not (meaning that it can be used for searching)
* @return bool True if the provider is enabled, false otherwise
*/
public function isActive(): bool;
/**
* Searches for a keyword and returns a list of search results
* @param string $keyword The keyword to search for
* @return SearchResultDTO[] A list of search results
*/
public function searchByKeyword(string $keyword): array;
/**
* A list of capabilities this provider supports (which kind of data it can provide).
* Not every part have to contain all of these data, but the provider should be able to provide them in general.
* Currently, this list is purely informational and not used in functional checks.
* @return ProviderCapabilities[]
*/
public function getCapabilities(): array;
}

View file

@ -0,0 +1,67 @@
<?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;
/**
* This enum contains all capabilities (which data it can provide) a provider can have.
*/
enum ProviderCapabilities
{
/** Basic information about a part, like the name, description, part number, manufacturer etc */
case BASIC;
/** Information about the footprint of a part */
case FOOTPRINT;
/** Provider can provide a picture for a part */
case PICTURE;
/** Provider can provide datasheets for a part */
case DATASHEET;
/** Provider can provide prices for a part */
case PRICE;
public function getTranslationKey(): string
{
return 'info_providers.capabilities.' . match($this) {
self::BASIC => 'basic',
self::FOOTPRINT => 'footprint',
self::PICTURE => 'picture',
self::DATASHEET => 'datasheet',
self::PRICE => 'price',
};
}
public function getFAIconClass(): string
{
return 'fa-solid ' . match($this) {
self::BASIC => 'fa-info-circle',
self::FOOTPRINT => 'fa-microchip',
self::PICTURE => 'fa-image',
self::DATASHEET => 'fa-file-alt',
self::PRICE => 'fa-money-bill-wave',
};
}
}

View file

@ -0,0 +1,61 @@
<?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;
class TestProvider implements InfoProviderInterface
{
public function getProviderInfo(): array
{
return [
'name' => 'Test Provider',
'description' => 'This is a test provider',
//'url' => 'https://example.com',
'disabled_help' => 'This provider is disabled for testing purposes'
];
}
public function getProviderKey(): string
{
return 'test';
}
public function isActive(): bool
{
return false;
}
public function searchByKeyword(string $keyword): array
{
// TODO: Implement searchByKeyword() method.
}
public function getCapabilities(): array
{
return [
ProviderCapabilities::BASIC,
ProviderCapabilities::FOOTPRINT,
];
}
}