Skip to content

Commit

Permalink
feat: add the protected apiVersion property (#2588)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hectorhammett authored May 2, 2024
1 parent 017400f commit 7e79f3d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Service/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class Resource
/** @var string $rootUrlTemplate */
private $rootUrlTemplate;

/** @var string $apiVersion */
protected $apiVersion;

/** @var \Google\Client $client */
private $client;

Expand Down Expand Up @@ -225,6 +228,13 @@ public function call($name, $arguments, $expectedClass = null)
$expectedClass = null;
}

// If the class which is extending from this one contains
// an Api Version, add it to the header
if ($this->apiVersion) {
$request = $request
->withHeader('X-Goog-Api-Version', $this->apiVersion);
}

// if the client is marked for deferring, rather than
// execute the request, return the response
if ($this->client->shouldDefer()) {
Expand Down
29 changes: 29 additions & 0 deletions tests/Google/Service/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public function testCall()
$this->assertEquals("https://test.example.com/method/path", (string) $request->getUri());
$this->assertEquals("POST", $request->getMethod());
$this->assertFalse($request->hasHeader('Content-Type'));
$this->assertFalse($request->hasHeader('X-Goog-Api-Version'));
}

public function testCallWithUniverseDomainTemplate()
Expand Down Expand Up @@ -474,4 +475,32 @@ public function testExceptionMessage()
$this->assertEquals($errors, $e->getErrors());
}
}

public function testVersionedResource()
{
$resource = new VersionedResource(
$this->service,
"test",
"testResource",
[
"methods" => [
"testMethod" => [
"parameters" => [],
"path" => "method/path",
"httpMethod" => "POST",
]
]
]
);
$request = $resource->call("testMethod", [['postBody' => ['foo' => 'bar']]]);
$this->assertEquals("https://test.example.com/method/path", (string) $request->getUri());
$this->assertEquals("POST", $request->getMethod());
$this->assertTrue($request->hasHeader('X-Goog-Api-Version'));
$this->assertEquals('v1_20240101', $request->getHeaderLine('X-Goog-Api-Version'));
}
}

class VersionedResource extends GoogleResource
{
protected $apiVersion = 'v1_20240101';
}

0 comments on commit 7e79f3d

Please sign in to comment.