protected function isError(ResponseInterface $response) { if (\array_get($response->getHeaders(), 'RETS-Error', [null])[0] == 1) { return true; } $content_type = \array_get($response->getHeaders(), 'Content-Type', [null])[0]; if ($content_type and strpos($content_type, 'xml') !== false) { return true; } return false; }
/** * @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; }
/** * @param RequestInterface|ResponseInterface $message * @return string */ protected function formatHeaders($message) { $headers = []; foreach ($message->getHeaders() as $header => $value) { $headers[] = sprintf('%s: %s', $header, implode("\n : ", $value)); } return implode("\n", $headers); }
/** * Returns the Guzzle array of headers as a string. * * @param ResponseInterface $response The Guzzle response. * * @return string */ public function getHeadersAsString(ResponseInterface $response) { $headers = $response->getHeaders(); $rawHeaders = []; foreach ($headers as $name => $values) { $rawHeaders[] = $name . ": " . implode(", ", $values); } return implode("\r\n", $rawHeaders); }
/** * Create the response object * * @param ResponseInterface $adapterResponse * @return \Tmdb\HttpClient\Response */ private function createResponse(ResponseInterface $adapterResponse = null) { $response = new Response(); if ($adapterResponse !== null) { $response->setCode($adapterResponse->getStatusCode()); $response->setHeaders(new ParameterBag($adapterResponse->getHeaders())); $response->setBody((string) $adapterResponse->getBody()); } return $response; }
public function save(RequestInterface $request, ResponseInterface $response) { $ttl = (int) $request->getConfig()->get('cache.ttl'); $key = $this->getCacheKey($request); // Persist the response body if needed if ($response->getBody() && $response->getBody()->getSize() > 0) { $this->cache->save($key, array('code' => $response->getStatusCode(), 'headers' => $response->getHeaders(), 'body' => (string) $response->getBody()), $ttl); $request->getConfig()->set('cache.key', $key); } }
public function parseMetadata(ResponseInterface $response) { $metadata = []; foreach ($response->getHeaders() as $header => $value) { $position = strpos($header, static::METADATA_PREFIX); if ($position === 0) { $metadata[ltrim($header, static::METADATA_PREFIX)] = $response->getHeader($header); } } return $metadata; }
/** * Asserts response match with the response schema. * * @param ResponseInterface $response * @param SchemaManager $schemaManager * @param string $path percent-encoded path used on the request. * @param string $httpMethod * @param string $message */ public function assertResponseMatch(ResponseInterface $response, SchemaManager $schemaManager, $path, $httpMethod, $message = '') { $this->assertResponseMediaTypeMatch($response->getHeader('Content-Type'), $schemaManager, $path, $httpMethod, $message); $httpCode = $response->getStatusCode(); $headers = $response->getHeaders(); foreach ($headers as &$value) { $value = implode(', ', $value); } $this->assertResponseHeadersMatch($headers, $schemaManager, $path, $httpMethod, $httpCode, $message); $this->assertResponseBodyMatch($response->json(['object' => true]), $schemaManager, $path, $httpMethod, $httpCode, $message); }
/** * Extract a map of headers with an optional prefix from the response. */ private function extractHeaders($name, Shape $shape, ResponseInterface $response, &$result) { // Check if the headers are prefixed by a location name $result[$name] = []; $prefix = $shape['locationName']; $prefixLen = strlen($prefix); foreach ($response->getHeaders() as $k => $values) { if (!$prefixLen) { $result[$name][$k] = implode(', ', $values); } elseif (stripos($k, $prefix) === 0) { $result[$name][substr($k, $prefixLen)] = implode(', ', $values); } } }
/** * Validate the response. * * @return $this */ protected function validateResponse() { $body = $this->request->getBody(); $this->response = $body->getContents(); // Send new request if ($this->request == false || $this->request->getReasonPhrase() != 'OK') { $this->objDebug->addDebug("Request", substr($this->response, 0, 4096)); $this->objDebug->addDebug("Error Response", substr($this->response, 0, 4096)); throw new \RuntimeException("Error on transmission, with message: " . $this->request->getStatusCode()); } $this->objDebug->addDebug("Request", substr($this->response, 0, 4096)); // Build response Header information. $strResponseHeader = ""; $arrHeaderKeys = array_keys($this->request->getHeaders()); foreach ($arrHeaderKeys as $keyHeader) { $strResponseHeader .= $keyHeader . ": " . $this->request->getHeader($keyHeader) . "\n"; } $this->objDebug->addDebug("Response", $strResponseHeader . "\n\n" . $this->request->getBody()->read(2048)); // Check if we have a response if (strlen($this->response) == 0) { throw new \RuntimeException("We got a blank response from server."); } // Check for "Fatal error" on client side if (strpos($this->response, "Fatal error") !== false) { throw new \RuntimeException("We got a Fatal error on client site. " . $this->response); } // Check for "Warning" on client side if (strpos($this->response, "Warning") !== false) { $intStart = stripos($this->response, "<strong>Warning</strong>:"); $intEnd = stripos($this->response, "on line"); if ($intEnd === false) { $intLength = strlen($this->response) - $intStart; } else { $intLength = $intEnd - $intStart; } throw new \RuntimeException("We got a Warning on client site.<br /><br />" . substr($this->response, $intStart, $intLength)); } return $this; }
/** * @return array */ public function headers() { return $this->response->getHeaders(); }
/** * Conver the Guzzle response to a Symfony response. * * @param ResponseInterface $response * @return Response */ protected function convertResponse(ResponseInterface $response) { return new Response($response->getBody(), $response->getStatusCode(), $response->getHeaders()); }
/** * @param \GuzzleHttp\Message\ResponseInterface $response * * @return array */ protected function getBodyContent(ResponseInterface $response) { $body = $response->getBody(); $headers = $response->getHeaders(); return $this->createContent($headers, $body); }
public function __construct(HttpResponseInterface $response) { $this->_statusCode = $response->getStatusCode(); $this->_headers = $response->getHeaders(); $this->_body = $response->json(); }
/** * Converts a Guzzle response into a PSR response. * * @param GuzzleResponse $response * * @return ResponseInterface */ private function createResponse(GuzzleResponse $response) { $body = $response->getBody(); return $this->messageFactory->createResponse($response->getStatusCode(), null, $response->getHeaders(), isset($body) ? $body->detach() : null, $response->getProtocolVersion()); }
private function createResponse(GuzzleResponse $response) { $body = $response->getBody(); return new Response($response->getStatusCode(), $response->getHeaders(), isset($body) ? $body->detach() : null, $response->getProtocolVersion(), $response->getReasonPhrase()); }
/** * Save data * * @author Florian Preusner * @version 2.1 * @since 2015-05 * * @param ResponseInterface $request */ public function save(ResponseInterface $request) { $this->setHeaders($request->getHeaders()); }
private function createPsr7Response(GuzzleResponse $response) { if ($body = $response->getBody()) { $body = new PsrStream($body); } return new Psr7Response($response->getStatusCode(), $response->getHeaders(), $body, $response->getReasonPhrase()); }
/** * Collect & sanitize data about a Guzzle response. * * @param ResponseInterface $response Guzzle response. * @return array */ private function collectResponse(ResponseInterface $response) { return ['statusCode' => $response->getStatusCode(), 'reasonPhrase' => $response->getReasonPhrase(), 'headers' => $response->getHeaders(), 'body' => (string) $response->getBody()]; }