first commit
This commit is contained in:
50
laravel/app/Services/WbService/WbClient.php
Normal file
50
laravel/app/Services/WbService/WbClient.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user