Skip to content

Commit

Permalink
feat: Add support for the API version header (#706)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hectorhammett committed Apr 29, 2024
1 parent 2e2d45d commit 28a3161
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion googleapis
Submodule googleapis updated 729 files
23 changes: 23 additions & 0 deletions src/Ast/PhpProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@

namespace Google\Generator\Ast;

use Google\Generator\Utils\ResolvedType;

/** A property within a class. */
final class PhpProperty extends PhpClassMember
{
private ?ResolvedType $type;

public function __construct(string $name)
{
$this->name = $name;
$this->value = null;
$this->type = null;
}

/**
Expand All @@ -39,6 +44,18 @@ public function withValue(Expression $value): PhpProperty
return $this->clone(fn ($clone) => $clone->value = $value);
}

/**
* Adds a type declaration to the property
*
* @param ResolvedType $type The type of the property
*
* @return PhpProperty
*/
public function withType(ResolvedType $type): PhpProperty
{
return $this->clone(fn ($clone) => $clone->type = $type);
}

public function getName(): string
{
return $this->name;
Expand All @@ -49,7 +66,13 @@ public function toCode(): string
return
$this->phpDocToCode() .
$this->accessToCode() .
$this->typeToCode() .
'$' . $this->name .
(is_null($this->value) ? ';' : ' = ' . static::toPhp($this->value) . ';');
}

private function typeToCode(): string
{
return $this->type ? $this->type->toCode() . ' ' : '';
}
}
15 changes: 15 additions & 0 deletions src/Generation/GapicClientV2Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Google\Auth\FetchAuthTokenInterface;
use Google\Generator\Ast\AST;
use Google\Generator\Ast\Access;
use Google\Generator\Ast\TypeDeclaration;
use Google\Generator\Ast\PhpClass;
use Google\Generator\Ast\PhpClassMember;
use Google\Generator\Ast\PhpDoc;
Expand Down Expand Up @@ -139,6 +140,7 @@ private function generateClass(): PhpClass
->withMember($this->hasServiceAddressTemplate() ? $this->serviceAddressTemplate() : null)
->withMember($this->servicePort())
->withMember($this->codegenName())
->withMember($this->apiVersion())
->withMember($this->serviceScopes())
->withMember($this->operationsClient())
->withMember($this->getClientDefaults())
Expand All @@ -160,6 +162,19 @@ private function serviceName(): PhpClassMember
->withValue($this->serviceDetails->serviceName);
}

private function apiVersion()
{
if (is_null($this->serviceDetails->apiVersion)) {
return;
}

return AST::property('apiVersion')
->withPhpDocText('The api version of the service')
->withAccess(Access::PRIVATE)
->withType(ResolvedType::string())
->withValue(AST::literal("'" . $this->serviceDetails->apiVersion . "'"));
}

private function serviceAddress(): PhpClassMember
{
return AST::constant('SERVICE_ADDRESS')
Expand Down
4 changes: 4 additions & 0 deletions src/Generation/ServiceDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class ServiceDetails
/** @var string *Readonly* The proto package name for this service. */
public string $package;

/** @var string *Readonly* The api version for this service */
public ?string $apiVersion;

/** @var string *Readonly* The PHP namespace for this service. */
public string $namespace;

Expand Down Expand Up @@ -172,6 +175,7 @@ public function __construct(
$this->migrationMode = $migrationMode;
$this->transportType = $transportType;
$this->serviceYamlConfig = $serviceYamlConfig;
$this->apiVersion = ProtoHelpers::getCustomOption($desc, CustomOptions::GOOGLE_API_VERSION);
$this->gapicClientType = Type::fromName("{$namespace}\\Gapic\\{$desc->getName()}GapicClient");
$this->emptyClientType = Type::fromName("{$namespace}\\{$desc->getName()}Client");
$this->gapicClientV2Type = Type::fromName("{$namespace}\\Client\\{$desc->getName()}Client");
Expand Down
1 change: 1 addition & 0 deletions src/Utils/CustomOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class CustomOptions
public const GOOGLE_API_HTTP = 72295728;
public const GOOGLE_API_RESOURCEREFERENCE = 1055;
public const GOOGLE_API_RESOURCEDEFINITION = 1053;
public const GOOGLE_API_VERSION = 525000001;

public const GOOGLE_API_FIELDBEHAVIOR_REQUIRED = 2;

Expand Down
1 change: 1 addition & 0 deletions tests/Unit/ProtoTests/Basic/basic.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import "google/api/field_behavior.proto";
service Basic {
option (google.api.default_host) = "basic.example.com";
option (google.api.oauth_scopes) = "scope1,scope2";
option (google.api.api_version) = "v1_20240418";

// Test summary text for AMethod
rpc AMethod(Request) returns(Response) {
Expand Down
3 changes: 3 additions & 0 deletions tests/Unit/ProtoTests/Basic/out/src/Client/BasicClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ final class BasicClient
/** The name of the code generator, to be included in the agent header. */
private const CODEGEN_NAME = 'gapic';

/** The api version of the service */
private string $apiVersion = 'v1_20240418';

/** The default scopes required by the service. */
public static $serviceScopes = [
'scope1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final class BasicGrpcOnlyClient
private const SERVICE_NAME = 'testing.basicgrpconly.BasicGrpcOnly';

/** The default address of the service. */
private const SERVICE_ADDRESS = 'basicGrpcOnly.example.com';
private const SERVICE_ADDRESS = 'basicgrpconly.example.com';

/** The default port of the service. */
private const DEFAULT_SERVICE_PORT = 443;
Expand Down Expand Up @@ -86,7 +86,7 @@ private static function supportedTransports()
*
* @type string $apiEndpoint
* The address of the API remote host. May optionally include the port, formatted
* as "<uri>:<port>". Default 'basicGrpcOnly.example.com:443'.
* as "<uri>:<port>". Default 'basicgrpconly.example.com:443'.
* @type string|array|FetchAuthTokenInterface|CredentialsWrapper $credentials
* The credentials to be used by the client to authorize API calls. This option
* accepts either a path to a credentials file, or a decoded credentials file as a
Expand Down

0 comments on commit 28a3161

Please sign in to comment.