1 | <?php |
||
126 | class Client extends AbstractHttpClient |
||
127 | { |
||
128 | const DEPRECATION_HEADER = 'X-DEPRECATION-NOTICE'; |
||
129 | |||
130 | /** |
||
131 | * @sideeffect Test123 |
||
132 | * @var LoggerInterface |
||
133 | */ |
||
134 | protected $logger; |
||
135 | |||
136 | /** |
||
137 | * @var Manager |
||
138 | */ |
||
139 | protected $oauthManager; |
||
140 | |||
141 | /** |
||
142 | * @var ClientRequestInterface[] |
||
143 | */ |
||
144 | protected $batchRequests = []; |
||
145 | |||
146 | protected $tokenRefreshed = false; |
||
147 | |||
148 | /** |
||
149 | * @param array|Config $config |
||
150 | * @param $cache |
||
151 | * @param LoggerInterface $logger |
||
152 | */ |
||
153 | 83 | public function __construct($config, $cache = null, LoggerInterface $logger = null) |
|
154 | { |
||
155 | 83 | parent::__construct($config); |
|
156 | |||
157 | 83 | $manager = new Manager($config, $cache); |
|
158 | 83 | $this->setOauthManager($manager); |
|
159 | 83 | $this->setLogger($logger); |
|
160 | 83 | } |
|
161 | |||
162 | /** |
||
163 | * @return Manager |
||
164 | */ |
||
165 | 62 | public function getOauthManager() |
|
169 | |||
170 | /** |
||
171 | * @param Manager $oauthManager |
||
172 | * @return $this |
||
173 | */ |
||
174 | 83 | protected function setOauthManager(Manager $oauthManager) |
|
175 | { |
||
176 | 83 | $this->oauthManager = $oauthManager; |
|
177 | 83 | return $this; |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * @param LoggerInterface $logger |
||
182 | * @return $this |
||
183 | */ |
||
184 | 83 | protected function setLogger(LoggerInterface $logger = null) |
|
185 | { |
||
186 | 83 | if ($logger instanceof LoggerInterface) { |
|
187 | 4 | $this->logger = $logger; |
|
188 | 4 | } |
|
189 | 83 | return $this; |
|
190 | } |
||
191 | |||
192 | /** |
||
193 | * @param array $options |
||
194 | * @return AdapterInterface |
||
195 | */ |
||
196 | 81 | public function getHttpClient($options = []) |
|
197 | { |
||
198 | 81 | if (is_null($this->httpClient)) { |
|
199 | 81 | $client = parent::getHttpClient($options); |
|
200 | 81 | if ($this->logger instanceof LoggerInterface) { |
|
201 | 4 | $client->setLogger($this->logger); |
|
202 | 4 | } |
|
203 | 81 | } |
|
204 | |||
205 | 81 | return $this->httpClient; |
|
206 | } |
||
207 | |||
208 | |||
209 | /** |
||
210 | * @return string |
||
211 | */ |
||
212 | 82 | protected function getBaseUrl() |
|
216 | |||
217 | /** |
||
218 | * Executes an API request synchronously |
||
219 | * |
||
220 | * @param ClientRequestInterface $request |
||
221 | * @return ApiResponseInterface |
||
222 | * @throws InvalidTokenException |
||
223 | * @throws ApiException |
||
224 | * @throws \Exception |
||
225 | */ |
||
226 | 78 | public function execute(ClientRequestInterface $request) |
|
227 | { |
||
228 | 78 | if ($request instanceof ContextAwareInterface) { |
|
229 | 78 | $request->setContextIfNull($this->getConfig()->getContext()); |
|
230 | 78 | } |
|
231 | 78 | $httpRequest = $this->createHttpRequest($request); |
|
232 | |||
233 | try { |
||
234 | 78 | $response = $this->getHttpClient()->execute($httpRequest); |
|
235 | 78 | } catch (ApiException $exception) { |
|
236 | 12 | if ($exception instanceof InvalidTokenException && !$this->tokenRefreshed) { |
|
237 | 1 | $this->tokenRefreshed = true; |
|
238 | 1 | $this->getOauthManager()->refreshToken(); |
|
239 | 1 | return $this->execute($request); |
|
240 | } |
||
241 | 12 | if ($this->getConfig()->getThrowExceptions() || !$exception->getResponse() instanceof ResponseInterface) { |
|
242 | 10 | throw $exception; |
|
243 | } |
||
244 | 2 | $response = $exception->getResponse(); |
|
245 | } |
||
246 | 68 | $this->logDeprecatedRequest($response, $httpRequest); |
|
247 | |||
248 | 68 | $response = $request->buildResponse($response); |
|
249 | |||
250 | 68 | return $response; |
|
251 | } |
||
252 | |||
253 | /** |
||
254 | * Executes an API request asynchronously |
||
255 | * @param ClientRequestInterface $request |
||
256 | * @return ApiResponseInterface |
||
257 | */ |
||
258 | 2 | public function executeAsync(ClientRequestInterface $request) |
|
259 | { |
||
260 | 2 | if ($request instanceof ContextAwareInterface) { |
|
261 | 2 | $request->setContextIfNull($this->getConfig()->getContext()); |
|
262 | 2 | } |
|
263 | 2 | $httpRequest = $this->createHttpRequest($request); |
|
264 | 2 | $response = $request->buildResponse($this->getHttpClient()->executeAsync($httpRequest)); |
|
265 | |||
266 | 2 | $response = $response->then( |
|
267 | function ($httpResponse) use ($httpRequest) { |
||
268 | 2 | $this->logDeprecatedRequest($httpResponse, $httpRequest); |
|
269 | 2 | return $httpResponse; |
|
270 | } |
||
271 | 2 | ); |
|
272 | |||
273 | 2 | return $response; |
|
274 | } |
||
275 | |||
276 | /** |
||
277 | * @param ClientRequestInterface $request |
||
278 | * @return RequestInterface |
||
279 | */ |
||
280 | 81 | protected function createHttpRequest(ClientRequestInterface $request) |
|
281 | { |
||
282 | 81 | $token = $this->getOAuthManager()->getToken(); |
|
283 | |||
284 | 81 | $httpRequest = $request->httpRequest(); |
|
285 | $httpRequest = $httpRequest |
||
286 | 81 | ->withHeader('Authorization', 'Bearer ' . $token->getToken()) |
|
287 | 81 | ; |
|
288 | 81 | return $httpRequest; |
|
289 | } |
||
290 | |||
291 | /** |
||
292 | * Executes API requests in batch |
||
293 | * @return Response\ApiResponseInterface[] |
||
294 | * @throws ApiException |
||
295 | */ |
||
296 | 60 | public function executeBatch() |
|
297 | { |
||
298 | 60 | $requests = $this->getBatchHttpRequests(); |
|
299 | 60 | $httpResponses = $this->getHttpClient()->executeBatch($requests); |
|
300 | |||
301 | 60 | $responses = []; |
|
302 | 60 | foreach ($httpResponses as $key => $httpResponse) { |
|
303 | 60 | $request = $this->batchRequests[$key]; |
|
304 | 60 | $httpRequest = $requests[$key]; |
|
305 | 60 | if ($httpResponse instanceof ApiException) { |
|
306 | 1 | if ($this->getConfig()->getThrowExceptions() || |
|
307 | 1 | !$httpResponse->getResponse() instanceof ResponseInterface |
|
308 | 1 | ) { |
|
309 | throw $httpResponse; |
||
310 | } |
||
311 | 1 | $httpResponse = $httpResponse->getResponse(); |
|
312 | 1 | } |
|
313 | 60 | $responses[$request->getIdentifier()] = $request->buildResponse($httpResponse); |
|
314 | 60 | $this->logDeprecatedRequest($httpResponse, $httpRequest); |
|
315 | 60 | } |
|
316 | 60 | $this->batchRequests = []; |
|
317 | |||
318 | 60 | return $responses; |
|
319 | } |
||
320 | |||
321 | /** |
||
322 | * @param ResponseInterface $response |
||
323 | * @param RequestInterface $request |
||
324 | * @return $this |
||
325 | */ |
||
326 | 71 | protected function logDeprecatedRequest(ResponseInterface $response, RequestInterface $request) |
|
327 | { |
||
328 | 71 | if (is_null($this->logger)) { |
|
329 | 67 | return $this; |
|
330 | } |
||
331 | |||
332 | 4 | if ($response->hasHeader(static::DEPRECATION_HEADER)) { |
|
333 | 2 | $message = sprintf( |
|
334 | 2 | Message::DEPRECATED_METHOD, |
|
335 | 2 | $request->getUri(), |
|
336 | 2 | $request->getMethod(), |
|
337 | 2 | $response->getHeaderLine(static::DEPRECATION_HEADER) |
|
338 | 2 | ); |
|
339 | 2 | $this->logger->warning($message); |
|
340 | 2 | } |
|
341 | 4 | return $this; |
|
342 | } |
||
343 | |||
344 | /** |
||
345 | * @param RequestInterface $request |
||
346 | * @param ResponseInterface $response |
||
347 | * @return string |
||
348 | */ |
||
349 | protected function format(RequestInterface $request, ResponseInterface $response) |
||
350 | { |
||
351 | $entries = [ |
||
352 | $request->getMethod(), |
||
353 | (string)$request->getUri(), |
||
354 | $response->getStatusCode() |
||
355 | ]; |
||
356 | return implode(', ', $entries); |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * @return array |
||
361 | */ |
||
362 | 60 | protected function getBatchHttpRequests() |
|
363 | { |
||
364 | 60 | $requests = array_map( |
|
365 | 60 | function ($request) { |
|
366 | 60 | return $this->createHttpRequest($request); |
|
367 | 60 | }, |
|
368 | 60 | $this->batchRequests |
|
369 | 60 | ); |
|
370 | |||
371 | 60 | return $requests; |
|
372 | } |
||
373 | |||
374 | /** |
||
375 | * Adds a request to the batch execution queue |
||
376 | * @param ClientRequestInterface $request |
||
377 | * @return $this |
||
378 | */ |
||
379 | 60 | public function addBatchRequest(ClientRequestInterface $request) |
|
380 | { |
||
381 | 60 | if ($request instanceof ContextAwareInterface) { |
|
382 | 60 | $request->setContextIfNull($this->getConfig()->getContext()); |
|
383 | 60 | } |
|
384 | 60 | $this->batchRequests[] = $request; |
|
385 | 60 | return $this; |
|
386 | } |
||
387 | |||
388 | /** |
||
389 | * Instantiates a client with the given config |
||
390 | * @param Config $config |
||
391 | * @return static |
||
392 | */ |
||
393 | public static function ofConfig(Config $config) |
||
397 | |||
398 | /** |
||
399 | * Instantiates a client with the given config and cache adapter |
||
400 | * @param Config $config |
||
401 | * @param $cache |
||
402 | * @return static |
||
403 | */ |
||
404 | public static function ofConfigAndCache(Config $config, $cache) |
||
408 | |||
409 | /** |
||
410 | * Instantiates a client with the given config and a PSR-3 compliant logger |
||
411 | * @param Config $config |
||
412 | * @param LoggerInterface $logger |
||
413 | * @return static |
||
414 | */ |
||
415 | public static function ofConfigAndLogger(Config $config, LoggerInterface $logger) |
||
419 | |||
420 | /** |
||
421 | * Instantiates a client with the given config, a cache adapter and a PSR-3 compliant logger |
||
422 | * @param Config $config |
||
423 | * @param $cache |
||
424 | * @param LoggerInterface $logger |
||
425 | * @return static |
||
426 | */ |
||
427 | public static function ofConfigCacheAndLogger(Config $config, $cache, LoggerInterface $logger) |
||
431 | } |
||
432 |