Skip to content
Draft
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
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
"license": "Apache-2.0",
"require": {
"php": "^8.1",
"google/auth": "^1.37",
"google/auth": "^1.53",
"google/apiclient-services": "~0.350",
"firebase/php-jwt": "^6.0||^7.0",
"monolog/monolog": "^2.9||^3.0",
"guzzlehttp/guzzle": "^7.4.5",
"guzzlehttp/psr7": "^2.6"
"guzzlehttp/guzzle": "^7.8.2||^8.0",
"guzzlehttp/psr7": "^2.6.3||^3.0"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.8",
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ parameters:
level: 5
paths:
- src
excludePaths:
# Legacy, unused Guzzle 6 auth handler retained for backwards
# compatibility only; it relies on APIs removed in Guzzle 8.
- src/AuthHandler/Guzzle6AuthHandler.php
12 changes: 2 additions & 10 deletions src/AuthHandler/AuthHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,9 @@ class AuthHandlerFactory
*/
public static function build($cache = null, array $cacheConfig = [])
{
$guzzleVersion = null;
if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) {
$guzzleVersion = ClientInterface::MAJOR_VERSION;
} elseif (defined('\GuzzleHttp\ClientInterface::VERSION')) {
$guzzleVersion = (int) substr(ClientInterface::VERSION, 0, 1);
}

switch ($guzzleVersion) {
case 6:
return new Guzzle6AuthHandler($cache, $cacheConfig);
switch (ClientInterface::MAJOR_VERSION) {
case 7:
case 8:
return new Guzzle7AuthHandler($cache, $cacheConfig);
default:
throw new Exception('Version not supported');
Expand Down
2 changes: 1 addition & 1 deletion src/AuthHandler/Guzzle6AuthHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Psr\Cache\CacheItemPoolInterface;

/**
* This supports Guzzle 6
* @deprecated Guzzle 6 is no longer supported; use Guzzle7AuthHandler.
*/
class Guzzle6AuthHandler
{
Expand Down
33 changes: 4 additions & 29 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
use Google\Http\REST;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Ring\Client\StreamHandler;
use InvalidArgumentException;
use LogicException;
use Monolog\Handler\StreamHandler as MonologStreamHandler;
Expand Down Expand Up @@ -1239,34 +1238,10 @@ public function setApiFormatV2($value)

protected function createDefaultHttpClient()
{
$guzzleVersion = null;
if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) {
$guzzleVersion = ClientInterface::MAJOR_VERSION;
} elseif (defined('\GuzzleHttp\ClientInterface::VERSION')) {
$guzzleVersion = (int)substr(ClientInterface::VERSION, 0, 1);
}

if (5 === $guzzleVersion) {
$options = [
'base_url' => $this->config['base_path'],
'defaults' => ['exceptions' => false],
];
if ($this->isAppEngine()) {
if (class_exists(StreamHandler::class)) {
// set StreamHandler on AppEngine by default
$options['handler'] = new StreamHandler();
$options['defaults']['verify'] = '/etc/ca-certificates.crt';
}
}
} elseif (6 === $guzzleVersion || 7 === $guzzleVersion) {
// guzzle 6 or 7
$options = [
'base_uri' => $this->config['base_path'],
'http_errors' => false,
];
} else {
throw new LogicException('Could not find supported version of Guzzle.');
}
$options = [
'base_uri' => $this->config['base_path'],
'http_errors' => false,
];

return new GuzzleClient($options);
}
Expand Down
7 changes: 4 additions & 3 deletions src/Http/REST.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ public static function doExecute(ClientInterface $client, RequestInterface $requ
$response = $httpHandler($request);
} catch (RequestException $e) {
// if Guzzle throws an exception, catch it and handle the response
if (!$e->hasResponse()) {
// (on Guzzle 7 the response is on RequestException, but on Guzzle 8
// it is only on its ResponseException subclass)
$response = method_exists($e, 'getResponse') ? $e->getResponse() : null;
if (null === $response) {
throw $e;
}

$response = $e->getResponse();
}

return self::decodeHttpResponse($response, $request, $expectedClass);
Expand Down
7 changes: 4 additions & 3 deletions tests/Google/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ private function checkAuthHandler($http, $className)
$property = $class->getProperty('stack');
$property->setAccessible(true);
$middlewares = $property->getValue($stack);
$middleware = array_pop($middlewares);

if (null === $className) {
// only the default middlewares have been added
$this->assertCount(3, $middlewares);
$defaultStack = (new GuzzleClient())->getConfig('handler');
$this->assertCount(count($property->getValue($defaultStack)), $middlewares);
} else {
$middleware = array_pop($middlewares);
$authClass = sprintf('Google\Auth\Middleware\%sMiddleware', $className);
$this->assertInstanceOf($authClass, $middleware[0]);
}
Expand Down Expand Up @@ -904,7 +905,7 @@ public function testCredentialsOptionWithFetchAuthTokenInterface()
$callable(new Request('GET', '/fake-uri'), ['auth' => 'google_auth']);
});

$httpClient = $this->prophesize('GuzzleHttp\ClientInterface');
$httpClient = $this->prophesize('GuzzleHttp\Client');
$httpClient->getConfig()
->shouldBeCalled()
->willReturn(['handler' => $handler->reveal()]);
Expand Down
34 changes: 34 additions & 0 deletions tests/Google/Http/RESTTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
use Google\Service\Exception as ServiceException;
use Google\Tests\BaseTest;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Prophecy\Argument;

class RESTTest extends BaseTest
{
Expand Down Expand Up @@ -86,6 +89,37 @@ public function testExceptionResponse()
$response = $this->rest->doExecute($http, $request);
}

public function testRequestExceptionWithResponseIsHandled()
{
$this->expectException(ServiceException::class);
$this->expectExceptionCode(400);

$request = new Request('GET', 'http://www.example.com');
$response = new Response(400, [], Psr7\Utils::streamFor('{"error": "bad request"}'));

$http = $this->prophesize('GuzzleHttp\ClientInterface');
$http->send(Argument::type('Psr\Http\Message\RequestInterface'), [])
->shouldBeCalledTimes(1)
->willThrow(new ClientException('Bad Request', $request, $response));

$this->rest->doExecute($http->reveal(), $request);
}

public function testRequestExceptionWithoutResponseIsRethrown()
{
$this->expectException(RequestException::class);
$this->expectExceptionMessage('Request failed');

$request = new Request('GET', 'http://www.example.com');

$http = $this->prophesize('GuzzleHttp\ClientInterface');
$http->send(Argument::type('Psr\Http\Message\RequestInterface'), [])
->shouldBeCalledTimes(1)
->willThrow(new RequestException('Request failed', $request));

$this->rest->doExecute($http->reveal(), $request);
}

public function testDecodeEmptyResponse()
{
$stream = Psr7\Utils::streamFor('{}');
Expand Down
Loading