mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-06 00:14:36 +02:00
Slightly restructured mouserprovider to remove redundant code
This commit is contained in:
parent
a0b31cfd7e
commit
d7bc74fb2b
1 changed files with 39 additions and 76 deletions
|
@ -41,6 +41,7 @@ use App\Services\InfoProviderSystem\DTOs\PriceDTO;
|
||||||
use App\Services\InfoProviderSystem\DTOs\SearchResultDTO;
|
use App\Services\InfoProviderSystem\DTOs\SearchResultDTO;
|
||||||
use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO;
|
use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO;
|
||||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||||
|
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||||
|
|
||||||
|
|
||||||
class MouserProvider implements InfoProviderInterface
|
class MouserProvider implements InfoProviderInterface
|
||||||
|
@ -82,11 +83,7 @@ class MouserProvider implements InfoProviderInterface
|
||||||
return !empty($this->api_key);
|
return !empty($this->api_key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function searchByKeyword(string $keyword): array
|
||||||
* @param string $term
|
|
||||||
* @return PartDetailDTO[]
|
|
||||||
*/
|
|
||||||
private function queryByTerm(string $term): array
|
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
SearchByKeywordRequest description:
|
SearchByKeywordRequest description:
|
||||||
|
@ -129,7 +126,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' => $term,
|
'keyword' => $keyword,
|
||||||
'records' => $this->search_limit, //self::NUMBER_OF_RESULTS,
|
'records' => $this->search_limit, //self::NUMBER_OF_RESULTS,
|
||||||
'startingRecord' => 0,
|
'startingRecord' => 0,
|
||||||
'searchOptions' => $this->options,
|
'searchOptions' => $this->options,
|
||||||
|
@ -138,38 +135,10 @@ class MouserProvider implements InfoProviderInterface
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$arr = $response->toArray();
|
return $this->responseToDTOArray($response);
|
||||||
if (isset($arr['SearchResults'])) {
|
|
||||||
$products = $arr['SearchResults']['Parts'] ?? [];
|
|
||||||
} else {
|
|
||||||
throw new \RuntimeException('Unknown response format');
|
|
||||||
}
|
|
||||||
$result = [];
|
|
||||||
foreach ($products as $product) {
|
|
||||||
$result[] = new PartDetailDTO(
|
|
||||||
provider_key: $this->getProviderKey(),
|
|
||||||
provider_id: $product['MouserPartNumber'],
|
|
||||||
name: $product['ManufacturerPartNumber'],
|
|
||||||
description: $product['Description'],
|
|
||||||
manufacturer: $product['Manufacturer'],
|
|
||||||
mpn: $product['ManufacturerPartNumber'],
|
|
||||||
preview_image_url: $product['ImagePath'],
|
|
||||||
category: $product['Category'],
|
|
||||||
provider_url: $product['ProductDetailUrl'],
|
|
||||||
manufacturing_status: $this->releaseStatusCodeToManufacturingStatus($product['LifecycleStatus'] ?? null),
|
|
||||||
datasheets: $this->parseDataSheets($product['DataSheetUrl'], $product['MouserPartNumber'] ?? null),
|
|
||||||
vendor_infos: $this->pricingToDTOs($product['PriceBreaks'] ?? [], $product['MouserPartNumber'], $product['ProductDetailUrl']),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return $result;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function getDetails(string $id): PartDetailDTO
|
||||||
* @param string $parte
|
|
||||||
* @return PartDetailDTO[]
|
|
||||||
*/
|
|
||||||
private function queryPartNumber(string $parte): array
|
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
SearchByPartRequest description:
|
SearchByPartRequest description:
|
||||||
|
@ -195,18 +164,49 @@ 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' => $parte,
|
'mouserPartNumber' => $id,
|
||||||
'partSearchOptions' => 2
|
'partSearchOptions' => 2
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
|
$tmp = $this->responseToDTOArray($response);
|
||||||
|
|
||||||
|
//Ensure that we have exactly one result
|
||||||
|
if (count($tmp) === 0) {
|
||||||
|
throw new \RuntimeException('No part found with ID ' . $id);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count($tmp) > 1) {
|
||||||
|
throw new \RuntimeException('Multiple parts found with ID ' . $id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $tmp[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCapabilities(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
ProviderCapabilities::BASIC,
|
||||||
|
ProviderCapabilities::PICTURE,
|
||||||
|
ProviderCapabilities::DATASHEET,
|
||||||
|
ProviderCapabilities::PRICE,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ResponseInterface $response
|
||||||
|
* @return PartDetailDTO[]
|
||||||
|
*/
|
||||||
|
private function responseToDTOArray(ResponseInterface $response): array
|
||||||
|
{
|
||||||
$arr = $response->toArray();
|
$arr = $response->toArray();
|
||||||
|
|
||||||
if (isset($arr['SearchResults'])) {
|
if (isset($arr['SearchResults'])) {
|
||||||
$products = $arr['SearchResults']['Parts'] ?? [];
|
$products = $arr['SearchResults']['Parts'] ?? [];
|
||||||
} else {
|
} else {
|
||||||
throw new \RuntimeException('Unknown response format');
|
throw new \RuntimeException('Unknown response format');
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = [];
|
$result = [];
|
||||||
foreach ($products as $product) {
|
foreach ($products as $product) {
|
||||||
$result[] = new PartDetailDTO(
|
$result[] = new PartDetailDTO(
|
||||||
|
@ -225,13 +225,6 @@ class MouserProvider implements InfoProviderInterface
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private function generateProductURL($sku): string
|
|
||||||
{
|
|
||||||
return 'https://' . $this->store_id . '/' . $sku;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -252,7 +245,7 @@ class MouserProvider implements InfoProviderInterface
|
||||||
private function floatvalue($val){
|
private function floatvalue($val){
|
||||||
$val = str_replace(",",".",$val);
|
$val = str_replace(",",".",$val);
|
||||||
$val = preg_replace('/\.(?=.*\.)/', '', $val);
|
$val = preg_replace('/\.(?=.*\.)/', '', $val);
|
||||||
return floatval($val);
|
return (float)$val;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -300,34 +293,4 @@ class MouserProvider implements InfoProviderInterface
|
||||||
default => ManufacturingStatus::ACTIVE,
|
default => ManufacturingStatus::ACTIVE,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public function searchByKeyword(string $keyword): array
|
|
||||||
{
|
|
||||||
return $this->queryByTerm($keyword);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getDetails(string $id): PartDetailDTO
|
|
||||||
{
|
|
||||||
$tmp = $this->queryPartNumber($id);
|
|
||||||
|
|
||||||
if (count($tmp) === 0) {
|
|
||||||
throw new \RuntimeException('No part found with ID ' . $id);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count($tmp) > 1) {
|
|
||||||
throw new \RuntimeException('Multiple parts found with ID ' . $id);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $tmp[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getCapabilities(): array
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
ProviderCapabilities::BASIC,
|
|
||||||
ProviderCapabilities::PICTURE,
|
|
||||||
ProviderCapabilities::DATASHEET,
|
|
||||||
ProviderCapabilities::PRICE,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue