Skip to content

fix: use Response::toArray instead of decoder #520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions api/src/BookRepository/GutendexBookRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
namespace App\BookRepository;

use App\Entity\Book;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

final readonly class GutendexBookRepository implements RestrictedBookRepositoryInterface
{
public function __construct(
private HttpClientInterface $gutendexClient,
private DecoderInterface $decoder,
) {
}

Expand All @@ -31,7 +29,7 @@ public function find(string $url): ?Book

$book = new Book();

$data = $this->decoder->decode($response->getContent(), 'json');
$data = $response->toArray();
$book->title = $data['title'];
$book->author = $data['authors'][0]['name'] ?? null;

Expand Down
6 changes: 2 additions & 4 deletions api/src/BookRepository/OpenLibraryBookRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
namespace App\BookRepository;

use App\Entity\Book;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

final readonly class OpenLibraryBookRepository implements RestrictedBookRepositoryInterface
{
public function __construct(
private HttpClientInterface $openLibraryClient,
private DecoderInterface $decoder,
) {
}

Expand All @@ -31,13 +29,13 @@ public function find(string $url): ?Book

$book = new Book();

$data = $this->decoder->decode($response->getContent(), 'json');
$data = $response->toArray();
$book->title = $data['title'];

$book->author = null;
if (isset($data['authors'][0]['key'])) {
$authorResponse = $this->openLibraryClient->request('GET', $data['authors'][0]['key'] . '.json', $options);
$author = $this->decoder->decode($authorResponse->getContent(), 'json');
$author = $authorResponse->toArray();
if (isset($author['name'])) {
$book->author = $author['name'];
}
Expand Down
6 changes: 2 additions & 4 deletions api/src/Command/BooksImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\JsonEncode;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand All @@ -25,7 +24,6 @@ final class BooksImportCommand extends Command
{
public function __construct(
private readonly SerializerInterface $serializer,
private readonly DecoderInterface $decoder,
private readonly HttpClientInterface $client,
private readonly LoggerInterface $logger,
) {
Expand Down Expand Up @@ -91,10 +89,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int

private function getData(string $uri): array
{
return $this->decoder->decode($this->client->request(Request::METHOD_GET, $uri, [
return $this->client->request(Request::METHOD_GET, $uri, [
'headers' => [
'Accept' => 'application/json',
],
])->getContent(), 'json');
])->toArray();
}
}