mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 09:35:49 +02:00
Properly formatted MouserProvider and fixed some type issues
This commit is contained in:
parent
d7bc74fb2b
commit
4f0730b6f9
1 changed files with 28 additions and 25 deletions
|
@ -49,18 +49,15 @@ class MouserProvider implements InfoProviderInterface
|
||||||
|
|
||||||
private const ENDPOINT_URL = 'https://api.mouser.com/api/v2/search';
|
private const ENDPOINT_URL = 'https://api.mouser.com/api/v2/search';
|
||||||
|
|
||||||
private const NUMBER_OF_RESULTS = 50; //MAX 50
|
|
||||||
|
|
||||||
|
|
||||||
public const DISTRIBUTOR_NAME = 'Mouser';
|
public const DISTRIBUTOR_NAME = 'Mouser';
|
||||||
|
|
||||||
public function __construct(private readonly HttpClientInterface $mouserClient,
|
public function __construct(
|
||||||
|
private readonly HttpClientInterface $mouserClient,
|
||||||
private readonly string $api_key,
|
private readonly string $api_key,
|
||||||
private readonly string $language,
|
private readonly string $language,
|
||||||
private readonly string $options,
|
private readonly string $options,
|
||||||
private readonly int $search_limit)
|
private readonly int $search_limit
|
||||||
{
|
) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getProviderInfo(): array
|
public function getProviderInfo(): array
|
||||||
|
@ -123,7 +120,7 @@ class MouserProvider implements InfoProviderInterface
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$response = $this->mouserClient->request('POST', self::ENDPOINT_URL . "/keyword?apiKey=" . $this->api_key , [
|
$response = $this->mouserClient->request('POST', self::ENDPOINT_URL."/keyword?apiKey=".$this->api_key, [
|
||||||
'json' => [
|
'json' => [
|
||||||
'SearchByKeywordRequest' => [
|
'SearchByKeywordRequest' => [
|
||||||
'keyword' => $keyword,
|
'keyword' => $keyword,
|
||||||
|
@ -161,7 +158,7 @@ class MouserProvider implements InfoProviderInterface
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$response = $this->mouserClient->request('POST', self::ENDPOINT_URL . "/partnumber?apiKey=" . $this->api_key , [
|
$response = $this->mouserClient->request('POST', self::ENDPOINT_URL."/partnumber?apiKey=".$this->api_key, [
|
||||||
'json' => [
|
'json' => [
|
||||||
'SearchByPartRequest' => [
|
'SearchByPartRequest' => [
|
||||||
'mouserPartNumber' => $id,
|
'mouserPartNumber' => $id,
|
||||||
|
@ -173,11 +170,11 @@ class MouserProvider implements InfoProviderInterface
|
||||||
|
|
||||||
//Ensure that we have exactly one result
|
//Ensure that we have exactly one result
|
||||||
if (count($tmp) === 0) {
|
if (count($tmp) === 0) {
|
||||||
throw new \RuntimeException('No part found with ID ' . $id);
|
throw new \RuntimeException('No part found with ID '.$id);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count($tmp) > 1) {
|
if (count($tmp) > 1) {
|
||||||
throw new \RuntimeException('Multiple parts found with ID ' . $id);
|
throw new \RuntimeException('Multiple parts found with ID '.$id);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $tmp[0];
|
return $tmp[0];
|
||||||
|
@ -195,7 +192,7 @@ class MouserProvider implements InfoProviderInterface
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ResponseInterface $response
|
* @param ResponseInterface $response
|
||||||
* @return PartDetailDTO[]
|
* @return PartDetailDTO[]
|
||||||
*/
|
*/
|
||||||
private function responseToDTOArray(ResponseInterface $response): array
|
private function responseToDTOArray(ResponseInterface $response): array
|
||||||
|
@ -214,23 +211,25 @@ class MouserProvider implements InfoProviderInterface
|
||||||
provider_id: $product['MouserPartNumber'],
|
provider_id: $product['MouserPartNumber'],
|
||||||
name: $product['ManufacturerPartNumber'],
|
name: $product['ManufacturerPartNumber'],
|
||||||
description: $product['Description'],
|
description: $product['Description'],
|
||||||
|
category: $product['Category'],
|
||||||
manufacturer: $product['Manufacturer'],
|
manufacturer: $product['Manufacturer'],
|
||||||
mpn: $product['ManufacturerPartNumber'],
|
mpn: $product['ManufacturerPartNumber'],
|
||||||
preview_image_url: $product['ImagePath'],
|
preview_image_url: $product['ImagePath'],
|
||||||
category: $product['Category'],
|
|
||||||
provider_url: $product['ProductDetailUrl'],
|
|
||||||
manufacturing_status: $this->releaseStatusCodeToManufacturingStatus($product['LifecycleStatus'] ?? null),
|
manufacturing_status: $this->releaseStatusCodeToManufacturingStatus($product['LifecycleStatus'] ?? null),
|
||||||
datasheets: $this->parseDataSheets($product['DataSheetUrl'], $product['MouserPartNumber'] ?? null),
|
provider_url: $product['ProductDetailUrl'],
|
||||||
vendor_infos: $this->pricingToDTOs($product['PriceBreaks'] ?? [], $product['MouserPartNumber'], $product['ProductDetailUrl']),
|
datasheets: $this->parseDataSheets($product['DataSheetUrl'] ?? null,
|
||||||
|
$product['MouserPartNumber'] ?? null),
|
||||||
|
vendor_infos: $this->pricingToDTOs($product['PriceBreaks'] ?? [], $product['MouserPartNumber'],
|
||||||
|
$product['ProductDetailUrl']),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function parseDataSheets(string $sheetUrl, string $sheetName): ?array
|
private function parseDataSheets(?string $sheetUrl, ?string $sheetName): ?array
|
||||||
{
|
{
|
||||||
if ($sheetUrl === null) {
|
if (empty($sheetUrl)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$result = [];
|
$result = [];
|
||||||
|
@ -242,8 +241,9 @@ class MouserProvider implements InfoProviderInterface
|
||||||
* Mouser API price is a string in the form "n[.,]nnn[.,] currency"
|
* Mouser API price is a string in the form "n[.,]nnn[.,] currency"
|
||||||
* then this convert it to a number
|
* then this convert it to a number
|
||||||
*/
|
*/
|
||||||
private function floatvalue($val){
|
private function priceStrToFloat($val): float
|
||||||
$val = str_replace(",",".",$val);
|
{
|
||||||
|
$val = str_replace(",", ".", $val);
|
||||||
$val = preg_replace('/\.(?=.*\.)/', '', $val);
|
$val = preg_replace('/\.(?=.*\.)/', '', $val);
|
||||||
return (float)$val;
|
return (float)$val;
|
||||||
}
|
}
|
||||||
|
@ -260,18 +260,21 @@ class MouserProvider implements InfoProviderInterface
|
||||||
$prices = [];
|
$prices = [];
|
||||||
|
|
||||||
foreach ($price_breaks as $price_break) {
|
foreach ($price_breaks as $price_break) {
|
||||||
$number = $this->floatvalue($price_break['Price']);
|
$number = $this->priceStrToFloat($price_break['Price']);
|
||||||
$prices[] = new PriceDTO(minimum_discount_amount: $price_break['Quantity'], price: (string)$number, currency_iso_code: $price_break['Currency']);
|
$prices[] = new PriceDTO(
|
||||||
$number = 0;
|
minimum_discount_amount: $price_break['Quantity'],
|
||||||
|
price: (string)$number,
|
||||||
|
currency_iso_code: $price_break['Currency']
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
new PurchaseInfoDTO(distributor_name: self::DISTRIBUTOR_NAME, order_number: $order_number, prices: $prices, product_url: $product_url)
|
new PurchaseInfoDTO(distributor_name: self::DISTRIBUTOR_NAME, order_number: $order_number, prices: $prices,
|
||||||
|
product_url: $product_url)
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Converts the product status from the MOUSER API to the manufacturing status used in Part-DB:
|
/* Converts the product status from the MOUSER API to the manufacturing status used in Part-DB:
|
||||||
Factory Special Order - Ordine speciale in fabbrica
|
Factory Special Order - Ordine speciale in fabbrica
|
||||||
Not Recommended for New Designs - Non raccomandato per nuovi progetti
|
Not Recommended for New Designs - Non raccomandato per nuovi progetti
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue