예제 #1
0
 /**
  * @param \Requests_Response $response
  *
  * @return IApiResult
  */
 protected function _getResult(\Requests_Response $response)
 {
     $result = new ApiResult();
     $result->setStatusCode($response->status_code);
     $callId = $response->headers->getValues('X-Call-Id');
     if (!empty($callId)) {
         $result->setCallId(reset($callId));
     }
     $decoded = json_decode($response->body);
     if (isset($decoded->meta) && isset($decoded->data) && isset($decoded->meta->code) && $decoded->meta->code == $response->status_code) {
         $meta = $decoded->meta;
         $data = $decoded->data;
         if (isset($meta->message)) {
             $result->setStatusMessage($meta->message);
         }
         $result->setContent(json_encode($data));
     } else {
         $result->setContent($response->body);
     }
     $cookies = $response->cookies;
     if (!is_array($cookies)) {
         if ($cookies instanceof \IteratorAggregate) {
             $cookies = (array) $cookies->getIterator();
         }
     }
     $result->setCookies($cookies);
     $headers = $response->headers;
     if (!is_array($headers)) {
         if ($headers instanceof \IteratorAggregate) {
             $headers = (array) $headers->getIterator();
         }
     }
     $result->setHeaders($headers);
     return $result;
 }
예제 #2
0
 /**
  * @param ResponseInterface $response
  *
  * @return ApiResult
  */
 protected function _getResult($response)
 {
     if (!$response instanceof ResponseInterface) {
         throw new \InvalidArgumentException("{$response} should be an instance of ResponseInterface");
     }
     $result = new ApiResult();
     $result->setStatusCode($response->getStatusCode());
     $callId = $response->getHeader('X-Call-Id');
     if (!empty($callId)) {
         $result->setCallId($callId);
     }
     $decoded = json_decode((string) $response->getBody());
     if (isset($decoded->meta) && isset($decoded->data) && isset($decoded->meta->code) && $decoded->meta->code == $response->getStatusCode()) {
         $meta = $decoded->meta;
         $data = $decoded->data;
         if (isset($meta->message)) {
             $result->setStatusMessage($meta->message);
         }
         $result->setContent(json_encode($data));
     } else {
         $result->setContent((string) $response->getBody());
     }
     $result->setHeaders($response->getHeaders());
     return $result;
 }