Пример #1
0
 /**
  * {@inheritdoc}
  */
 public function makeApiCall(RequestInterface $request)
 {
     $response = null;
     if ($this->authenticator->getAccessToken() !== null) {
         $authenticatedRequest = new OAuthDecorator($request);
         $authenticatedRequest->setAccessToken($this->authenticator->getAccessToken());
         $authenticatedRequest->addAuthentication();
         $response = $this->client->makeCall($authenticatedRequest->getFullUrl(), $this->addDefaultHeaders($authenticatedRequest->getHeaders()), $authenticatedRequest->getOptions());
         if ($response['status'] == 200) {
             $this->authenticationRetryLimit = 0;
             try {
                 return new Response($response['body'], $response['headers']);
             } catch (ResponseException $e) {
                 throw new ClientException($e->getMessage(), $e->getCode(), $e);
             }
         }
     }
     if ($response === null || $response['status'] == 401) {
         $this->authenticationRetryLimit++;
         if ($this->authenticationRetryLimit > self::MAX_RETRY_LIMIT) {
             throw new AccessDeniedException('Authentication retry limit reached.');
         }
         try {
             $this->authenticator->setBaseUrl($request->getBaseUrl());
             $this->authenticator->getAuthenticationTokens();
             // Reexecute event
             return $this->makeApiCall($request);
         } catch (AccessDeniedException $e) {
             throw new AccessDeniedException($e->getMessage(), $e->getCode(), $e);
         } catch (AuthenticationException $e) {
             throw new AccessDeniedException('Could not authenticate against API.', $e->getCode(), $e);
         }
     }
     throw new ClientException(sprintf('The server returned an error with status %s.', $response['status']));
 }
 /**
  * Adds authentication details to request with OAuth decorator.
  *
  * @param  RequestInterface $request
  *
  * @return OAuthDecorator OAuth ready decorated Request
  */
 protected function authenticateRequest(RequestInterface $request)
 {
     $authenticatedRequest = new OAuthDecorator($request);
     $authenticatedRequest->setAccessToken($this->authenticator->getAccessToken());
     $authenticatedRequest->addAuthentication();
     return $authenticatedRequest;
 }