Esempio n. 1
0
 /**
  * Processes Set-Cookie headers from a request/response pair.
  *
  * @param RequestInterface $request  A request object
  * @param MessageInterface $response A response object
  */
 public function processSetCookieHeaders(RequestInterface $request, MessageInterface $response)
 {
     foreach ($response->getHeader('Set-Cookie', false) as $header) {
         $cookie = new Cookie();
         $cookie->fromSetCookieHeader($header, parse_url($request->getHost(), PHP_URL_HOST));
         $this->addCookie($cookie);
     }
 }
Esempio n. 2
0
 /**
  * {@inheritDoc}
  * @param Response $response
  */
 public function postSend(RequestInterface $request, MessageInterface $response)
 {
     if ($response->isClientError() || $response->isServerError() || $response->getHeader('X-Curl-Error-Result')) {
         if ($this->client instanceof BatchClientInterface) {
             $this->failedMessages[] = [$request, $response];
             return;
         }
         throw new RequestFailedException($request, $response);
     }
 }
 /**
  * {@inheritDoc}
  */
 public function postSend(RequestInterface $request, MessageInterface $response)
 {
     $header = $response->getHeader('Link');
     if (empty($header)) {
         return null;
     }
     $pagination = array();
     foreach (explode(',', $header) as $link) {
         preg_match('/<(.*)>; rel="(.*)"/i', trim($link, ','), $match);
         if (3 === count($match)) {
             $pagination[$match[2]] = $match[1];
         }
     }
     $response->setPagination($pagination);
 }
 /**
  * {@inheritDoc}
  */
 public function postSend(RequestInterface $request, MessageInterface $response)
 {
     /** @var $response \Github\HttpClient\Message\Response */
     if ($response->isClientError() || $response->isServerError()) {
         $remaining = $response->getHeader('X-RateLimit-Remaining');
         if (null !== $remaining && 1 > $remaining) {
             throw new ApiLimitExceedException($this->options['api_limit']);
         }
         $content = $response->getContent();
         if (is_array($content) && isset($content['message'])) {
             if (400 == $response->getStatusCode()) {
                 throw new ErrorException($content['message'], 400);
             } elseif (422 == $response->getStatusCode() && isset($content['errors'])) {
                 $errors = array();
                 foreach ($content['errors'] as $error) {
                     switch ($error['code']) {
                         case 'missing':
                             $errors[] = sprintf('Resource "%s" not exists anymore', $error['resource']);
                             break;
                         case 'missing_field':
                             $errors[] = sprintf('Field "%s" is missing, for resource "%s"', $error['field'], $error['resource']);
                             break;
                         case 'invalid':
                             $errors[] = sprintf('Field "%s" is invalid, for resource "%s"', $error['field'], $error['resource']);
                             break;
                         case 'already_exists':
                             $errors[] = sprintf('Field "%s" already exists, for resource "%s"', $error['field'], $error['resource']);
                             break;
                         default:
                             $errors[] = $error['message'];
                             break;
                     }
                 }
                 throw new ValidationFailedException('Validation Failed: ' . implode(', ', $errors), 422);
             }
         }
         throw new RuntimeException(isset($content['message']) ? $content['message'] : $content, $response->getStatusCode());
     }
 }
 /**
  * Get the 'parsed' content based on the response headers.
  *
  * @param HttpMessageInterface $rawResponse
  *
  * @return mixed
  */
 protected function getResponseContent(HttpMessageInterface $rawResponse)
 {
     if (false !== strpos($rawResponse->getHeader('Content-Type'), 'application/json')) {
         $response = json_decode($rawResponse->getContent(), true);
     } else {
         parse_str($rawResponse->getContent(), $response);
     }
     return $response;
 }
Esempio n. 6
0
 /**
  * {@inheritDoc}
  */
 public function postSend(RequestInterface $request, MessageInterface $response)
 {
     if (Strings::endsWith($response->getHeader('Content-Type'), '/json')) {
         $response->setContent(Json::decode($response->getContent(), Json::FORCE_ARRAY));
     }
 }
Esempio n. 7
0
 /**
  * {@inheritDoc}
  */
 protected function checkApiLimit(MessageInterface $response)
 {
     $this->remainingCalls = $response->getHeader('X-RateLimit-Remaining');
     if (null !== $this->remainingCalls && 1 > $this->remainingCalls) {
         throw new ApiLimitExceedException($this->options['api_limit']);
     }
 }