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

feat: add withResultFunction to OperationResponse #573

Closed
wants to merge 2 commits into from
Closed
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
32 changes: 32 additions & 0 deletions src/OperationResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

namespace Google\ApiCore;

use Closure;
use Google\LongRunning\Client\OperationsClient;
use Google\LongRunning\OperationsClient as LegacyOperationsClient;
use Google\LongRunning\CancelOperationRequest;
Expand Down Expand Up @@ -94,6 +95,7 @@ class OperationResponse
private $operationStatusDoneValue;
private ?string $operationErrorCodeMethod;
private ?string $operationErrorMessageMethod;
private ?Closure $resultFunction;

/**
* OperationResponse constructor.
Expand Down Expand Up @@ -139,6 +141,7 @@ public function __construct(string $operationName, $operationsClient, array $opt
'getOperationRequest' => GetOperationRequest::class,
'cancelOperationRequest' => CancelOperationRequest::class,
'deleteOperationRequest' => DeleteOperationRequest::class,
'resultFunction' => null,
];
$this->operationReturnType = $options['operationReturnType'];
$this->metadataReturnType = $options['metadataReturnType'];
Expand All @@ -154,6 +157,7 @@ public function __construct(string $operationName, $operationsClient, array $opt
$this->getOperationRequest = $options['getOperationRequest'];
$this->cancelOperationRequest = $options['cancelOperationRequest'];
$this->deleteOperationRequest = $options['deleteOperationRequest'];
$this->resultFunction = $options['resultFunction'];

if (isset($options['initialPollDelayMillis'])) {
$this->defaultPollSettings['initialPollDelayMillis'] = $options['initialPollDelayMillis'];
Expand Down Expand Up @@ -310,6 +314,11 @@ public function getResult()
/** @var Message $response */
$response = new $operationReturnType();
$response->mergeFromString($anyResponse->getValue());

if ($this->resultFunction) {
return ($this->resultFunction)($response);
}

return $response;
}

Expand Down Expand Up @@ -344,6 +353,29 @@ public function getError()
return null;
}

public function withResultFunction(Closure $resultFunction)
{
return new OperationResponse(
$this->operationName,
$this->operationsClient,
$this->getDescriptorOptions() + [
'lastProtoResponse' => $this->lastProtoResponse,
'getOperationMethod' => $this->getOperationMethod,
'cancelOperationMethod' => $this->cancelOperationMethod,
'deleteOperationMethod' => $this->deleteOperationMethod,
'operationStatusMethod' => $this->operationStatusMethod,
'operationStatusDoneValue' => $this->operationStatusDoneValue,
'additionalOperationArguments' => $this->additionalArgs,
'operationErrorCodeMethod' => $this->operationErrorCodeMethod,
'operationErrorMessageMethod' => $this->operationErrorMessageMethod,
'getOperationRequest' => $this->getOperationRequest,
'cancelOperationRequest' => $this->cancelOperationRequest,
'deleteOperationRequest' => $this->deleteOperationRequest,
'resultFunction' => $resultFunction,
]
);
}

/**
* Get an array containing the values of 'operationReturnType', 'metadataReturnType', and
* the polling options `initialPollDelayMillis`, `pollDelayMultiplier`, `maxPollDelayMillis`,
Expand Down