1 | <?php |
||
127 | class Client extends AbstractHttpClient |
||
128 | { |
||
129 | const DEPRECATION_HEADER = 'X-DEPRECATION-NOTICE'; |
||
130 | |||
131 | /** |
||
132 | * @sideeffect Test123 |
||
133 | * @var LoggerInterface |
||
134 | */ |
||
135 | protected $logger; |
||
136 | |||
137 | /** |
||
138 | * @var Manager |
||
139 | */ |
||
140 | protected $oauthManager; |
||
141 | |||
142 | /** |
||
143 | * @var ClientRequestInterface[] |
||
144 | */ |
||
145 | protected $batchRequests = []; |
||
146 | |||
147 | protected $tokenRefreshed = false; |
||
148 | |||
149 | /** |
||
150 | * @param array|Config $config |
||
151 | * @param $cache |
||
152 | * @param LoggerInterface $logger |
||
153 | */ |
||
154 | 32 | public function __construct($config, $cache = null, LoggerInterface $logger = null) |
|
155 | { |
||
156 | 32 | parent::__construct($config); |
|
157 | |||
158 | 32 | $manager = new Manager($config, $cache); |
|
159 | 32 | $this->setOauthManager($manager); |
|
160 | 32 | $this->setLogger($logger); |
|
161 | 32 | } |
|
162 | |||
163 | /** |
||
164 | * @return Manager |
||
165 | */ |
||
166 | 301 | public function getOauthManager() |
|
167 | { |
||
168 | 301 | return $this->oauthManager; |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * @param Manager $oauthManager |
||
173 | * @return $this |
||
174 | */ |
||
175 | 32 | protected function setOauthManager(Manager $oauthManager) |
|
176 | { |
||
177 | 32 | $this->oauthManager = $oauthManager; |
|
178 | 32 | return $this; |
|
179 | } |
||
180 | |||
181 | /** |
||
182 | * @param LoggerInterface $logger |
||
183 | * @return $this |
||
184 | */ |
||
185 | 32 | protected function setLogger(LoggerInterface $logger = null) |
|
186 | { |
||
187 | 32 | if ($logger instanceof LoggerInterface) { |
|
188 | 12 | $this->logger = $logger; |
|
189 | } |
||
190 | 32 | return $this; |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * @param array $options |
||
195 | * @return AdapterInterface |
||
196 | */ |
||
197 | 325 | public function getHttpClient($options = []) |
|
198 | { |
||
199 | 325 | if (is_null($this->httpClient)) { |
|
200 | 30 | $client = parent::getHttpClient($options); |
|
201 | 30 | if ($this->logger instanceof LoggerInterface) { |
|
202 | 12 | $client->setLogger($this->logger); |
|
203 | } |
||
204 | } |
||
205 | |||
206 | 325 | return $this->httpClient; |
|
207 | } |
||
208 | |||
209 | |||
210 | /** |
||
211 | * @return string |
||
212 | */ |
||
213 | 31 | protected function getBaseUrl() |
|
214 | { |
||
215 | 31 | return $this->getConfig()->getApiUrl() . '/' . $this->getConfig()->getProject() . '/'; |
|
216 | } |
||
217 | |||
218 | /** |
||
219 | * Executes an API request synchronously |
||
220 | * |
||
221 | * @param ClientRequestInterface $request |
||
222 | * @return ApiResponseInterface |
||
223 | * @throws InvalidTokenException |
||
224 | * @throws ApiException |
||
225 | * @throws \Exception |
||
226 | */ |
||
227 | 317 | public function execute(ClientRequestInterface $request) |
|
255 | |||
256 | /** |
||
257 | * Executes an API request asynchronously |
||
258 | * @param ClientRequestInterface $request |
||
259 | * @return ApiResponseInterface |
||
260 | */ |
||
261 | 2 | public function executeAsync(ClientRequestInterface $request) |
|
262 | { |
||
263 | 2 | if ($request instanceof ContextAwareInterface) { |
|
264 | 2 | $request->setContextIfNull($this->getConfig()->getContext()); |
|
265 | } |
||
266 | 2 | $httpRequest = $this->createHttpRequest($request); |
|
267 | 2 | $response = $request->buildResponse($this->getHttpClient()->executeAsync($httpRequest)); |
|
268 | |||
269 | 2 | $response = $response->then( |
|
270 | function ($httpResponse) use ($httpRequest) { |
||
271 | 2 | $this->logDeprecatedRequest($httpResponse, $httpRequest); |
|
272 | 2 | return $httpResponse; |
|
273 | 2 | } |
|
274 | ); |
||
275 | |||
276 | 2 | return $response; |
|
277 | } |
||
278 | |||
279 | /** |
||
280 | * @param ClientRequestInterface $request |
||
281 | * @return RequestInterface |
||
282 | */ |
||
283 | 324 | protected function createHttpRequest(ClientRequestInterface $request) |
|
284 | { |
||
285 | 324 | $token = $this->getOAuthManager()->getToken(); |
|
286 | |||
287 | 324 | $httpRequest = $request->httpRequest(); |
|
288 | $httpRequest = $httpRequest |
||
289 | 324 | ->withHeader('Authorization', 'Bearer ' . $token->getToken()) |
|
290 | ; |
||
291 | 324 | return $httpRequest; |
|
292 | } |
||
293 | |||
294 | /** |
||
295 | * Executes API requests in batch |
||
296 | * @return Response\ApiResponseInterface[] |
||
297 | * @throws ApiException |
||
298 | */ |
||
299 | 234 | public function executeBatch() |
|
332 | |||
333 | /** |
||
334 | * @param $exception |
||
335 | * @return $this |
||
336 | */ |
||
337 | 41 | protected function logException(ApiException $exception) |
|
356 | |||
357 | /** |
||
358 | * @param ResponseInterface $response |
||
359 | * @param RequestInterface $request |
||
360 | * @return $this |
||
361 | */ |
||
362 | 313 | protected function logDeprecatedRequest(ResponseInterface $response, RequestInterface $request) |
|
363 | { |
||
364 | 313 | if (is_null($this->logger)) { |
|
379 | |||
380 | /** |
||
381 | * @param RequestInterface $request |
||
382 | * @param ResponseInterface $response |
||
383 | * @return string |
||
384 | */ |
||
385 | protected function format(RequestInterface $request, ResponseInterface $response) |
||
394 | |||
395 | /** |
||
396 | * @return array |
||
397 | */ |
||
398 | 234 | protected function getBatchHttpRequests() |
|
409 | |||
410 | /** |
||
411 | * Adds a request to the batch execution queue |
||
412 | * @param ClientRequestInterface $request |
||
413 | * @return $this |
||
414 | */ |
||
415 | 234 | public function addBatchRequest(ClientRequestInterface $request) |
|
423 | |||
424 | /** |
||
425 | * Instantiates a client with the given config |
||
426 | * @param Config $config |
||
427 | * @return static |
||
428 | */ |
||
429 | public static function ofConfig(Config $config) |
||
433 | |||
434 | /** |
||
435 | * Instantiates a client with the given config and cache adapter |
||
436 | * @param Config $config |
||
437 | * @param $cache |
||
438 | * @return static |
||
439 | */ |
||
440 | public static function ofConfigAndCache(Config $config, $cache) |
||
444 | |||
445 | /** |
||
446 | * Instantiates a client with the given config and a PSR-3 compliant logger |
||
447 | * @param Config $config |
||
448 | * @param LoggerInterface $logger |
||
449 | * @return static |
||
450 | */ |
||
451 | 4 | public static function ofConfigAndLogger(Config $config, LoggerInterface $logger) |
|
455 | |||
456 | /** |
||
457 | * Instantiates a client with the given config, a cache adapter and a PSR-3 compliant logger |
||
458 | * @param Config $config |
||
459 | * @param $cache |
||
460 | * @param LoggerInterface $logger |
||
461 | * @return static |
||
462 | */ |
||
463 | 2 | public static function ofConfigCacheAndLogger(Config $config, $cache, LoggerInterface $logger) |
|
467 | } |
||
468 |