Example #1
0
 /**
  * Sends a response to the client.
  *
  * Sends only the status code if the response is marked as an error.
  *
  * @param   Response  $response
  */
 public function sendResponse(Response $response)
 {
     \http_response_code($response->getResponseCode());
     if ($response->isErrorResponse()) {
         return;
     }
     $output = fopen($this->outputStream, 'w');
     fwrite($output, $response);
     fclose($output);
 }
Example #2
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);
 }
Example #3
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;
 }