Пример #1
0
 /**
  * Check response status
  *
  * @throws ApigilityClient\Exception\RuntimeException
  * @return Bool
  */
 private function checkResponseStatus()
 {
     if (!$this->httpResponse->isSuccess()) {
         return new TriggerException($this->httpClient, $this->httpResponse);
     }
     return true;
 }
Пример #2
0
 /**
  * @param HttpResponse $response
  *
  * @return $this
  */
 public function setFromResponseObj(HttpResponse $response)
 {
     if ($response->isSuccess()) {
         $this->setStatus($response->isSuccess());
     } else {
         $this->setError($response->getReasonPhrase());
     }
     return $this;
 }
Пример #3
0
 protected function dispatchRequestAndDecodeResponse($url, $method, $data = null)
 {
     $request = new Request();
     $this->lastResponse = null;
     $request->getHeaders()->addHeaders(array('Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8', 'Accept' => 'application/json', 'User-Agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:37.0) Gecko/20100101 Firefox/37.0'));
     if (!empty($this->host)) {
         $request->getHeaders()->addHeaders(array('Host' => $this->host));
     }
     if (!empty($this->key)) {
         $request->getHeaders()->addHeaders(array('Authorization' => 'Bearer ' . $this->key));
     }
     $request->setUri($url);
     $request->setMethod($method);
     if (is_null($data)) {
         $data = array();
     }
     if (isset($this->key)) {
         $data["auth"] = $this->key;
     }
     if ($method == "POST" || $method == "PUT") {
         $request->setPost(new Parameters($data));
         if (isset($this->key)) {
             $request->setQuery(new Parameters(array('auth' => $this->key)));
         }
     } else {
         $request->setQuery(new Parameters($data));
     }
     $this->lastResponse = $this->httpClient->send($request);
     if ($this->lastResponse->isSuccess()) {
         return json_decode($this->lastResponse->getBody(), true);
     } else {
         return array('error' => true, 'headers' => array("code" => $this->lastResponse->getStatusCode(), "reasons" => $this->lastResponse->getReasonPhrase()), 'body' => json_decode($this->lastResponse->getBody(), true));
     }
 }
 /**
  * {@inheritdoc}
  * @see \InoOicClient\Oic\AbstractResponseHandler::handleResponse()
  */
 public function handleResponse(\Zend\Http\Response $httpResponse)
 {
     $responseData = null;
     $decodeException = null;
     try {
         $responseData = $this->getJsonCoder()->decode($httpResponse->getBody());
     } catch (\Exception $e) {
         $decodeException = $e;
     }
     if (!$httpResponse->isSuccess()) {
         if (isset($responseData[Param::ERROR])) {
             $error = $this->getErrorFactory()->createErrorFromArray($responseData);
             $this->setError($error);
             return;
         } else {
             throw new HttpErrorStatusException(sprintf("Error code '%d' from server", $httpResponse->getStatusCode()));
         }
     }
     if (null !== $decodeException) {
         throw new InvalidResponseFormatException('The HTTP response does not contain valid JSON', null, $decodeException);
     }
     try {
         $this->response = $this->getResponseFactory()->createResponse($responseData);
     } catch (\Exception $e) {
         throw new Exception\InvalidResponseException(sprintf("Invalid response: [%s] %s", get_class($e), $e->getMessage()), null, $e);
     }
 }
 /**
  * Parses the HTTP response from a userinfo request.
  * 
  * @param Http\Response $httpResponse
  * @throws HttpAuthenticateException
  * @throws HttpErrorStatusException
  * @throws InvalidResponseFormatException
  * @throws Exception\InvalidResponseException
  */
 public function handleResponse(Http\Response $httpResponse)
 {
     if (!$httpResponse->isSuccess()) {
         $statusCode = $httpResponse->getStatusCode();
         if (401 === $statusCode && ($authenticateHeader = $httpResponse->getHeaders()->get($this->wwwAuthenticateHeaderName)->current())) {
             $params = $this->parseAuthenticateHeaderValue($authenticateHeader->getFieldValue());
             if (isset($params['error'])) {
                 $this->setError($this->getErrorFactory()->createErrorFromArray($params));
                 return;
             }
             throw new HttpAuthenticateException(sprintf("Missing error information in WWW-Authenticate header: %s", $authenticateHeader->getFieldValue()));
         }
         throw new HttpErrorStatusException(sprintf("Error status response from server: %s", $statusCode));
     }
     try {
         $responseData = $this->getJsonCoder()->decode($httpResponse->getBody());
     } catch (\Exception $e) {
         throw new InvalidResponseFormatException('The HTTP response does not contain valid JSON', null, $e);
     }
     try {
         $this->response = $this->getResponseFactory()->createResponse($responseData);
     } catch (\Exception $e) {
         throw new Exception\InvalidResponseException(sprintf("Invalid response: [%s] %s", get_class($e), $e->getMessage()), null, $e);
     }
 }
