Example #1
0
 /**
  * {@inheritdoc}
  */
 public function sendRequest(RequestInterface $request)
 {
     $request = $this->sanitizeRequest($request);
     $headers = new Headers();
     foreach ($request->getHeaders() as $key => $value) {
         $headers->addHeader(new GenericHeader($key, $request->getHeaderLine($key)));
     }
     $zendRequest = new Request();
     $zendRequest->setMethod($request->getMethod());
     $zendRequest->setUri((string) $request->getUri());
     $zendRequest->setHeaders($headers);
     $zendRequest->setContent($request->getBody()->getContents());
     $options = ['httpversion' => $request->getProtocolVersion()];
     if (extension_loaded('curl')) {
         $options['curloptions'] = [CURLOPT_HTTP_VERSION => $this->getProtocolVersion($request->getProtocolVersion())];
     }
     $this->client->setOptions($options);
     if ($this->client->getAdapter() instanceof ZendClient\Adapter\Curl && $request->getMethod()) {
         $request = $request->withHeader('Content-Length', '0');
     }
     try {
         $zendResponse = $this->client->send($zendRequest);
     } catch (RuntimeException $exception) {
         throw new NetworkException($exception->getMessage(), $request, $exception);
     }
     return $this->responseFactory->createResponse($zendResponse->getStatusCode(), $zendResponse->getReasonPhrase(), $zendResponse->getHeaders()->toArray(), $zendResponse->getContent(), $zendResponse->getVersion());
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function sendRequest(RequestInterface $request)
 {
     $this->requests[] = $request;
     if (count($this->exceptions) > 0) {
         throw array_shift($this->exceptions);
     }
     if (count($this->responses) > 0) {
         return array_shift($this->responses);
     }
     // Return success response by default
     return $this->responseFactory->createResponse();
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function sendRequest(RequestInterface $request)
 {
     $cakeRequest = new Request();
     $cakeRequest->method($request->getMethod());
     $cakeRequest->url((string) $request->getUri());
     $cakeRequest->version($request->getProtocolVersion());
     $cakeRequest->body($request->getBody()->getContents());
     foreach ($request->getHeaders() as $header => $values) {
         $cakeRequest->header($header, $request->getHeaderLine($header));
     }
     if (null === $cakeRequest->header('Content-Type')) {
         $cakeRequest->header('Content-Type', 'application/x-www-form-urlencoded');
     }
     try {
         $cakeResponse = $this->client->send($cakeRequest, $this->client->config());
     } catch (Exception $exception) {
         throw new NetworkException('Failed to send request', $request, $exception);
     }
     return $this->responseFactory->createResponse($cakeResponse->statusCode(), null, $cakeResponse->headers(), $cakeResponse->body(), $cakeResponse->version());
 }
Example #4
0
 /**
  * Converts a Buzz response into a PSR response.
  *
  * @param BuzzResponse $response
  *
  * @return ResponseInterface
  */
 private function createResponse(BuzzResponse $response)
 {
     $body = $response->getContent();
     return $this->responseFactory->createResponse($response->getStatusCode(), null, $this->getBuzzHeaders($response), $body, number_format($response->getProtocolVersion(), 1));
 }
Example #5
0
 /**
  * Converts a Guzzle response into a PSR response.
  *
  * @param GuzzleResponse $response
  *
  * @return ResponseInterface
  */
 private function createResponse(GuzzleResponse $response)
 {
     $body = $response->getBody();
     return $this->responseFactory->createResponse($response->getStatusCode(), null, $response->getHeaders(), isset($body) ? $body->detach() : null, $response->getProtocolVersion());
 }
Example #6
0
 /**
  * Transform a React Response to a valid PSR7 ResponseInterface instance.
  *
  * @param ReactResponse   $response
  * @param StreamInterface $body
  *
  * @return ResponseInterface
  */
 private function buildResponse(ReactResponse $response, StreamInterface $body)
 {
     $body->rewind();
     return $this->responseFactory->createResponse($response->getCode(), $response->getReasonPhrase(), $response->getHeaders(), $body, $response->getVersion());
 }