/**
  * @throws HttpException
  */
 protected function handleError()
 {
     $body = (string) $this->response->getBody();
     $code = (int) $this->response->getStatusCode();
     $content = json_decode($body);
     throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code);
 }
Example #2
0
 public function testConstruct()
 {
     $response = new Response(\Psc\Net\Service::OK, array('blubb' => 'blubb'));
     $this->assertEquals(array('blubb' => 'blubb'), $response->getBody());
     $this->assertEquals(\Psc\Net\Service::OK, $response->getStatus());
     $this->assertInstanceOf('Psc\\Net\\ServiceResponse', $response);
 }
Example #3
0
 /**
  * @throws HttpException
  */
 protected function handleError()
 {
     $body = (string) $this->response->getBody();
     $code = (int) $this->response->getStatusCode();
     $content = json_decode($body);
     $error = $content->errors[0];
     throw new HttpException(isset($error) ? $error->description : 'Request not processed.', $code);
 }
Example #4
0
 public function testResponseValues()
 {
     $resource = $this->prophesize(Resource::CLASS);
     $resource->asJson()->willReturn('foobar');
     $response = new Response($resource->reveal());
     $this->assertSame(200, $response->getStatusCode());
     $this->assertSame('application/hal+json', $response->getHeaderLine('Content-Type'));
     $this->assertSame('foobar', (string) $response->getBody());
 }
Example #5
0
 /**
  * @param Response $response
  */
 function __construct(Response $response)
 {
     $this->response = $response;
     if (($h = $response->getHeader("Content-Type", Header::class)) && $h->match("application/json", Header::MATCH_WORD) && ($failure = json_decode($response->getBody()))) {
         $message = $failure->message;
         if (isset($failure->errors)) {
             $this->errors = (array) $failure->errors;
         }
     } else {
         $message = trim($response->getBody()->toString());
     }
     if (!strlen($message)) {
         $message = $response->getTransferInfo("error");
     }
     if (!strlen($message)) {
         $message = $response->getResponseStatus();
     }
     parent::__construct($message, $response->getResponseCode(), null);
 }
 /**
  * Verify basic functionality of the response object.
  *
  * @test
  * @covers ::__construct
  * @covers ::getHttpCode
  * @covers ::getHeaders
  * @covers ::getBody
  *
  * @return void
  */
 public function construct()
 {
     $httpCode = 200;
     $headers = ['Content-Type' => 'text/json'];
     $body = ['doesnt' => 'matter'];
     $response = new Response($httpCode, $headers, $body);
     $this->assertSame($httpCode, $response->getHttpCode());
     $this->assertSame($headers, $response->getHeaders());
     $this->assertSame($body, $response->getBody());
 }
Example #7
0
 /**
  * Sends the body of the response to the client.
  *
  * @return void
  */
 protected function sendBody()
 {
     $body = $this->response->getBody();
     // Response::setBody() makes an is_callable check when setting the value.
     // is_callable is a fairly heavy function due to the checks it has to
     // perform compared to is_string, so this is a safe and fast way, though
     // not necessarily intuitive way to structure the handling.
     if (is_string($body)) {
         echo $body;
     } else {
         call_user_func($body);
     }
 }
Example #8
0
 public function __construct(Response $parent)
 {
     $status = $parent->getStatus();
     $convertToJson = false;
     $ok = self::statusIsOK($status);
     if ($ok) {
         $contentType = $parent->getHeader('Content-Type');
         if (!empty($contentType)) {
             $convertToJson = stristr($contentType, '/json') !== false;
         }
     }
     $bodyString = $parent->getBody();
     parent::__construct($convertToJson ? json_decode($bodyString) : $bodyString, $parent->getHeaders(), $status);
     $this->ok = $ok;
 }
 public function respond(Response $response)
 {
     $ajaxResponse = array();
     $ajaxResponse['components'] = array();
     $ajaxResponse['header'] = array();
     $ajaxResponse['script'] = $this->script;
     $headerResponse = new HeaderResponse($response);
     foreach ($this->components as $component) {
         $response->clean();
         $component->beforePageRender();
         $component->render();
         $value = $response->getBody();
         $response->clean();
         array_push($ajaxResponse['components'], array('id' => $component->getMarkupId(), 'value' => $value));
         $this->renderComponentHeader($component, $response, $headerResponse);
         $value = $response->getBody();
         array_push($ajaxResponse['header'], $value);
         $response->clean();
     }
     FeedbackModel::get()->cleanup();
     header('Content-Type: application/json');
     print json_encode($ajaxResponse);
 }
Example #10
0
File: API.php Project: m6w6/seekat
 /**
  * Import handler for the endpoint's underlying data
  *
  * \seekat\Call will call this when the request will have finished.
  *
  * @param Response $response
  * @return API self
  * @throws UnexpectedValueException
  * @throws RequestException
  * @throws \Exception
  */
 function import(Response $response) : API
 {
     $this->__log->info(__FUNCTION__ . ": " . $response->getInfo(), ["url" => (string) $this->__url]);
     if ($response->getResponseCode() >= 400) {
         $e = new RequestException($response);
         $this->__log->critical(__FUNCTION__ . ": " . $e->getMessage(), ["url" => (string) $this->__url]);
         throw $e;
     }
     if (!($type = $response->getHeader("Content-Type", Header::class))) {
         $e = new RequestException($response);
         $this->__log->error(__FUNCTION__ . ": Empty Content-Type -> " . $e->getMessage(), ["url" => (string) $this->__url]);
         throw $e;
     }
     try {
         $this->__type = new ContentType($type);
         $this->__data = $this->__type->parseBody($response->getBody());
         if ($link = $response->getHeader("Link", Header::class)) {
             $this->__links = new Links($link);
         }
     } catch (\Exception $e) {
         $this->__log->error(__FUNCTION__ . ": " . $e->getMessage(), ["url" => (string) $this->__url]);
         throw $e;
     }
     return $this;
 }
