-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathCatApiClient.php
55 lines (45 loc) · 1.03 KB
/
CatApiClient.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
45
46
47
48
49
50
51
52
53
54
55
<?php
namespace Drupal\cat_api\Service;
use Drupal\Component\Serialization\Json;
use GuzzleHttp\Client;
/**
* Class CatApiClient
*
* @package Drupal\cat_api\Service
*/
class CatApiClient implements CatApiClientInterface {
/**
* Prepared instance of http client.
*
* @var \GuzzleHttp\Client
*/
private $httpClient;
/**
* Json serializer.
*
* @var \Drupal\Component\Serialization\Json
*/
private $json;
/**
* CatApiClient constructor.
*
* @param \GuzzleHttp\Client $http_client
* @param \Drupal\Component\Serialization\Json $json
*/
public function __construct(Client $http_client, Json $json) {
$this->httpClient = $http_client;
$this->json = $json;
}
/**
* {@inheritDoc}
*/
public function get(string $endpoint, array $query = []): array {
$response = $this->httpClient->get($endpoint, [
'query' => $query,
]);
if ($response->getStatusCode() === 200) {
return $this->json::decode($response->getBody()->getContents());
}
return [];
}
}