Skip to content

Commit

Permalink
Support all maintained Guzzle versions (v5 and v6)
Browse files Browse the repository at this point in the history
  • Loading branch information
voronkovich committed Aug 16, 2018
1 parent c382875 commit 98ced5d
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/HttpClient/GuzzleAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down

0 comments on commit 98ced5d

Please sign in to comment.