Skip to content

Commit

Permalink
Add a GenerateRequestIdMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Hectorhammett committed Dec 9, 2024
1 parent 59321ca commit 7bdb96f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/GapicClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Google\ApiCore\LongRunning\OperationsClient;
use Google\ApiCore\Middleware\CredentialsWrapperMiddleware;
use Google\ApiCore\Middleware\FixedHeaderMiddleware;
use Google\ApiCore\Middleware\GenerateRequestIdMiddleware;
use Google\ApiCore\Middleware\OperationsMiddleware;
use Google\ApiCore\Middleware\OptionsFilterMiddleware;
use Google\ApiCore\Middleware\PagedMiddleware;
Expand Down Expand Up @@ -659,15 +660,14 @@ private function createCallStack(array $callConstructionOptions)
];
}

$randomIdentifier = rand(1000, 9999);

$callStack = function (Call $call, array $options) use ($randomIdentifier) {
$callStack = function (Call $call, array $options) {
$startCallMethod = $this->transportCallMethods[$call->getCallType()];
return $this->transport->$startCallMethod($call, $options);
};
$callStack = new CredentialsWrapperMiddleware($callStack, $this->credentialsWrapper);
$callStack = new FixedHeaderMiddleware($callStack, $fixedHeaders, true);
$callStack = new RetryMiddleware($callStack, $callConstructionOptions['retrySettings']);
$callStack = new GenerateRequestIdMiddleware($callStack);
$callStack = new RequestAutoPopulationMiddleware(
$callStack,
$callConstructionOptions['autoPopulationSettings'],
Expand Down
63 changes: 63 additions & 0 deletions src/Middleware/GenerateRequestIdMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/*
* Copyright 2024 Google LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace Google\ApiCore\Middleware;

use Google\ApiCore\Call;
use Google\ApiCore\OperationResponse;
use Google\Protobuf\Internal\Message;

/**
* Middleware which wraps the response in an OperationResponse object.
*/
class GenerateRequestIdMiddleware implements MiddlewareInterface
{
/** @var callable */
private $nextHandler;

public function __construct(callable $nextHandler)
{
$this->nextHandler = $nextHandler;
}

public function __invoke(Call $call, array $options)
{
$next = $this->nextHandler;

$requestId = crc32((string) spl_object_id($call) . rand(0,9999));
$options['requestId'] = $requestId;

return $next(
$call,
$options
);
}
}

0 comments on commit 7bdb96f

Please sign in to comment.