1 | <?php |
||
19 | class AbstractApi |
||
20 | { |
||
21 | /** |
||
22 | * @var ClientInterface HTTP-client from Guzzle |
||
23 | */ |
||
24 | protected $client; |
||
25 | |||
26 | /** |
||
27 | * @var Serializer |
||
28 | */ |
||
29 | protected $serializer; |
||
30 | |||
31 | /** |
||
32 | * @var LoggerInterface |
||
33 | */ |
||
34 | protected $logger; |
||
35 | |||
36 | /** |
||
37 | * @var string Authentication token for API |
||
38 | */ |
||
39 | protected $authToken; |
||
40 | |||
41 | /** |
||
42 | * @var string CSRF-token for API |
||
43 | */ |
||
44 | protected $csRfToken; |
||
45 | |||
46 | |||
47 | public function __construct(ClientInterface $httpClient, Serializer $serializer, LoggerInterface $logger) |
||
53 | |||
54 | /** |
||
55 | * Make GET request and return DTO objects |
||
56 | * |
||
57 | * @return array|object |
||
58 | */ |
||
59 | public function getGetJsonData(string $path, array $parameters = [], string $type, DeserializationContext $context = null) |
||
60 | { |
||
61 | return $this->serializer->deserialize( |
||
62 | $this->getGetResponseBody($path, $parameters), |
||
63 | $type, |
||
64 | 'json', |
||
65 | $context |
||
66 | ); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Make POST request and return DTO objects |
||
71 | * |
||
72 | * @return array|object |
||
73 | */ |
||
74 | public function getPostJsonData(string $path, array $parameters = [], string $type, DeserializationContext $context = null) |
||
75 | { |
||
76 | return $this->serializer->deserialize( |
||
77 | $this->getPostResponseBody($path, $parameters), |
||
78 | $type, |
||
79 | 'json', |
||
80 | $context |
||
81 | ); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Make GET request and return response body |
||
86 | */ |
||
87 | public function getGetResponseBody($path, array $parameters = []): StreamInterface |
||
88 | { |
||
89 | return $this->sendGetRequest($path, $parameters)->getBody(); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Make POST request and return response body |
||
94 | */ |
||
95 | public function getPostResponseBody(string $path, array $parameters = []): StreamInterface |
||
96 | { |
||
97 | return $this->sendPostRequest($path, $parameters)->getBody(); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param string $path Request path |
||
102 | * @param array $parameters Key => Value array of query parameters |
||
103 | * |
||
104 | * @return ResponseInterface |
||
105 | * |
||
106 | * @throws NetworkException |
||
107 | */ |
||
108 | private function sendGetRequest(string $path, array $parameters = []): ResponseInterface |
||
114 | |||
115 | /** |
||
116 | * @param string $path Request path |
||
117 | * @param array $parameters Key => Value array of request data |
||
118 | * |
||
119 | * @return ResponseInterface |
||
120 | * |
||
121 | * @throws NetworkException |
||
122 | */ |
||
123 | private function sendPostRequest(string $path, array $parameters = []): ResponseInterface |
||
129 | |||
130 | private function sendRequest(string $method, string $path, array $parameters): ResponseInterface |
||
131 | { |
||
132 | try { |
||
133 | $response = $this->client->request($method, $path, ['query' => $parameters]); |
||
144 | |||
145 | /** |
||
146 | * @todo refactor with $this->checkResponse() |
||
147 | * |
||
148 | * @param \Exception $e |
||
149 | * |
||
150 | * @throws ForbiddenException |
||
151 | * @throws NotFoundException |
||
152 | * @throws ServerProblemException |
||
153 | * @throws UnauthorizedException |
||
154 | */ |
||
155 | private function processTransferException(\Exception $e): void |
||
172 | |||
173 | /** |
||
174 | * @throws ForbiddenException |
||
175 | * @throws NotFoundException |
||
176 | * @throws ServerProblemException |
||
177 | * @throws UnauthorizedException |
||
178 | */ |
||
179 | private function checkResponse(ResponseInterface $response): void |
||
205 | } |
||
206 |