public function decode(ResponseInterface $raw, $totalTime = 0) { $executionTime = $callTime = 0; if ($raw->hasHeader('X-Execution-Time')) { $executionTime = $raw->getHeader('X-Execution-Time'); } if ($raw->hasHeader('X-Call-Time')) { $callTime = $raw->getHeader('X-Call-Time'); } $body = ''; try { $body = (string) $raw->getBody(); $result = $this->_getDecoder()->decode($body, self::FORMAT, $this->_getDecodeContext()); } catch (\Exception $e) { if (!empty($body)) { $body = ' (' . $body . ')'; } error_log("Invalid API Response: " . $body); throw new InvalidApiResponseException("Unable to decode raw api response.", 500, $e); } if (!property_exists($result, 'type') || !property_exists($result, 'status') || !property_exists($result, 'result') || !property_exists($result->status, 'message') || !property_exists($result->status, 'code')) { error_log("Invalid API Result: " . json_encode($result)); throw new InvalidApiResponseException("Invalid api result", 500); } if ($executionTime === 0) { $executionTime = $totalTime; } if ($callTime === 0) { $callTime = $executionTime; } return ResponseBuilder::create(ApiCallData::create($result->type, $result->result, $result->status->code, $result->status->message, (double) str_replace([',', 'ms'], '', $totalTime), (double) str_replace([',', 'ms'], '', $executionTime), (double) str_replace([',', 'ms'], '', $callTime))); }
private function getExpected(ResponseInterface $response) { if (!($body = $response->getBody())) { return false; } elseif ($response->hasHeader('Transfer-Encoding') || $response->hasHeader('Content-Encoding')) { // Currently does not support un-gzipping or inflating responses return false; } return call_user_func($this->expectedFn, $response); }
private function canValidate(ResponseInterface $response) { if (!($body = $response->getBody())) { return false; } elseif ($response->hasHeader('Transfer-Encoding') || $response->hasHeader('Content-Encoding')) { // Currently does not support un-gzipping or inflating responses return false; } elseif (!$body->isSeekable()) { return false; } elseif ($this->sizeCutoff !== null && $body->getSize() > $this->sizeCutoff) { return false; } return true; }
/** * Gets the Location header. * * @throws \RuntimeException If the Location header is missing * * @return string */ public function getLocation() { if (!$this->response->hasHeader('Location')) { throw new \RuntimeException('Response is missing a Location header'); } return $this->response->getHeader('Location'); }
/** * Gets the age of a response in seconds. * * @param ResponseInterface $response * * @return int */ public static function getResponseAge(ResponseInterface $response) { if ($response->hasHeader('Age')) { return (int) $response->getHeader('Age'); } $date = strtotime($response->getHeader('Date') ?: 'now'); return time() - $date; }
private function handle304Response(RequestInterface $request, ResponseInterface $response, ResponseInterface $validated, CompleteEvent $event) { // Make sure that this response has the same ETag if ($validated->getHeader('ETag') !== $response->getHeader('ETag')) { // Revalidation failed, so remove from cache and retry. $this->storage->delete($request); $event->intercept($event->getClient()->send($request)); return; } // Replace cached headers with any of these headers from the // origin server that might be more up to date $modified = false; foreach (self::$replaceHeaders as $name) { if ($validated->hasHeader($name) && $validated->getHeader($name) != $response->getHeader($name)) { $modified = true; $response->setHeader($name, $validated->getHeader($name)); } } // Store the updated response in cache if ($modified) { $this->storage->cache($request, $response); } }
/** * Parses the fetched tokens. * * @param $resp GuzzleHttp\Message\ReponseInterface the response. * @return array the tokens parsed from the response body. */ public function parseTokenResponse(ResponseInterface $resp) { $body = $resp->getBody()->getContents(); if ($resp->hasHeader('Content-Type') && $resp->getHeader('Content-Type') == 'application/x-www-form-urlencoded') { $res = array(); parse_str($body, $res); return $res; } else { // Assume it's JSON; if it's not there needs to be an exception, so // we use the json decode exception instead of adding a new one. return $resp->json(); } }
/** * Checks that the response has header location contains path * * @Then /^the response header location should contains "([^"]+)"$/ */ public function theResponseHeaderLocationShouldContainsPath($path) { Assertions::assertTrue($this->response->hasHeader('location')); Assertions::assertContains($path, $this->response->getHeader('location')); }
/** * @Then The OC-Checksum header should not be there */ public function theOcChecksumHeaderShouldNotBeThere() { if ($this->response->hasHeader('OC-Checksum')) { throw new \Exception("Expected no checksum header but got " . $this->response->getHeader('OC-Checksum')); } }