Пример #1
0
 /**
  * @covers Hermes\Api\Response::__construct
  * @covers Hermes\Api\Response::getContent
  */
 public function testGetContent()
 {
     $http = new Client();
     $response = new ZendResponse();
     $headers = new Headers();
     $headers->addHeaderLine('Content-Type', 'application/json');
     $response->setHeaders($headers);
     $response->setContent(static::$sampleJson);
     $this->object = new Response($http, $response);
     $content = $this->object->getContent();
 }
Пример #2
0
 /**
  * Perform the request to api server.
  *
  * @param String $path    Example: "/v1/endpoint"
  * @param Array  $headers
  */
 private function doRequest($path, $headers = [])
 {
     if (!$this->isAvailable()) {
         throw new NotAvailableException('Service not available.');
     }
     if ($this->loadBalance !== null) {
         $uri = $this->loadBalance->getUri($this->serviceName);
         $this->zendClient->setUri($uri);
     }
     if ($this->appendPath && strlen($path) > 0 && $this->zendClient->getUri()->getPath() != '/') {
         $path = $this->zendClient->getUri()->getPath() . $path;
     }
     $this->zendClient->getUri()->setPath($path);
     $this->zendClient->getRequest()->getHeaders()->addHeaders($headers);
     if (!$this->zendClient->getRequest()->getHeaders()->has('X-Request-Id')) {
         $this->addRequestId();
     }
     $this->addRequestName($this->serviceName);
     $this->getEventManager()->trigger('request.pre', $this);
     $this->responseTime = 0;
     try {
         $requestTime = microtime(true);
         $zendHttpResponse = $this->zendClient->send();
         $this->responseTime = (double) sprintf('%.2f', (microtime(true) - $requestTime) * 1000);
         $this->addRequestTime($this->responseTime);
         $response = new Response($this->zendClient, $zendHttpResponse, $this->depth);
         $this->reportSuccess();
     } catch (RuntimeException $ex) {
         $this->reportFailure();
         $this->getEventManager()->trigger('request.fail', $this, $ex);
         throw new RuntimeException($ex->getMessage(), $ex->getCode(), $ex);
     } catch (\Exception $ex) {
         $this->reportFailure();
         $this->getEventManager()->trigger('request.fail', $this, $ex);
         throw new RuntimeException($ex->getMessage(), 500, $ex);
     }
     $this->getEventManager()->trigger('request.post', $this);
     $content = $response->getContent();
     return $content;
 }