fix(service): make api endpoints configurable

This commit is contained in:
Ondrej Vlach 2024-08-04 15:15:12 +02:00
parent d6e56eed78
commit 1266195e24
No known key found for this signature in database
GPG Key ID: 7F141CDACEDEE2DE
9 changed files with 38 additions and 11 deletions

View File

@ -6,7 +6,9 @@
parameters:
list.page.size: 24
list.page.max_visible_pages: 3
brilo.api.comments: 'https://jsonplaceholder.typicode.com/comments'
brilo.api.posts: 'https://jsonplaceholder.typicode.com/posts'
brilo.api.users: 'https://jsonplaceholder.typicode.com/users'
services:
# default configuration for services in *this* file
_defaults:
@ -31,5 +33,17 @@ services:
$pageSize: '%list.page.size%'
$maxVisiblePages: '%list.page.max_visible_pages%'
App\Service\Remote\BriloApiComments:
arguments:
$url: '%brilo.api.comments%'
App\Service\Remote\BriloApiPosts:
arguments:
$url: '%brilo.api.posts%'
App\Service\Remote\BriloApiUsers:
arguments:
$url: '%brilo.api.users%'
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones

View File

@ -6,9 +6,15 @@ services:
App\Service\Remote\BriloApiUsers:
public: true
autowire: true
arguments:
$url: '%brilo.api.users%'
App\Service\Remote\BriloApiComments:
public: true
autowire: true
arguments:
$url: '%brilo.api.comments%'
App\Service\Remote\BriloApiPosts:
public: true
autowire: true
arguments:
$url: '%brilo.api.posts%'

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);
}
}

View File

@ -55,7 +55,7 @@ qui aperiam non debitis possimus qui neque nisi nulla"
$response = new JsonMockResponse(self::API_POSTS_LIST);
$mockedClient = new MockHttpClient($response);
$briloApiPosts = new BriloApiPosts($mockedClient, $this->getLogger(), $serializer);
$briloApiPosts = new BriloApiPosts($mockedClient, $this->getLogger(), $serializer, 'http://nonexistent.local');
$this->assertEquals($testUserList, $briloApiPosts->getPosts());
}
@ -73,7 +73,7 @@ qui aperiam non debitis possimus qui neque nisi nulla"
return $response;
});
$this->expectException(BriloRemoteApiException::class);
$briloApiPosts = new BriloApiPosts($mockedClient, $this->getLogger(), $serializer);
$briloApiPosts = new BriloApiPosts($mockedClient, $this->getLogger(), $serializer, 'http://nonexistent.local');
$briloApiPosts->getPosts();
}

View File

@ -57,7 +57,7 @@ nihil sint nostrum voluptatem reiciendis et"
$response = new JsonMockResponse(self::API_COMMENTS_LIST);
$mockedClient = new MockHttpClient($response);
$briloApiPosts = new BriloApiComments($mockedClient, $this->getLogger(), $serializer);
$briloApiPosts = new BriloApiComments($mockedClient, $this->getLogger(), $serializer, 'http://nonexistent.local/');
$this->assertEquals($testCommentsList, $briloApiPosts->getComments());
}
@ -75,7 +75,7 @@ nihil sint nostrum voluptatem reiciendis et"
return $response;
});
$this->expectException(BriloRemoteApiException::class);
$briloApiPosts = new BriloApiComments($mockedClient, $this->getLogger(), $serializer);
$briloApiPosts = new BriloApiComments($mockedClient, $this->getLogger(), $serializer, 'http://nonexistent.local');
$briloApiPosts->getComments();
}

View File

@ -85,7 +85,7 @@ class BriloApiUsersTest extends TestCase
$response = new JsonMockResponse(self::API_USER_LIST);
$mockedClient = new MockHttpClient($response);
$briloApiUsers = new BriloApiUsers($mockedClient, $this->getLogger(), $serializer);
$briloApiUsers = new BriloApiUsers($mockedClient, $this->getLogger(), $serializer, 'http://nonexistent.local');
$this->assertEquals($testUserList, $briloApiUsers->getUsers());
}
@ -103,7 +103,7 @@ class BriloApiUsersTest extends TestCase
return $response;
});
$this->expectException(BriloRemoteApiException::class);
$briloApiUsers = new BriloApiUsers($mockedClient, $this->getLogger(), $serializer);
$briloApiUsers = new BriloApiUsers($mockedClient, $this->getLogger(), $serializer, 'http://nonexistent.local');
$briloApiUsers->getUsers();
}