51 lines
1.2 KiB
PHP
51 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\WbService;
|
|
|
|
use App\Contracts\WbClientInterface;
|
|
use App\DTO\WbRequestDTO;
|
|
use App\Exceptions\WbServiceException;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Throwable;
|
|
|
|
final class WbClient implements WbClientInterface
|
|
{
|
|
/**
|
|
* @throws WbServiceException
|
|
*/
|
|
public function fetch(WbRequestDTO $requestDTO): array
|
|
{
|
|
$query = [
|
|
'dateFrom' => $requestDTO->dateFrom,
|
|
'page' => $requestDTO->page,
|
|
'limit' => $requestDTO->limit,
|
|
'key' => $requestDTO->endpoint->key(),
|
|
];
|
|
|
|
if ($requestDTO->dateTo !== null) {
|
|
$query['dateTo'] = $requestDTO->dateTo;
|
|
}
|
|
|
|
try {
|
|
$response = Http::timeout(30)
|
|
->retry(3, 500)
|
|
->acceptJson()
|
|
->get(
|
|
$requestDTO->endpoint->url(),
|
|
$query
|
|
);
|
|
|
|
$response->throw();
|
|
} catch (Throwable $e) {
|
|
throw new WbServiceException(
|
|
'WB API request failed: ' . $e->getMessage(),
|
|
previous: $e
|
|
);
|
|
}
|
|
|
|
return $response->json();
|
|
}
|
|
}
|