Example #11
0
 /**
  * Convert guzzle response to data
  *
  * @param Response $response GuzzleHttp\Response
  *
  * @return string or array           data
  */
 public function format($response)
 {
     $data = (string) $response->getBody();
     if ($this->option('json') === true) {
         return $data;
     }
     return json_decode($data);
 }
Example #12
0
 protected function render($data = null, $nested = true)
 {
     extract($data);
     ob_start();
     // Include view files
     if ($nested) {
         include $this->getTemplatePathname('header.php');
     }
     foreach ($this->getTemplates() as $tpl) {
         include $tpl;
     }
     if ($nested) {
         include $this->getTemplatePathname('footer.php');
     }
     $output = ob_get_clean();
     Response::getBody()->write($output);
     return Container::get('response');
 }
Example #13
0
 /**
  * Get the response body as string
  *
  * This method returns the body of the HTTP response (the content), as it
  * should be in it's readable version - that is, after decoding it (if it
  * was decoded), deflating it (if it was gzip compressed), etc.
  *
  * If you want to get the raw body (as transfered on wire) use
  * $this->getRawBody() instead.
  *
  * @return string
  */
 public function getBody()
 {
     if ($this->stream != null) {
         $this->readStream();
     }
     return parent::getBody();
 }
Example #14
0
 /**
  * Get the response body after the request has been sent.
  * If redirects were allowed and several responses were received, the data references the last received response.
  * @return string
  */
 public function getResponseBody()
 {
     return $this->response->getBody();
 }
Example #15
0
 /**
  * Parse a signed JSON response
  *
  * @param Response $response
  * @param SignaturePublicKey $publicKey
  * @return mixed
  * @throws SignatureFailed
  * @throws TransferException
  */
 public function parseSignedJSON(Response $response, SignaturePublicKey $publicKey)
 {
     $code = $response->getStatusCode();
     if ($code >= 200 && $code < 300) {
         $body = (string) $response->getBody();
         $firstNewLine = \strpos($body, "\n");
         // There should be a newline immediately after the base64urlsafe-encoded signature
         if ($firstNewLine !== self::ENCODED_SIGNATURE_LENGTH) {
             throw new SignatureFailed(\sprintf("First newline found at position %s, expected %d.\n%s", \print_r($firstNewLine, true), \print_r(self::ENCODED_SIGNATURE_LENGTH, true), Base64::encode($body)));
         }
         $sig = Base64UrlSafe::decode(Binary::safeSubstr($body, 0, 88));
         $msg = Binary::safeSubstr($body, 89);
         if (!Asymmetric::verify($msg, $publicKey, $sig, true)) {
             throw new SignatureFailed();
         }
         return \Airship\parseJSON($msg, true);
     }
     throw new TransferException();
 }
Example #16
0
 public function testGetBodyReturnsBody()
 {
     $response = new Response('foo');
     $this->assertSame('foo', $response->getBody());
 }
Example #17
0
 /**
  * Gravatar profile
  * 
  * @param Response $response Response from gravatar.com
  */
 public function __construct(Response $response)
 {
     $this->responseData = \array_merge($this->responseData, \unserialize($response->getBody())["entry"][0]);
 }
 /**
  * Use Response object to modify headers and output body
  * @param  Response $response
  */
 private function respond(Response $response)
 {
     foreach ($response->getHeaders() as $header) {
         header($header);
     }
     echo $response->getBody();
 }
Example #19
0
 public static function fromResponse(Response $response)
 {
     return new self($response->getBody(), $response->getContentType());
 }
Example #20
0
 public function testConstructBodyNull()
 {
     $response = new Response(["test" => "value"], null);
     $this->assertNull($response->getBody());
 }
Example #21
0
 /**
  * @param Response $response
  *
  * @return \Generator
  *
  * @resolve string
  */
 protected function getResponseBody(Response $response) : \Generator
 {
     $data = '';
     $stream = $response->getBody();
     while ($stream->isReadable()) {
         $data .= (yield $stream->read());
     }
     return $data;
 }
Example #22
0
 public function render(array $data = [], $nested = true)
 {
     // Set data to display in page
     if (isset($data['template'])) {
         throw new \InvalidArgumentException("Duplicate template key found");
     }
     $data = Container::get('hooks')->fire('view.alter_data', $data);
     extract($data);
     ob_start();
     // Include view files
     if ($nested) {
         include $this->getTemplatePathname('header.php');
     }
     foreach ($this->getTemplates() as $tpl) {
         include $tpl;
     }
     if ($nested) {
         include $this->getTemplatePathname('footer.php');
     }
     $output = ob_get_clean();
     $response = Response::getBody()->write($output);
     return $response;
 }
Example #23
0
 private function assertResponseBody($body)
 {
     $response = new Response($body);
     $this->assertEquals($body, $response->getBody());
 }