Пример #6
0
 /**
  * Construtor.
  *
  * @param Zend\Http\Client   $client
  * @param Zend\Http\Response $response
  */
 public function __construct(ZendHttpClient $client, ZendHttpResponse $response, $depth = 0)
 {
     $this->httpClient = $client;
     $this->httpResponse = $response;
     if (!$this->httpResponse->isSuccess()) {
         $error = json_decode($this->httpResponse->getBody());
         if (empty($error)) {
             $error = new \stdClass();
             $error->status = $this->httpResponse->getStatusCode();
             $error->title = $this->httpResponse->getReasonPhrase();
             $error->detail = '';
         }
         if (!isset($error->status)) {
             $error->status = 500;
         }
         if (!isset($error->detail)) {
             $error->detail = 'An error occurred.';
         }
         throw new RuntimeException(json_encode($error, null, 100), $error->status);
     }
     if (!$this->httpResponse->getHeaders()->has('Content-Type')) {
         throw new RuntimeException("Missing 'Content-Type' header.", 500);
     }
     $contentType = $this->httpResponse->getHeaders()->get('Content-Type')->getFieldValue();
     $pos = strpos($contentType, ';');
     if ($pos !== false) {
         $contentType = substr($contentType, 0, $pos);
     }
     if (empty($this->httpResponse->getBody())) {
         $this->content = null;
     } elseif ($contentType == 'application/hal+json' || $contentType == 'application/json') {
         $this->content = new Resource(Hal::fromJson($this->httpResponse->getBody(), $depth));
     } elseif ($contentType == 'application/hal+xml' || $contentType == 'application/xml') {
         $this->content = new Resource(Hal::fromXml($this->httpResponse->getBody(), $depth));
     } else {
         throw new RuntimeException("Unable to handle content type '{$contentType}' for response: '{$this->httpResponse->getBody()}'.", 500);
     }
 }
Пример #7
0
 /**
  * Check for HTTP errors in a response.
  *
  * @param \Zend\Http\Response $result The response to check.
  *
  * @throws BackendException
  * @return void
  */
 public function checkForHttpError($result)
 {
     if (!$result->isSuccess()) {
         throw HttpErrorException::createFromResponse($result);
     }
 }
Пример #8
0
 /**
  * @param  HttpResponse $response
  * @throws Exception\RuntimeException if an error occurred on Postage side
  * @return array
  */
 private function parseResponse(HttpResponse $response)
 {
     $result = json_decode($response->getBody(), true);
     if ($response->isSuccess()) {
         return isset($result['data']) ? $result['data'] : array();
     }
     if ($result['response']['status'] !== 'ok') {
         $errors = false;
         if (isset($result['data']) && isset($result['data']['errors'])) {
             $errors = implode(', ', $result['data']['errors']);
         }
         if (isset($result['response']['message'])) {
             throw new Exception\RuntimeException(sprintf('An error occurred on Postage, message: %s%s', $result['response']['message'], $errors ? ' (' . $errors . ')' : ''));
         } else {
             throw new Exception\RuntimeException(sprintf('An error occurred on Postage, status code: %s%s', $result['response']['status'], $errors ? ' (' . $errors . ')' : ''), (int) $result['response']['status']);
         }
     }
     // We need to return an array and not throw an exception because of the poor Postage API
     // error handling, it may returns an empty array with just status === 'ok'
     return array();
 }
Пример #9
0
 /**
  * @param  HttpResponse $response
  * @throws Exception\InvalidCredentialsException
  * @throws Exception\ValidationErrorException
  * @throws Exception\RuntimeException
  * @return array
  */
 private function parseResponse(HttpResponse $response)
 {
     $result = json_decode($response->getBody(), true);
     if ($response->isSuccess()) {
         return $result;
     }
     switch ($response->getStatusCode()) {
         case 401:
             throw new Exception\InvalidCredentialsException('Authentication error: missing or incorrect Postmark API Key header');
         case 422:
             throw new Exception\ValidationErrorException(sprintf('An error occured on Postmark (error code %s), message: %s', $result['ErrorCode'], $result['Message']), (int) $result['ErrorCode']);
         case 500:
             throw new Exception\RuntimeException('Postmark server error, please try again');
         default:
             throw new Exception\RuntimeException('Unknown error during request to Postmark server');
     }
 }
