fix(service): make api endpoints configurable

This commit is contained in:
2024-08-04 15:15:12 +02:00
parent d6e56eed78
commit 1266195e24
9 changed files with 38 additions and 11 deletions

View File

@@ -18,6 +18,7 @@ class BriloApiComments
protected readonly HttpClientInterface $httpClient,
protected readonly LoggerInterface $logger,
protected readonly SerializerInterface $serializer,
protected readonly string $url,
protected readonly int $retryCount = 3,
protected readonly int $sleepTimeS = 1,
) {
@@ -30,6 +31,6 @@ class BriloApiComments
*/
public function getComments(): array
{
return $this->fetchApiResponse('https://jsonplaceholder.typicode.com/comments', Comment::class);
return $this->fetchApiResponse($this->url, Comment::class);
}
}

View File

@@ -7,6 +7,8 @@ namespace App\Service\Remote;
use App\Entity\Remote\Brilo\Users\User;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -33,7 +35,9 @@ trait BriloApiFetchTrait
*/
protected function fetchApiResponse(string $uri, string $class): array
{
return $this->retryingFailRequest($this->retryCount, $this->sleepTimeS, [TransportException::class, BriloRemoteApiException::class], $this->logger, function () use ($uri, $class) {
return $this->retryingFailRequest($this->retryCount, $this->sleepTimeS, [
TransportException::class, BriloRemoteApiException::class, NotNormalizableValueException::class, MissingConstructorArgumentsException::class
], $this->logger, function () use ($uri, $class) {
$this->logger->debug('Trying to download brilo users');
$response = $this->httpClient->request('GET', $uri);

View File

@@ -18,6 +18,7 @@ class BriloApiPosts
protected readonly HttpClientInterface $httpClient,
protected readonly LoggerInterface $logger,
protected readonly SerializerInterface $serializer,
protected readonly string $url,
protected readonly int $retryCount = 3,
protected readonly int $sleepTimeS = 1,
) {
@@ -30,6 +31,6 @@ class BriloApiPosts
*/
public function getPosts(): array
{
return $this->fetchApiResponse('https://jsonplaceholder.typicode.com/posts', Post::class);
return $this->fetchApiResponse($this->url, Post::class);
}
}

View File

@@ -19,6 +19,7 @@ final class BriloApiUsers
protected readonly HttpClientInterface $httpClient,
protected readonly LoggerInterface $logger,
protected readonly SerializerInterface $serializer,
protected readonly string $url,
protected readonly int $retryCount = 3,
protected readonly int $sleepTimeS = 1,
) {
@@ -31,6 +32,6 @@ final class BriloApiUsers
*/
public function getUsers(): array
{
return $this->fetchApiResponse('https://jsonplaceholder.typicode.com/users', User::class);
return $this->fetchApiResponse($this->url, User::class);
}
}