-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.php
44 lines (34 loc) · 1.38 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
namespace lucasaba\RapidAPI\Infra;
use JMS\Serializer\SerializerInterface;
use lucasaba\RapidAPI\Request\IRequest;
use lucasaba\RapidAPI\Response\Response;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class Client implements IClient
{
const BASE_URI = 'https://v3.football.api-sports.io';
const RAPID_HOST = 'v3.football.api-sports.io';
private HttpClientInterface $httpClient;
private SerializerInterface $serializer;
public function __construct(HttpClientInterface $httpClient, SerializerInterface $serializer, string $apiKey)
{
$this->httpClient = $httpClient->withOptions([
'base_uri' => self::BASE_URI,
'headers' => [
'x-rapidapi-host' => self::RAPID_HOST,
'x-rapidapi-key' => $apiKey
]
]);
$this->serializer = $serializer;
}
public function get(IRequest $request, string $serializerClass, bool $saveToFile = false): Response
{
$response = $this->httpClient->request('GET', $request->getEndpoint(), ['query' => $request->getQuery()]);
/** @var Response $deserialized */
$deserialized = $this->serializer->deserialize($response->getContent(), $serializerClass, 'json');
if($saveToFile) {
file_put_contents($deserialized->getGet().'.json', $response->getContent());
}
return $deserialized;
}
}