Пример #10
0
 /**
  * @param  HttpResponse $response
  * @throws Exception\InvalidCredentialsException
  * @throws Exception\ValidationErrorException
  * @throws Exception\UnknownTemplateException
  * @throws Exception\RuntimeException
  * @return array
  */
 private function parseResponse(HttpResponse $response)
 {
     $result = json_decode($response->getBody(), true);
     if ($response->isSuccess()) {
         return $result;
     }
     switch ($result['name']) {
         case 'InvalidKey':
             throw new Exception\InvalidCredentialsException(sprintf('Mandrill authentication error (code %s): %s', $result['code'], $result['message']));
         case 'ValidationError':
             throw new Exception\ValidationErrorException(sprintf('An error occurred on Mandrill (code %s): %s', $result['code'], $result['message']));
         case 'Unknown_Template':
             throw new Exception\UnknownTemplateException(sprintf('An error occurred on Mandrill (code %s): %s', $result['code'], $result['message']));
         default:
             throw new Exception\RuntimeException(sprintf('An error occurred on Mandrill (code %s): %s', $result['code'], $result['message']));
     }
 }
Пример #11
0
 /**
  * @param  HttpResponse $response
  * @throws Exception\RuntimeException
  * @return array
  */
 private function parseResponse(HttpResponse $response)
 {
     $result = json_decode($response->getBody(), true);
     if ($response->isSuccess()) {
         return $result;
     }
     // There is a 4xx error
     if ($response->isClientError()) {
         if (isset($result['errors']) && is_array($result['errors'])) {
             $message = implode(', ', $result['errors']);
         } elseif (isset($result['error'])) {
             $message = $result['error'];
         } else {
             $message = 'Unknown error';
         }
         throw new Exception\RuntimeException(sprintf('An error occured on SendGrid (http code %s), message: %s', $response->getStatusCode(), $message));
     }
     // There is a 5xx error
     throw new Exception\RuntimeException('SendGrid server error, please try again');
 }
Пример #12
0
 /**
  * Check for HTTP errors in a response.
  *
  * @param \Zend\Http\Response $result The response to check.
  *
  * @throws \Exception
  * @return void
  */
 public function checkForHttpError($result)
 {
     if (!$result->isSuccess()) {
         throw new \Exception('HTTP error ' . $result->getStatusCode());
     }
 }
Пример #13
0
 /**
  * Did an error occur in the request?
  *
  * @return bool
  */
 public function isError()
 {
     return !$this->httpResponse->isSuccess();
 }
Пример #14
0
 /**
  * Returns true if the Http status code is 200 or 201 or 204; false otherwise
  *
  * @return bool
  */
 public function isHttpStatusSuccessful()
 {
     return $this->serverRawResponse->isSuccess();
 }
Пример #15
0
 /**
  * @param  HttpResponse $response
  * @throws Exception\InvalidCredentialsException
  * @throws Exception\ValidationErrorException
  * @throws Exception\RuntimeException
  * @return array
  */
 private function parseResponse(HttpResponse $response)
 {
     $result = json_decode($response->getBody(), true);
     if ($response->isSuccess()) {
         return $result;
     }
     switch ($response->getStatusCode()) {
         case 400:
             throw new Exception\ValidationErrorException(sprintf('An error occured on Mailgun, reason: %s', $response->getReasonPhrase()));
         case 401:
             throw new Exception\InvalidCredentialsException('Authentication error: missing or incorrect Mailgun authorization');
         case 402:
             throw new Exception\RuntimeException(sprintf('An error occured on Mailgun, reason: %s', $response->getReasonPhrase()));
         case 500:
         case 502:
         case 503:
         case 504:
             throw new Exception\RuntimeException('Mailgun server error, please try again');
         default:
             throw new Exception\RuntimeException('Unknown error during request to Mailgun server');
     }
 }
Пример #16
0
 protected function processResponse(Response $response)
 {
     if (!$response->isSuccess()) {
         $data = json_decode($response->getBody(), self::JSON_DECODE_TYPE_ARRAY, self::JSON_DECODE_DEPTH);
         if (is_array($data) && array_key_exists('status', $data) && is_array($data['status']) && array_key_exists('code', $data['status']) && array_key_exists('message', $data['status'])) {
             throw new RuntimeException($data['status']['message'], $data['status']['code'], new RuntimeException($response, -1, new RuntimeException($this->getClient()->getLastRawRequest())));
         }
         throw new RuntimeException($response->getReasonPhrase(), $response->getStatusCode(), new RuntimeException($response, -1, new RuntimeException($this->getClient()->getLastRawRequest())));
     }
     $data = json_decode($response->getBody(), self::JSON_DECODE_TYPE_ARRAY, self::JSON_DECODE_DEPTH);
     if (!is_array($data)) {
         throw new RuntimeException($response->getReasonPhrase(), $response->getStatusCode(), new RuntimeException($response, -1, new RuntimeException($this->getClient()->getLastRawRequest())));
     }
     if (is_array($data) && array_key_exists('status', $data) && is_array($data['status']) && array_key_exists('code', $data['status']) && array_key_exists('message', $data['status']) && $data['status']['code'] < 0) {
         throw new RuntimeException($data['status']['message'], $data['status']['code'], new RuntimeException($response, -1, new RuntimeException($this->getClient()->getLastRawRequest())));
     }
     return $data;
 }