Skip to content

Commit ea23f87

Browse files
GrahamCampbellbshaffer
authored andcommitted
feat: add support for Guzzle 8
1 parent b7b4aeb commit ea23f87

16 files changed

Lines changed: 72 additions & 34 deletions

File tree

BigQuery/tests/Unit/BigQueryClientTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
use GuzzleHttp\HandlerStack;
4848
use GuzzleHttp\Psr7\Request;
4949
use GuzzleHttp\Psr7\Response;
50-
use GuzzleHttp\Exception\RequestException;
50+
use GuzzleHttp\Exception\ServerException;
5151
use PHPUnit\Framework\TestCase;
5252
use Prophecy\Argument;
5353
use Prophecy\PhpUnit\ProphecyTrait;
@@ -1033,7 +1033,7 @@ public function testRetryLogging()
10331033
$doneJobResponse['status']['state'] = 'DONE';
10341034

10351035
$apiMockHandler = new MockHandler([
1036-
new RequestException(
1036+
new ServerException(
10371037
'Transient error',
10381038
new Request('POST', ''),
10391039
new Response(502)

BigQuery/tests/Unit/Connection/RestTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,14 @@ function ($args) use (&$actualRequest, $response) {
210210

211211
private function getMetadata(Request $request)
212212
{
213-
$lines = explode(PHP_EOL, (string) $request->getBody());
214-
return json_decode($lines[5], true);
213+
// guzzle/psr7 2 generates per-part Content-Length headers, psr7 3
214+
// does not; strip them so the metadata line offset matches on both.
215+
$lines = array_values(array_filter(
216+
explode(PHP_EOL, (string) $request->getBody()),
217+
function ($line) {
218+
return strpos($line, 'Content-Length:') !== 0;
219+
}
220+
));
221+
return json_decode($lines[4], true);
215222
}
216223
}

Core/composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
"require": {
77
"php": "^8.1",
88
"rize/uri-template": "~0.3||~0.4",
9-
"google/auth": "^1.34",
10-
"guzzlehttp/guzzle": "^6.5.8||^7.4.4",
11-
"guzzlehttp/promises": "^1.4||^2.0",
12-
"guzzlehttp/psr7": "^2.6",
9+
"google/auth": "^1.53",
10+
"guzzlehttp/guzzle": "^7.8.2||^8.0",
11+
"guzzlehttp/promises": "^2.0.3||^3.0",
12+
"guzzlehttp/psr7": "^2.6.3||^3.0",
1313
"monolog/monolog": "^2.9||^3.0",
1414
"psr/http-message": "^1.0||^2.0",
1515
"google/gax": "^1.38.0"

Core/src/RequestWrapper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,9 @@ private function convertToGoogleException(\Exception $ex)
453453
*/
454454
private function getExceptionMessage(\Exception $ex)
455455
{
456-
if ($ex instanceof RequestException && $ex->hasResponse()) {
456+
// Guzzle 7 carries the response on RequestException, Guzzle 8 only on
457+
// its ResponseException subclass, hence the method_exists() check.
458+
if ($ex instanceof RequestException && method_exists($ex, 'getResponse') && $ex->getResponse()) {
457459
return (string) $ex->getResponse()->getBody();
458460
}
459461

Core/src/RetryDeciderTrait.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,13 @@ private function getRetryFunction($shouldRetryMessages = true)
6666
return false;
6767
}
6868

69-
$message = ($ex instanceof RequestException && $ex->hasResponse())
70-
? (string) $ex->getResponse()->getBody()
69+
// Guzzle 7 carries the response on RequestException, Guzzle 8 only
70+
// on its ResponseException subclass, hence the method_exists() check.
71+
$response = $ex instanceof RequestException && method_exists($ex, 'getResponse')
72+
? $ex->getResponse()
73+
: null;
74+
$message = $response
75+
? (string) $response->getBody()
7176
: $ex->getMessage();
7277

7378
try {

Core/tests/Unit/RequestWrapperTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
use Google\Cloud\Core\Exception\ServerException;
3232
use Google\Cloud\Core\Exception\ServiceException;
3333
use Google\Cloud\Core\RequestWrapper;
34-
use GuzzleHttp\Exception\RequestException;
34+
use GuzzleHttp\Exception\ClientException;
3535
use GuzzleHttp\Promise\Create;
3636
use GuzzleHttp\Promise\PromiseInterface;
3737
use GuzzleHttp\Psr7\Request;
@@ -335,7 +335,7 @@ public function testExceptionMessageIsNotTruncatedWithGuzzle()
335335
$msg = str_repeat('0', 121);
336336
$jsonMsg = '{"msg":"' . $msg . '"}';
337337

338-
throw new RequestException(
338+
throw new ClientException(
339339
$jsonMsg,
340340
$request,
341341
new Response(400, [], $jsonMsg)

Gax/composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
"license": "BSD-3-Clause",
1010
"require": {
1111
"php": "^8.1",
12-
"google/auth": "^1.52",
12+
"google/auth": "^1.53",
1313
"google/grpc-gcp": "^0.4",
1414
"grpc/grpc": "^1.13",
1515
"google/protobuf": "^4.31||^5.34",
16-
"guzzlehttp/promises": "^2.0",
17-
"guzzlehttp/psr7": "^2.0",
16+
"guzzlehttp/promises": "^2.0.3||^3.0",
17+
"guzzlehttp/psr7": "^2.6.3||^3.0",
1818
"google/common-protos": "^4.9",
1919
"google/longrunning": "~0.4",
2020
"ramsey/uuid": "^4.0"

Gax/src/ApiException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,9 @@ public static function createFromRpcStatus(Status $status)
354354
*/
355355
public static function createFromRequestException(RequestException $ex, bool $isStream = false)
356356
{
357-
$res = $ex->getResponse();
357+
// Guzzle 7 carries the response on RequestException, Guzzle 8 only on
358+
// its ResponseException subclass, hence the method_exists() check.
359+
$res = method_exists($ex, 'getResponse') ? $ex->getResponse() : null;
358360
$body = (string) $res->getBody();
359361
$decoded = json_decode($body, true);
360362

Gax/src/Transport/GrpcFallbackTransport.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ private function getCallOptions(array $options)
203203
*/
204204
private function transformException(\Exception $ex)
205205
{
206-
if ($ex instanceof RequestException && $ex->hasResponse()) {
206+
// Guzzle 7 carries the response on RequestException, Guzzle 8 only on
207+
// its ResponseException subclass, hence the method_exists() check.
208+
if ($ex instanceof RequestException && method_exists($ex, 'getResponse') && $ex->getResponse()) {
207209
$res = $ex->getResponse();
208210
$body = (string) $res->getBody();
209211
$status = new Status();

Gax/src/Transport/Rest/RestServerStreamingCall.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ public function start($request, array $headers = [], array $callOptions = [])
8686
$callOptions
8787
)->wait();
8888
} catch (\Exception $ex) {
89-
if ($ex instanceof RequestException && $ex->hasResponse()) {
89+
// Guzzle 7 carries the response on RequestException, Guzzle 8 only
90+
// on its ResponseException subclass, hence the method_exists() check.
91+
if ($ex instanceof RequestException && method_exists($ex, 'getResponse') && $ex->getResponse()) {
9092
$ex = ApiException::createFromRequestException($ex, /* isStream */ true);
9193
}
9294
throw $ex;

0 commit comments

Comments
 (0)