From 23a21370bd0fedbc778bf516856e35279abd5380 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 17 May 2024 12:12:31 -0700 Subject: [PATCH 01/12] feat: add support for new surface custom operations clients --- src/GapicClientTrait.php | 2 +- src/OperationResponse.php | 74 ++++++++++++++----- tests/Tests/Unit/OperationResponseTest.php | 70 ++++++++++++++++++ .../testdata/src/CustomOperationClient.php | 52 +++++++++++++ 4 files changed, 180 insertions(+), 18 deletions(-) create mode 100644 tests/Tests/Unit/testdata/src/CustomOperationClient.php diff --git a/src/GapicClientTrait.php b/src/GapicClientTrait.php index 14a13ccd3..195b06f6d 100644 --- a/src/GapicClientTrait.php +++ b/src/GapicClientTrait.php @@ -629,7 +629,7 @@ private function createCallStack(array $callConstructionOptions) 'X-Goog-User-Project' => [$quotaProject] ]; } - + if (isset($this->apiVersion)) { $fixedHeaders += [ 'X-Goog-Api-Version' => [$this->apiVersion] diff --git a/src/OperationResponse.php b/src/OperationResponse.php index 38ed73e1d..7335d9f71 100644 --- a/src/OperationResponse.php +++ b/src/OperationResponse.php @@ -33,6 +33,7 @@ namespace Google\ApiCore; use Google\LongRunning\Client\OperationsClient; +use Google\LongRunning\OperationsClient as LegacyOperationsClient; use Google\LongRunning\CancelOperationRequest; use Google\LongRunning\DeleteOperationRequest; use Google\LongRunning\GetOperationRequest; @@ -263,10 +264,18 @@ public function reload() if ($this->deleted) { throw new ValidationException("Cannot call reload() on a deleted operation"); } - $this->lastProtoResponse = $this->operationsCall( - $this->getOperationMethod, - GetOperationRequest::class - ); + + $requestClass = null; + if ($this->operationsClient instanceof OperationsClient) { + $requestClass = GetOperationRequest::class; + } elseif ($this->isNewSurfaceOperationsClient()) { + $requestClass = (new \ReflectionMethod( + $this->operationsClient, + $this->getOperationMethod) + )->getParameters()[0]->getType()->getName(); + } + + $this->lastProtoResponse = $this->operationsCall($this->getOperationMethod, $requestClass); } /** @@ -389,10 +398,18 @@ public function cancel() if (is_null($this->cancelOperationMethod)) { throw new LogicException('The cancel operation is not supported by this API'); } - $this->operationsCall( - $this->cancelOperationMethod, - CancelOperationRequest::class - ); + + $requestClass = null; + if ($this->operationsClient instanceof OperationsClient) { + $requestClass = CancelOperationRequest::class; + } elseif ($this->isNewSurfaceOperationsClient()) { + $requestClass = (new \ReflectionMethod( + $this->operationsClient, + $this->cancelOperationMethod) + )->getParameters()[0]->getType()->getName(); + } + + $this->operationsCall($this->cancelOperationMethod, $requestClass); } /** @@ -411,10 +428,18 @@ public function delete() if (is_null($this->deleteOperationMethod)) { throw new LogicException('The delete operation is not supported by this API'); } - $this->operationsCall( - $this->deleteOperationMethod, - DeleteOperationRequest::class - ); + + $requestClass = null; + if ($this->operationsClient instanceof OperationsClient) { + $requestClass = DeleteOperationRequest::class; + } elseif ($this->isNewSurfaceOperationsClient()) { + $requestClass = (new \ReflectionMethod( + $this->operationsClient, + $this->deleteOperationMethod) + )->getParameters()[0]->getType()->getName(); + } + + $this->operationsCall($this->deleteOperationMethod, $requestClass); $this->deleted = true; } @@ -456,12 +481,21 @@ public function getMetadata() return $metadata; } - private function operationsCall(string $method, string $requestClass) + /** + * Call the operations client to perform an operation. + * + * @param string $method The method to call on the operations client. + * @param string|null $requestClass The request class to use for the call. + * Will be null for legacy operations clients. + */ + private function operationsCall(string $method, ?string $requestClass) { - $firstArgument = $this->operationsClient instanceof OperationsClient - ? $requestClass::build($this->getName()) - : $this->getName(); - $args = array_merge([$firstArgument], $this->additionalArgs); + $args = array_merge([$this->getName()], array_values($this->additionalArgs)); + if ($requestClass) { + $request = call_user_func_array($requestClass . '::build', $args); + $args = [$request]; + } + return call_user_func_array([$this->operationsClient, $method], $args); } @@ -495,4 +529,10 @@ private function hasProtoResponse() { return !is_null($this->lastProtoResponse); } + + private function isNewSurfaceOperationsClient(): bool + { + return !$this->operationsClient instanceof LegacyOperationsClient + && false !== strpos(get_class($this->operationsClient), '\\Client\\'); + } } diff --git a/tests/Tests/Unit/OperationResponseTest.php b/tests/Tests/Unit/OperationResponseTest.php index db1fb09b0..b2a06249f 100644 --- a/tests/Tests/Unit/OperationResponseTest.php +++ b/tests/Tests/Unit/OperationResponseTest.php @@ -259,6 +259,76 @@ public function testCustomOperation() $operationResponse->delete(); } + public function testNewSurfaceCustomOperation() + { + // This mock requires a specific namespace, so it must be defined in a separate file + require_once __DIR__ . '/testdata/src/CustomOperationClient.php'; + + $phpunit = $this; + $operationName = 'test-123'; + $operation = $this->prophesize(CustomOperation::class); + $operation->isThisOperationDoneOrWhat() + ->shouldBeCalledTimes(2) + ->willReturn('Yes, it is!'); + $operation->getError() + ->shouldBeCalledOnce() + ->willReturn(null); + $operationClient = $this->prophesize(Client\NewSurfaceCustomOperationClient::class); + $operationClient->getNewSurfaceOperation(Argument::type(Client\GetOperationRequest::class)) + ->shouldBeCalledOnce() + ->will(function ($args) use ($operation, $phpunit) { + list($request) = $args; + $phpunit->assertEquals('test-123', $request->name); + $phpunit->assertEquals('arg2', $request->arg2); + $phpunit->assertEquals('arg3', $request->arg3); + return $operation->reveal(); + }); + $operationClient->cancelNewSurfaceOperation(Argument::type(Client\CancelOperationRequest::class)) + ->shouldBeCalledOnce() + ->will(function ($args) use ($operation, $phpunit) { + list($request) = $args; + $phpunit->assertEquals('test-123', $request->name); + $phpunit->assertEquals('arg2', $request->arg2); + $phpunit->assertEquals('arg3', $request->arg3); + return true; + }); + $operationClient->deleteNewSurfaceOperation(Argument::type(Client\DeleteOperationRequest::class)) + ->shouldBeCalledOnce() + ->will(function ($args) use ($operation, $phpunit) { + list($request) = $args; + $phpunit->assertEquals('test-123', $request->name); + $phpunit->assertEquals('arg2', $request->arg2); + $phpunit->assertEquals('arg3', $request->arg3); + return true; + }); + $options = [ + 'getOperationMethod' => 'getNewSurfaceOperation', + 'cancelOperationMethod' => 'cancelNewSurfaceOperation', + 'deleteOperationMethod' => 'deleteNewSurfaceOperation', + 'additionalOperationArguments' => [ + 'setArgumentTwo' => 'arg2', + 'setArgumentThree' => 'arg3' + ], + 'operationStatusMethod' => 'isThisOperationDoneOrWhat', + 'operationStatusDoneValue' => 'Yes, it is!', + ]; + $operationResponse = new OperationResponse($operationName, $operationClient->reveal(), $options); + + // Test getOperationMethod + $operationResponse->reload(); + + // Test operationStatusMethod and operationStatusDoneValue + $this->assertTrue($operationResponse->isDone()); + + $this->assertTrue($operationResponse->operationSucceeded()); + + // test cancelOperationMethod + $operationResponse->cancel(); + + // test deleteOperationMethod + $operationResponse->delete(); + } + /** * @dataProvider provideOperationsClients */ diff --git a/tests/Tests/Unit/testdata/src/CustomOperationClient.php b/tests/Tests/Unit/testdata/src/CustomOperationClient.php new file mode 100644 index 000000000..75b412bc1 --- /dev/null +++ b/tests/Tests/Unit/testdata/src/CustomOperationClient.php @@ -0,0 +1,52 @@ +name = $name; + $request->arg2 = $arg2; + $request->arg3 = $arg3; + + return $request; + } +} + +class GetOperationRequest extends BaseOperationRequest +{ +} + +class CancelOperationRequest extends BaseOperationRequest +{ +} + +class DeleteOperationRequest extends BaseOperationRequest +{ +} + From fccc085a73786572978a4464759c66f623c53ef6 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 17 May 2024 15:14:06 -0700 Subject: [PATCH 02/12] fix cs --- src/OperationResponse.php | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/OperationResponse.php b/src/OperationResponse.php index 7335d9f71..dc523e020 100644 --- a/src/OperationResponse.php +++ b/src/OperationResponse.php @@ -269,10 +269,7 @@ public function reload() if ($this->operationsClient instanceof OperationsClient) { $requestClass = GetOperationRequest::class; } elseif ($this->isNewSurfaceOperationsClient()) { - $requestClass = (new \ReflectionMethod( - $this->operationsClient, - $this->getOperationMethod) - )->getParameters()[0]->getType()->getName(); + $requestClass = $this->getRequestClass($this->getOperationMethod); } $this->lastProtoResponse = $this->operationsCall($this->getOperationMethod, $requestClass); @@ -403,10 +400,7 @@ public function cancel() if ($this->operationsClient instanceof OperationsClient) { $requestClass = CancelOperationRequest::class; } elseif ($this->isNewSurfaceOperationsClient()) { - $requestClass = (new \ReflectionMethod( - $this->operationsClient, - $this->cancelOperationMethod) - )->getParameters()[0]->getType()->getName(); + $requestClass = $this->getRequestClass($this->cancelOperationMethod); } $this->operationsCall($this->cancelOperationMethod, $requestClass); @@ -433,10 +427,7 @@ public function delete() if ($this->operationsClient instanceof OperationsClient) { $requestClass = DeleteOperationRequest::class; } elseif ($this->isNewSurfaceOperationsClient()) { - $requestClass = (new \ReflectionMethod( - $this->operationsClient, - $this->deleteOperationMethod) - )->getParameters()[0]->getType()->getName(); + $requestClass = $this->getRequestClass($this->deleteOperationMethod); } $this->operationsCall($this->deleteOperationMethod, $requestClass); @@ -535,4 +526,16 @@ private function isNewSurfaceOperationsClient(): bool return !$this->operationsClient instanceof LegacyOperationsClient && false !== strpos(get_class($this->operationsClient), '\\Client\\'); } + + private function getRequestClass(string $method): string + { + $refl = new \ReflectionMethod($this->operationsClient, $method); + $type = $refl->getParameters()[0]->getType(); + + if (!$type instanceof \ReflectionNamedType) { + throw new \RuntimeException('Could not determine request class'); + } + + return $type->getName(); + } } From 6d7cb6c2500dedfb5f7584ef64e17f46fc07d889 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 17 May 2024 15:29:46 -0700 Subject: [PATCH 03/12] simplify tests --- tests/Tests/Unit/OperationResponseTest.php | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/tests/Tests/Unit/OperationResponseTest.php b/tests/Tests/Unit/OperationResponseTest.php index b2a06249f..104e540a3 100644 --- a/tests/Tests/Unit/OperationResponseTest.php +++ b/tests/Tests/Unit/OperationResponseTest.php @@ -266,26 +266,19 @@ public function testNewSurfaceCustomOperation() $phpunit = $this; $operationName = 'test-123'; - $operation = $this->prophesize(CustomOperation::class); - $operation->isThisOperationDoneOrWhat() - ->shouldBeCalledTimes(2) - ->willReturn('Yes, it is!'); - $operation->getError() - ->shouldBeCalledOnce() - ->willReturn(null); $operationClient = $this->prophesize(Client\NewSurfaceCustomOperationClient::class); $operationClient->getNewSurfaceOperation(Argument::type(Client\GetOperationRequest::class)) ->shouldBeCalledOnce() - ->will(function ($args) use ($operation, $phpunit) { + ->will(function ($args) use ($phpunit) { list($request) = $args; $phpunit->assertEquals('test-123', $request->name); $phpunit->assertEquals('arg2', $request->arg2); $phpunit->assertEquals('arg3', $request->arg3); - return $operation->reveal(); + return $phpunit->prophesize(Operation::class)->reveal(); }); $operationClient->cancelNewSurfaceOperation(Argument::type(Client\CancelOperationRequest::class)) ->shouldBeCalledOnce() - ->will(function ($args) use ($operation, $phpunit) { + ->will(function ($args) use ($phpunit) { list($request) = $args; $phpunit->assertEquals('test-123', $request->name); $phpunit->assertEquals('arg2', $request->arg2); @@ -294,7 +287,7 @@ public function testNewSurfaceCustomOperation() }); $operationClient->deleteNewSurfaceOperation(Argument::type(Client\DeleteOperationRequest::class)) ->shouldBeCalledOnce() - ->will(function ($args) use ($operation, $phpunit) { + ->will(function ($args) use ($phpunit) { list($request) = $args; $phpunit->assertEquals('test-123', $request->name); $phpunit->assertEquals('arg2', $request->arg2); @@ -309,19 +302,12 @@ public function testNewSurfaceCustomOperation() 'setArgumentTwo' => 'arg2', 'setArgumentThree' => 'arg3' ], - 'operationStatusMethod' => 'isThisOperationDoneOrWhat', - 'operationStatusDoneValue' => 'Yes, it is!', ]; $operationResponse = new OperationResponse($operationName, $operationClient->reveal(), $options); // Test getOperationMethod $operationResponse->reload(); - // Test operationStatusMethod and operationStatusDoneValue - $this->assertTrue($operationResponse->isDone()); - - $this->assertTrue($operationResponse->operationSucceeded()); - // test cancelOperationMethod $operationResponse->cancel(); From 3c3e1474ea03804a5d8256fe3f0e611d1f46d46d Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 20 May 2024 14:42:40 -0700 Subject: [PATCH 04/12] add Request class configuration for custom operations --- src/OperationResponse.php | 33 ++++++++-------------- tests/Tests/Unit/OperationResponseTest.php | 3 ++ 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/OperationResponse.php b/src/OperationResponse.php index dc523e020..3f190a038 100644 --- a/src/OperationResponse.php +++ b/src/OperationResponse.php @@ -85,6 +85,9 @@ class OperationResponse private string $getOperationMethod; private ?string $cancelOperationMethod; private ?string $deleteOperationMethod; + private string $getOperationRequest; + private ?string $cancelOperationRequest; + private ?string $deleteOperationRequest; private string $operationStatusMethod; /** @var mixed */ private $operationStatusDoneValue; @@ -132,6 +135,9 @@ public function __construct(string $operationName, $operationsClient, array $opt 'additionalOperationArguments' => [], 'operationErrorCodeMethod' => null, 'operationErrorMessageMethod' => null, + 'getOperationRequest' => GetOperationRequest::class, + 'cancelOperationRequest' => CancelOperationRequest::class, + 'deleteOperationRequest' => DeleteOperationRequest::class, ]; $this->operationReturnType = $options['operationReturnType']; $this->metadataReturnType = $options['metadataReturnType']; @@ -144,6 +150,9 @@ public function __construct(string $operationName, $operationsClient, array $opt $this->operationStatusDoneValue = $options['operationStatusDoneValue']; $this->operationErrorCodeMethod = $options['operationErrorCodeMethod']; $this->operationErrorMessageMethod = $options['operationErrorMessageMethod']; + $this->getOperationRequest = $options['getOperationRequest']; + $this->cancelOperationRequest = $options['cancelOperationRequest']; + $this->deleteOperationRequest = $options['deleteOperationRequest']; if (isset($options['initialPollDelayMillis'])) { $this->defaultPollSettings['initialPollDelayMillis'] = $options['initialPollDelayMillis']; @@ -265,13 +274,7 @@ public function reload() throw new ValidationException("Cannot call reload() on a deleted operation"); } - $requestClass = null; - if ($this->operationsClient instanceof OperationsClient) { - $requestClass = GetOperationRequest::class; - } elseif ($this->isNewSurfaceOperationsClient()) { - $requestClass = $this->getRequestClass($this->getOperationMethod); - } - + $requestClass = $this->isNewSurfaceOperationsClient() ? $this->getOperationRequest : null; $this->lastProtoResponse = $this->operationsCall($this->getOperationMethod, $requestClass); } @@ -396,13 +399,7 @@ public function cancel() throw new LogicException('The cancel operation is not supported by this API'); } - $requestClass = null; - if ($this->operationsClient instanceof OperationsClient) { - $requestClass = CancelOperationRequest::class; - } elseif ($this->isNewSurfaceOperationsClient()) { - $requestClass = $this->getRequestClass($this->cancelOperationMethod); - } - + $requestClass = $this->isNewSurfaceOperationsClient() ? $this->cancelOperationRequest : null; $this->operationsCall($this->cancelOperationMethod, $requestClass); } @@ -423,13 +420,7 @@ public function delete() throw new LogicException('The delete operation is not supported by this API'); } - $requestClass = null; - if ($this->operationsClient instanceof OperationsClient) { - $requestClass = DeleteOperationRequest::class; - } elseif ($this->isNewSurfaceOperationsClient()) { - $requestClass = $this->getRequestClass($this->deleteOperationMethod); - } - + $requestClass = $this->isNewSurfaceOperationsClient() ? $this->deleteOperationRequest : null; $this->operationsCall($this->deleteOperationMethod, $requestClass); $this->deleted = true; } diff --git a/tests/Tests/Unit/OperationResponseTest.php b/tests/Tests/Unit/OperationResponseTest.php index 104e540a3..527c2aab9 100644 --- a/tests/Tests/Unit/OperationResponseTest.php +++ b/tests/Tests/Unit/OperationResponseTest.php @@ -302,6 +302,9 @@ public function testNewSurfaceCustomOperation() 'setArgumentTwo' => 'arg2', 'setArgumentThree' => 'arg3' ], + 'getOperationRequest' => Client\GetOperationRequest::class, + 'cancelOperationRequest' => Client\CancelOperationRequest::class, + 'deleteOperationRequest' => Client\DeleteOperationRequest::class, ]; $operationResponse = new OperationResponse($operationName, $operationClient->reveal(), $options); From a1cb6d61d2b86a3735e5237453390c246194aad5 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 20 May 2024 14:49:45 -0700 Subject: [PATCH 05/12] remove unused method getRequestClass --- src/OperationResponse.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/OperationResponse.php b/src/OperationResponse.php index 3f190a038..fd8ffc504 100644 --- a/src/OperationResponse.php +++ b/src/OperationResponse.php @@ -517,16 +517,4 @@ private function isNewSurfaceOperationsClient(): bool return !$this->operationsClient instanceof LegacyOperationsClient && false !== strpos(get_class($this->operationsClient), '\\Client\\'); } - - private function getRequestClass(string $method): string - { - $refl = new \ReflectionMethod($this->operationsClient, $method); - $type = $refl->getParameters()[0]->getType(); - - if (!$type instanceof \ReflectionNamedType) { - throw new \RuntimeException('Could not determine request class'); - } - - return $type->getName(); - } } From bdc36a3995d11c35eac429764e4e938ad9457f18 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 20 May 2024 14:58:27 -0700 Subject: [PATCH 06/12] fix segfault --- tests/Tests/Unit/OperationResponseTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Tests/Unit/OperationResponseTest.php b/tests/Tests/Unit/OperationResponseTest.php index 527c2aab9..8d755ffb6 100644 --- a/tests/Tests/Unit/OperationResponseTest.php +++ b/tests/Tests/Unit/OperationResponseTest.php @@ -274,7 +274,7 @@ public function testNewSurfaceCustomOperation() $phpunit->assertEquals('test-123', $request->name); $phpunit->assertEquals('arg2', $request->arg2); $phpunit->assertEquals('arg3', $request->arg3); - return $phpunit->prophesize(Operation::class)->reveal(); + return new \stdClass; }); $operationClient->cancelNewSurfaceOperation(Argument::type(Client\CancelOperationRequest::class)) ->shouldBeCalledOnce() From 66e4a77ede62b5f00a03ba27febe8ad07272a55c Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 20 May 2024 15:33:47 -0700 Subject: [PATCH 07/12] add validation for request build method --- src/OperationResponse.php | 3 +++ tests/Tests/Unit/OperationResponseTest.php | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/OperationResponse.php b/src/OperationResponse.php index fd8ffc504..8d92af07e 100644 --- a/src/OperationResponse.php +++ b/src/OperationResponse.php @@ -474,6 +474,9 @@ private function operationsCall(string $method, ?string $requestClass) { $args = array_merge([$this->getName()], array_values($this->additionalArgs)); if ($requestClass) { + if (!method_exists($requestClass, 'build')) { + throw new LogicException('Request class must support the static build method'); + } $request = call_user_func_array($requestClass . '::build', $args); $args = [$request]; } diff --git a/tests/Tests/Unit/OperationResponseTest.php b/tests/Tests/Unit/OperationResponseTest.php index 8d755ffb6..c914b3fce 100644 --- a/tests/Tests/Unit/OperationResponseTest.php +++ b/tests/Tests/Unit/OperationResponseTest.php @@ -318,6 +318,24 @@ public function testNewSurfaceCustomOperation() $operationResponse->delete(); } + public function testRequestClassWithoutBuildThrowsException() + { + $this->expectException(LogicException::class); + $this->expectExceptionMessage('Request class must support the static build method'); + + // This mock requires a specific namespace, so it must be defined in a separate file + require_once __DIR__ . '/testdata/src/CustomOperationClient.php'; + + $operationClient = $this->prophesize(Client\NewSurfaceCustomOperationClient::class); + $options = [ + 'getOperationRequest' => \stdClass::class, // a class that does not have a "build" method. + ]; + $operationResponse = new OperationResponse('test-123', $operationClient->reveal(), $options); + + // Test getOperationMethod + $operationResponse->reload(); + } + /** * @dataProvider provideOperationsClients */ From 286cc6d161858c7d898ca89b40cdee857f186ca4 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 21 May 2024 11:56:56 -0700 Subject: [PATCH 08/12] feat: add support for new surface operations client --- src/GapicClientTrait.php | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/GapicClientTrait.php b/src/GapicClientTrait.php index 195b06f6d..adb66bfb6 100644 --- a/src/GapicClientTrait.php +++ b/src/GapicClientTrait.php @@ -32,7 +32,7 @@ namespace Google\ApiCore; -use Google\ApiCore\LongRunning\OperationsClient; +use Google\ApiCore\LongRunning\OperationsClient as LegacyOperationsClient; use Google\ApiCore\Middleware\CredentialsWrapperMiddleware; use Google\ApiCore\Middleware\FixedHeaderMiddleware; use Google\ApiCore\Middleware\OperationsMiddleware; @@ -48,6 +48,7 @@ use Google\ApiCore\Transport\RestTransport; use Google\ApiCore\Transport\TransportInterface; use Google\Auth\FetchAuthTokenInterface; +use Google\LongRunning\Client\OperationsClient; use Google\LongRunning\Operation; use Google\Protobuf\Internal\Message; use GuzzleHttp\Promise\PromiseInterface; @@ -370,7 +371,7 @@ private function createTransport( /** * @param array $options - * @return OperationsClient + * @return LegacyOperationsClient */ private function createOperationsClient(array $options) { @@ -387,7 +388,32 @@ private function createOperationsClient(array $options) // operationsClientClass option $operationsClientClass = $this->pluck('operationsClientClass', $options, false) - ?: OperationsCLient::class; + ?: LegacyOperationsClient::class; + + return new $operationsClientClass($options); + } + + /** + * @param array $options + * @return OperationsClient + */ + private function createNewSurfaceOperationsClient(array $options) + { + $this->pluckArray([ + 'serviceName', + 'clientConfig', + 'descriptorsConfigPath', + ], $options); + + // User-supplied operations client + if ($operationsClient = $this->pluck('operationsClient', $options, false)) { + return $operationsClient; + } + + // operationsClientClass option + $operationsClientClass = $this->pluck('operationsClientClass', $options, false) + ?: OperationsClient::class; + return new $operationsClientClass($options); } From 0e153a3fdb6b28ca9370bd0f3775ddb616c4e40a Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 21 May 2024 11:58:45 -0700 Subject: [PATCH 09/12] Revert "feat: add support for new surface operations client" This reverts commit 286cc6d161858c7d898ca89b40cdee857f186ca4. --- src/GapicClientTrait.php | 32 +++----------------------------- 1 file changed, 3 insertions(+), 29 deletions(-) diff --git a/src/GapicClientTrait.php b/src/GapicClientTrait.php index adb66bfb6..195b06f6d 100644 --- a/src/GapicClientTrait.php +++ b/src/GapicClientTrait.php @@ -32,7 +32,7 @@ namespace Google\ApiCore; -use Google\ApiCore\LongRunning\OperationsClient as LegacyOperationsClient; +use Google\ApiCore\LongRunning\OperationsClient; use Google\ApiCore\Middleware\CredentialsWrapperMiddleware; use Google\ApiCore\Middleware\FixedHeaderMiddleware; use Google\ApiCore\Middleware\OperationsMiddleware; @@ -48,7 +48,6 @@ use Google\ApiCore\Transport\RestTransport; use Google\ApiCore\Transport\TransportInterface; use Google\Auth\FetchAuthTokenInterface; -use Google\LongRunning\Client\OperationsClient; use Google\LongRunning\Operation; use Google\Protobuf\Internal\Message; use GuzzleHttp\Promise\PromiseInterface; @@ -369,35 +368,11 @@ private function createTransport( } } - /** - * @param array $options - * @return LegacyOperationsClient - */ - private function createOperationsClient(array $options) - { - $this->pluckArray([ - 'serviceName', - 'clientConfig', - 'descriptorsConfigPath', - ], $options); - - // User-supplied operations client - if ($operationsClient = $this->pluck('operationsClient', $options, false)) { - return $operationsClient; - } - - // operationsClientClass option - $operationsClientClass = $this->pluck('operationsClientClass', $options, false) - ?: LegacyOperationsClient::class; - - return new $operationsClientClass($options); - } - /** * @param array $options * @return OperationsClient */ - private function createNewSurfaceOperationsClient(array $options) + private function createOperationsClient(array $options) { $this->pluckArray([ 'serviceName', @@ -412,8 +387,7 @@ private function createNewSurfaceOperationsClient(array $options) // operationsClientClass option $operationsClientClass = $this->pluck('operationsClientClass', $options, false) - ?: OperationsClient::class; - + ?: OperationsCLient::class; return new $operationsClientClass($options); } From a8aa8616d01128347e5d0c40065650a24ca21c46 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 29 May 2024 14:21:07 -0700 Subject: [PATCH 10/12] Update src/OperationResponse.php Co-authored-by: Noah Dietz --- src/OperationResponse.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OperationResponse.php b/src/OperationResponse.php index 8d92af07e..c3a83fad3 100644 --- a/src/OperationResponse.php +++ b/src/OperationResponse.php @@ -518,6 +518,6 @@ private function hasProtoResponse() private function isNewSurfaceOperationsClient(): bool { return !$this->operationsClient instanceof LegacyOperationsClient - && false !== strpos(get_class($this->operationsClient), '\\Client\\'); + && true === strpos(get_class($this->operationsClient), '\\Client\\'); } } From aa464f960aa73f5903aa8bcba3ed7077f2d29be3 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 29 May 2024 14:25:17 -0700 Subject: [PATCH 11/12] use const for client namespace --- src/OperationResponse.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/OperationResponse.php b/src/OperationResponse.php index c3a83fad3..e73e30b85 100644 --- a/src/OperationResponse.php +++ b/src/OperationResponse.php @@ -65,6 +65,7 @@ class OperationResponse const DEFAULT_POLLING_MULTIPLIER = 2; const DEFAULT_MAX_POLLING_INTERVAL = 60000; const DEFAULT_MAX_POLLING_DURATION = 0; + private const NEW_CLIENT_NAMESPACE = '\\Client\\'; private string $operationName; private ?object $operationsClient; @@ -518,6 +519,6 @@ private function hasProtoResponse() private function isNewSurfaceOperationsClient(): bool { return !$this->operationsClient instanceof LegacyOperationsClient - && true === strpos(get_class($this->operationsClient), '\\Client\\'); + && true === strpos(get_class($this->operationsClient), self::NEW_CLIENT_NAMESPACE); } } From 5bfe8ac8148f92bac901e9dd8086217b7985f4a2 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 29 May 2024 14:29:58 -0700 Subject: [PATCH 12/12] revert suggestion --- src/OperationResponse.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OperationResponse.php b/src/OperationResponse.php index e73e30b85..436d97834 100644 --- a/src/OperationResponse.php +++ b/src/OperationResponse.php @@ -519,6 +519,6 @@ private function hasProtoResponse() private function isNewSurfaceOperationsClient(): bool { return !$this->operationsClient instanceof LegacyOperationsClient - && true === strpos(get_class($this->operationsClient), self::NEW_CLIENT_NAMESPACE); + && false !== strpos(get_class($this->operationsClient), self::NEW_CLIENT_NAMESPACE); } }