Skip to content

Commit

Permalink
Allow Guzzle 7
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyholm committed Dec 30, 2019
1 parent 35289d9 commit 1b5f61c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 29 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ matrix:
- php: hhvm
- php: nightly
fast_finish: true
include:
- name: "Guzzle 7 beta"
php: 7.3
before_install:
- composer require "guzzlehttp/guzzle:^7.0@beta"

sudo: false

Expand Down Expand Up @@ -49,6 +54,7 @@ install:
# Resolve dependencies
- composer --version
- travis_retry composer update $COMPOSER_OPTS --no-interaction --prefer-source
- composer show guzzlehttp/guzzle

script:
# Unit tests
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"require": {
"php": ">=5.5",
"guzzlehttp/guzzle": "^5.3.3|^6.2.1",
"guzzlehttp/guzzle": "^5.3.3|^6.2.1|^7.0",
"guzzlehttp/psr7": "^1.4.1",
"guzzlehttp/promises": "~1.0",
"mtdowling/jmespath.php": "~2.2",
Expand Down
48 changes: 38 additions & 10 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,15 @@ function describe_type($input)
*/
function default_http_handler()
{
$version = (string) ClientInterface::VERSION;
if ($version[0] === '5') {
return new \Aws\Handler\GuzzleV5\GuzzleHandler();
$version = guzzle_major_version();
// If Guzzle 6 or 7 installed
if ($version === 6 || $version === 7) {
return new \Aws\Handler\GuzzleV6\GuzzleHandler();
}

if ($version[0] === '6') {
return new \Aws\Handler\GuzzleV6\GuzzleHandler();
// If Guzzle 5 installed
if ($version === 5) {
return new \Aws\Handler\GuzzleV5\GuzzleHandler();
}

throw new \RuntimeException('Unknown Guzzle version: ' . $version);
Expand All @@ -291,18 +293,44 @@ function default_http_handler()
*/
function default_user_agent()
{
$version = (string) ClientInterface::VERSION;
if ($version[0] === '5') {
return \GuzzleHttp\Client::getDefaultUserAgent();
$version = guzzle_major_version();
// If Guzzle 6 or 7 installed
if ($version === 6 || $version === 7) {
return \GuzzleHttp\default_user_agent();
}

if ($version[0] === '6') {
return \GuzzleHttp\default_user_agent();
// If Guzzle 5 installed
if ($version === 5) {
return \GuzzleHttp\Client::getDefaultUserAgent();
}

throw new \RuntimeException('Unknown Guzzle version: ' . $version);
}

/**
* Get a the major version of guzzle that is installed.
*
* @internal This function is internal and should not be used outside aws/aws-sdk-php.
* @return int
* @throws \RuntimeException
*/
function guzzle_major_version()
{
if (defined('\GuzzleHttp\ClientInterface::VERSION')) {
$version = (string) ClientInterface::VERSION;
if ($version[0] === '6') {
return 6;
}
if ($version[0] === '5') {
return 5;
}
} elseif (method_exists('\GuzzleHttp\Client', 'sendRequest')) {
return 7;
}

throw new \RuntimeException('Unable to determine what Guzzle version is installed.');
}

/**
* Serialize a request for a command but do not send it.
*
Expand Down
6 changes: 3 additions & 3 deletions tests/Credentials/EcsCredentialProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ private function getProxyCheckGuzzleClient()
{
$t = (time() + 1000);
$credentials = $this->getCredentialArray('foo', 'baz', null, "@{$t}");
$version = (string) ClientInterface::VERSION;
$version = \Aws\guzzle_major_version();

if ($version[0] === '5') {
if ($version === 5) {
return new \Aws\Handler\GuzzleV5\GuzzleHandler(
new Client([
'handler' => function (
Expand All @@ -134,7 +134,7 @@ private function getProxyCheckGuzzleClient()
);
}

if ($version[0] === '6') {
if ($version === 6 || $version === 7) {
return new \Aws\Handler\GuzzleV6\GuzzleHandler(
new Client([
'handler' => function (
Expand Down
14 changes: 7 additions & 7 deletions tests/Credentials/InstanceProfileProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ private function getCredentialArray(
private function getRequestClass()
{
// Guzzle 5 vs 6 namespace differences
$version = (string) ClientInterface::VERSION;
if ($version[0] === '5') {
$version = \Aws\guzzle_major_version();
if ($version === 5) {
return "\GuzzleHttp\Message\Request";
}
return "\GuzzleHttp\Psr7\Request";
Expand All @@ -61,22 +61,22 @@ private function getRequestClass()
private function getResponseClass()
{
// Guzzle 5 vs 6 namespace differences
$version = (string) ClientInterface::VERSION;
if ($version[0] === '5') {
$version = \Aws\guzzle_major_version();
if ($version === 5) {
return "\GuzzleHttp\Message\Response";
}
return "\GuzzleHttp\Psr7\Response";
}

private function getRequestException()
{
$version = (string) ClientInterface::VERSION;
if ($version[0] === '6') {
$version = \Aws\guzzle_major_version();
if ($version === 6 || $version === 7) {
return new RequestException(
'test',
new Psr7\Request('GET', 'http://www.example.com')
);
} elseif ($version[0] === '5') {
} elseif ($version === 5) {
return new RequestException(
'test',
new \GuzzleHttp\Message\Request('GET', 'http://www.example.com')
Expand Down
16 changes: 8 additions & 8 deletions tests/RetryMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ public function testDeciderRetriesWhenCurlErrorCodeMatches()
$decider = RetryMiddleware::createDefaultDecider();
$command = new Command('foo');
$request = new Request('GET', 'http://www.example.com');
$version = (string) ClientInterface::VERSION;
if ($version[0] === '6') {
$version = \Aws\guzzle_major_version();
if ($version === 6 || $version === 7) {
$previous = new RequestException(
'test',
$request,
null,
null,
['errno' => CURLE_RECV_ERROR]
);
} elseif ($version[0] === '5') {
} elseif ($version === 5) {
$previous = new RequestException(
'cURL error ' . CURLE_RECV_ERROR . ': test',
new \GuzzleHttp\Message\Request('GET', 'http://www.example.com')
Expand All @@ -126,18 +126,18 @@ public function testDeciderRetriesForCustomCurlErrors()
);
$command = new Command('foo');
$request = new Request('GET', 'http://www.example.com');
$version = (string) ClientInterface::VERSION;
$version = \Aws\guzzle_major_version();

// Custom error passed in to decider config should result in a retry
if ($version[0] === '6') {
if ($version === 6 || $version === 7) {
$previous = new RequestException(
'test',
$request,
null,
null,
['errno' => CURLE_BAD_CONTENT_ENCODING]
);
} elseif ($version[0] === '5') {
} elseif ($version === 5) {
$previous = new RequestException(
'cURL error ' . CURLE_BAD_CONTENT_ENCODING . ': test',
new \GuzzleHttp\Message\Request('GET', 'http://www.example.com')
Expand All @@ -152,15 +152,15 @@ public function testDeciderRetriesForCustomCurlErrors()
$this->assertTrue($decider(0, $command, $request, null, $err));

// Error not passed in to decider config should result in no retry
if ($version[0] === '6') {
if ($version === 6 || $version === 7) {
$previous = new RequestException(
'test',
$request,
null,
null,
['errno' => CURLE_ABORTED_BY_CALLBACK]
);
} elseif ($version[0] === '5') {
} elseif ($version === 5) {
$previous = new RequestException(
'cURL error ' . CURLE_ABORTED_BY_CALLBACK . ': test',
new \GuzzleHttp\Message\Request('GET', 'http://www.example.com')
Expand Down

0 comments on commit 1b5f61c

Please sign in to comment.