From 98ced5d2953a01c31fdbdd7f44a03480f9839de3 Mon Sep 17 00:00:00 2001 From: Oleg Voronkovich Date: Thu, 16 Aug 2018 06:35:57 +0300 Subject: [PATCH] Support all maintained Guzzle versions (v5 and v6) --- src/HttpClient/GuzzleAdapter.php | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/HttpClient/GuzzleAdapter.php b/src/HttpClient/GuzzleAdapter.php index 132744e..4c2b778 100644 --- a/src/HttpClient/GuzzleAdapter.php +++ b/src/HttpClient/GuzzleAdapter.php @@ -24,10 +24,38 @@ public function __construct(ClientInterface $client) public function request(string $uri, string $method = HttpClientInterface::METHOD_GET, array $headers = [], array $data = []): array { - $response = $this->client->request($method, $uri, ['headers' => $headers, 'form_params' => $data]); + $guzzleVersion = (int) $this->client::VERSION; + + $options = ['headers' => $headers]; + + switch ($method) { + case HttpClientInterface::METHOD_GET: + $options['query'] = $data; + break; + case HttpClientInterface::METHOD_POST: + $options[6 > $guzzleVersion ? 'body' : 'form_params'] = $data; + break; + default: + throw new \InvalidArgumentException( + sprintf( + 'Invalid HTTP method "%s". Use "%s" or "%s".', + $method, + HttpClientInterface::METHOD_GET, + HttpClientInterface::METHOD_POST + ) + ); + break; + } + + if (6 > $guzzleVersion) { + $request = $this->client->createRequest($method, $uri, $options); + $response = $this->client->send($request); + } else { + $response = $this->client->request($method, $uri, $options); + } $statusCode = $response->getStatusCode(); - $body = $response instanceof ResponseInterface ? $response->getBody()->getContents() : $response->getBody(); + $body = $response->getBody()->getContents(); return [$statusCode, $body]; }