Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Guzzle 7 #1940

Merged
merged 3 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changes/nextrelease/guzzle7.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"type": "feature",
"category": "",
"description": "Add support for Guzzle7."
}
]
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ matrix:
- php: hhvm
- php: nightly
fast_finish: true
include:
- name: "PHP: 7.3 - Guzzle 7 beta"
php: 7.3
env: COMPOSER_OPTS="--no-interaction"
before_install:
- composer require "guzzlehttp/guzzle:^7.0@beta"

sudo: false

Expand Down Expand Up @@ -49,6 +55,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
54 changes: 44 additions & 10 deletions src/functions.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Aws;

use GuzzleHttp\Client;
use Psr\Http\Message\RequestInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Promise\FulfilledPromise;
Expand Down Expand Up @@ -272,13 +273,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 +294,49 @@ 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 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()
{
Nyholm marked this conversation as resolved.
Show resolved Hide resolved
static $cache = null;
if (null !== $cache) {
return $cache;
}

if (defined('\GuzzleHttp\ClientInterface::VERSION')) {
$version = (string) ClientInterface::VERSION;
if ($version[0] === '6') {
return $cache = 6;
}
if ($version[0] === '5') {
return $cache = 5;
}
} elseif (method_exists(Client::class, 'sendRequest')) {
return $cache = 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