Skip to content

Commit

Permalink
Update component.json to include the updated Auth librry
Browse files Browse the repository at this point in the history
  • Loading branch information
Hectorhammett committed Dec 11, 2024
1 parent 6fd8fb4 commit 90e16a2
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 55 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"license": "BSD-3-Clause",
"require": {
"php": "^8.0",
"google/auth": "^1.34.0",
"google/auth": "^1.45",
"google/grpc-gcp": "^0.4",
"grpc/grpc": "^1.13",
"google/protobuf": "^v3.25.3||^4.26.1",
Expand Down
6 changes: 3 additions & 3 deletions src/BidiStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,12 @@ public function read()
if ($this->logger) {
$responseEvent = new RpcLogEvent();

$responseEvent->headers = (is_null($result)) ? $this->call->getMetadata() : null;
$responseEvent->status = (is_null($result)) ? $status->code : null;
$responseEvent->headers = $this->call->getMetadata();
$responseEvent->status = $status->code ?? null;
$responseEvent->processId = (int) getmypid();
$responseEvent->requestId = crc32((string) spl_object_id($this) . getmypid());

if ($result && $result instanceof Message) {
if ($result instanceof Message) {
$responseEvent->payload = $result->serializeToJsonString();
}

Expand Down
14 changes: 10 additions & 4 deletions src/ClientOptionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,15 @@ private function buildClientOptions(array $options)
$options += $defaultOptions;

// If logger is explicitly set to false, logging is disabled
if ($options['logger'] !== false) {
$options['logger'] = $options['logger'] ?? ApplicationDefaultCredentials::getDefaultLogger();
if (is_null($options['logger'])) {
$options['logger'] = ApplicationDefaultCredentials::getDefaultLogger();
}

if ($options['logger'] !== null && $options['logger'] !== false && !$options['logger'] instanceof LoggerInterface) {
if (
$options['logger'] !== null
&& $options['logger'] !== false
&& !$options['logger'] instanceof LoggerInterface
) {
throw new ValidationException(
'The "logger" option in the options array should be PSR-3 LoggerInterface compatible'
);
Expand All @@ -143,7 +147,9 @@ private function buildClientOptions(array $options)
$this->logConfiguration($options['logger'], $clientSuppliedOptions);

if (isset($options['logger'])) {
$options['credentialsConfig']['authHttpHandler'] = HttpHandlerFactory::build(logger: $options['logger']);
$options['credentialsConfig']['authHttpHandler'] = HttpHandlerFactory::build(
logger: $options['logger']
);
}

$options['credentialsConfig'] += $defaultOptions['credentialsConfig'];
Expand Down
3 changes: 2 additions & 1 deletion src/ClientStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function __construct(// @phpstan-ignore-line
*/
public function write($request)
{
// In some cases, $request can be a string
if ($this->logger && $request instanceof Message) {
$requestEvent = new RpcLogEvent();

Expand Down Expand Up @@ -102,7 +103,7 @@ public function readResponse()
$responseEvent->processId = (int) getmypid();
$responseEvent->requestId = crc32((string) spl_object_id($this) . getmypid());

if ($response && $response instanceof Message) {
if ($response instanceof Message) {
$response->serializeToJsonString();
}

Expand Down
3 changes: 2 additions & 1 deletion src/Transport/GrpcFallbackTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ public static function build(string $apiEndpoint, array $config = [])
$config += [
'httpHandler' => null,
'clientCertSource' => null,
'logger' => null,
];
list($baseUri, $port) = self::normalizeServiceAddress($apiEndpoint);
$httpHandler = $config['httpHandler'] ?: self::buildHttpHandlerAsync(logger: $config['logger'] ?? null);
$httpHandler = $config['httpHandler'] ?: self::buildHttpHandlerAsync(logger: $config['logger']);
$transport = new GrpcFallbackTransport("$baseUri:$port", $httpHandler);
if ($config['clientCertSource']) {
$transport->configureMtlsChannel($config['clientCertSource']);
Expand Down
5 changes: 3 additions & 2 deletions src/Transport/GrpcTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public static function build(string $apiEndpoint, array $config = [])
'channel' => null,
'interceptors' => [],
'clientCertSource' => null,
'logger' => null,
];
list($addr, $port) = self::normalizeServiceAddress($apiEndpoint);
$host = "$addr:$port";
Expand All @@ -157,10 +158,10 @@ public static function build(string $apiEndpoint, array $config = [])
);
}
try {
if (isset($config['logger']) && $config['logger'] === false) {
if ($config['logger'] === false) {
$config['logger'] = null;
}
return new GrpcTransport($host, $stubOpts, $channel, $config['interceptors'], $config['logger'] ?? null);
return new GrpcTransport($host, $stubOpts, $channel, $config['interceptors'], $config['logger']);
} catch (Exception $ex) {
throw new ValidationException(
'Failed to build GrpcTransport: ' . $ex->getMessage(),
Expand Down
2 changes: 1 addition & 1 deletion src/Transport/HttpUnaryTransportTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private static function buildCommonHeaders(array $options)
private static function buildHttpHandlerAsync(null|false|LoggerInterface $logger = null)
{
try {
return [HttpHandlerFactory::build(null, $logger), 'async'];
return [HttpHandlerFactory::build(logger: $logger), 'async'];
} catch (Exception $ex) {
throw new ValidationException('Failed to build HttpHandler', $ex->getCode(), $ex);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Transport/RestTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ public static function build(string $apiEndpoint, string $restConfigPath, array
'httpHandler' => null,
'clientCertSource' => null,
'hasEmulator' => false,
'logger' => null,
];
list($baseUri, $port) = self::normalizeServiceAddress($apiEndpoint);
$requestBuilder = $config['hasEmulator']
? new InsecureRequestBuilder("$baseUri:$port", $restConfigPath)
: new RequestBuilder("$baseUri:$port", $restConfigPath);
$httpHandler = $config['httpHandler'] ?: self::buildHttpHandlerAsync($config['logger'] ?? null);
$httpHandler = $config['httpHandler'] ?: self::buildHttpHandlerAsync($config['logger']);
$transport = new RestTransport($requestBuilder, $httpHandler);
if ($config['clientCertSource']) {
$transport->configureMtlsChannel($config['clientCertSource']);
Expand Down
31 changes: 1 addition & 30 deletions tests/Unit/Middleware/RetryMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,7 @@ public function testMaxRetries()

public function testRetriesAreIncludedInTheOptionsArray()
{
$call = $this->getMockBuilder(Call::class)
->disableOriginalConstructor()
->getMock();
$call = $this->prophesize(Call::class)->reveal();

$maxRetries = 1;
$reportedRetries = 0;
Expand Down Expand Up @@ -528,31 +526,4 @@ public function testUnlimitedMaxRetries()

$middleware($call->reveal(), [])->wait();
}
}

class MockHandler implements MiddlewareInterface
{
public int $calls;
public bool $containsRetries = false;

public function __construct()
{
$this->calls = 0;
}

public function __invoke(Call $call, array $options)
{
$promise = new Promise();

if ($this->calls === 0) {
$this->calls += 1;
return $promise->reject();
}

if (array_key_exists('retryAttempt') && $options['retryAttempt'] === 1) {
$this->containsRetries = true;
}

return $promise->resolve();
}
}
11 changes: 0 additions & 11 deletions tests/Unit/Transport/GrpcTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,17 +439,6 @@ public function testClientCertSourceOptionInvalid()
);
}

// public function testLoggerGetsCalledIfLoggerSupplied()
// {
// // $logger = $this->prophesize(StdOutLogger::class);
// // $logger->debug(Argument::cetera())
// // ->shouldBeCalledTimes(2);
// // $logger->info(Argument::cetera())
// // ->shouldBeCalledTimes(1);
// // $logger->log(Argument::cetera())
// // ->shouldBeCalled();
// }

/**
* @dataProvider buildDataGrpc
*/
Expand Down

0 comments on commit 90e16a2

Please sign in to comment.