/** * Applies the ETag or last modified cache control headers to the specified REST API request. The cache control * headers are applied conditionally. The ETag and last modified values will only be applied if they are not * null. Furthermore, the application of each are independent of the other. This could, therefore, only apply a * last modified cache control header and not an ETag cache control header if the ETag property of this instance * were null and last modified was not. * * @param Request $request * @param Request $request */ public function apply(Request $request) { $newRequest = $request; if ($this->etag !== null) { $newRequest = $request->withHeader(HeaderParameter::IF_MATCH, $this->etag); } if ($this->lastModified !== null) { $newRequest = $request->withHeader(HeaderParameter::IF_UNMODIFIED_SINCE, $this->lastModified); } return $newRequest; }
public function addRequiredHeaders() { foreach ($this->defaultHeaders as $k => $v) { // remove any header that is there now $this->request = $this->request->withoutHeader($k); $this->request = $this->request->withHeader($k, $v); } if (!empty($this->subProtocols)) { $this->request = $this->request->withoutHeader('Sec-WebSocket-Protocol'); $this->request = $this->request->withHeader('Sec-WebSocket-Protocol', $this->subProtocols); } $this->request = $this->request->withoutHeader("Sec-WebSocket-Key"); $this->request = $this->request->withHeader("Sec-WebSocket-Key", $this->websocketKey); }
public function testOnlyResponse() { $request = new Request('GET', 'http://petstore.swagger.io/v2/pet/findByStatus'); $request->withHeader('Accept', 'application/json'); $response = $this->guzzleHttpClient->send($request); $this->assertResponseMatch($response, self::$schemaManager, '/v2/pet/findByStatus', 'get'); }
/** * This method adds the current header parameters to the REST API request. * * @param Request $request * @return Request $request */ public function apply(Request $request) { if ($this->replace) { return $request->withHeader($this->name, $this->value); } else { return $request->withAddedHeader($this->name, $this->value); } }
/** * Send request upstream * * @param Request $request * * @return Response * @throws HttpException */ public static function makeRequest(Request $request) { try { $url = Url::createFromUrl($request->fullUrl()); $host = static::getHostFromUrl($url); $client = new Client(['base_uri' => $url->getScheme() . '://' . $host]); $proxyRequest = new GuzzleRequest($request->method(), $request->path()); $headers = $request->header(); array_walk($headers, function ($value, $key) use($proxyRequest) { $proxyRequest->withHeader($key, $value); }); $stream = \GuzzleHttp\Psr7\stream_for(json_encode($request->json()->all())); $response = $client->send($proxyRequest, ['timeout' => 2, 'body' => $stream, 'query' => $request->query(), 'form_params' => $request->input()]); return static::createLocalResponse($response); } catch (Exception $e) { if (get_class($e) == GuzzleException\ClientException::class) { return static::createLocalResponse($e->getResponse()); } abort(404); } }
/** * Adds date (in GMT format) header to the request headers. * * @param \GuzzleHttp\Psr7\Request $request HTTP request object. * * @return \GuzzleHttp\Psr7\Request */ public function handleRequest($request) { $date = gmdate(Resources::AZURE_DATE_FORMAT, time()); return $request->withHeader(Resources::DATE, $date); }
/** * Adds authentication header to the request headers. * * @param \GuzzleHttp\Psr7\Request $request HTTP request object. * * @return \GuzzleHttp\Psr7\Request */ public function handleRequest($request) { $requestHeaders = HttpFormatter::formatHeaders($request->getHeaders()); $signedKey = $this->_authenticationScheme->getAuthorizationHeader($requestHeaders, $request->getUri(), \GuzzleHttp\Psr7\parse_query($request->getUri()->getQuery()), $request->getMethod()); return $request->withHeader(Resources::AUTHENTICATION, $signedKey); }
/** * Build a Guzzle Request with an authorization header. * * @param array $postData * * @return Request */ protected function buildRequest(array $postData) { $uri = $this->buildURI(); $authorization = $this->authorizationBuilder->build($this->apiMethod, (string) $uri, $postData); $originalRequest = new Request($this->apiMethod, $uri); return $originalRequest->withHeader('Authorization', $authorization); }