Skip to content

Commit 7e79f3d

Browse files
feat: add the protected apiVersion property (#2588)
1 parent 017400f commit 7e79f3d

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/Service/Resource.php

+10
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class Resource
4848
/** @var string $rootUrlTemplate */
4949
private $rootUrlTemplate;
5050

51+
/** @var string $apiVersion */
52+
protected $apiVersion;
53+
5154
/** @var \Google\Client $client */
5255
private $client;
5356

@@ -225,6 +228,13 @@ public function call($name, $arguments, $expectedClass = null)
225228
$expectedClass = null;
226229
}
227230

231+
// If the class which is extending from this one contains
232+
// an Api Version, add it to the header
233+
if ($this->apiVersion) {
234+
$request = $request
235+
->withHeader('X-Goog-Api-Version', $this->apiVersion);
236+
}
237+
228238
// if the client is marked for deferring, rather than
229239
// execute the request, return the response
230240
if ($this->client->shouldDefer()) {

tests/Google/Service/ResourceTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public function testCall()
106106
$this->assertEquals("https://test.example.com/method/path", (string) $request->getUri());
107107
$this->assertEquals("POST", $request->getMethod());
108108
$this->assertFalse($request->hasHeader('Content-Type'));
109+
$this->assertFalse($request->hasHeader('X-Goog-Api-Version'));
109110
}
110111

111112
public function testCallWithUniverseDomainTemplate()
@@ -474,4 +475,32 @@ public function testExceptionMessage()
474475
$this->assertEquals($errors, $e->getErrors());
475476
}
476477
}
478+
479+
public function testVersionedResource()
480+
{
481+
$resource = new VersionedResource(
482+
$this->service,
483+
"test",
484+
"testResource",
485+
[
486+
"methods" => [
487+
"testMethod" => [
488+
"parameters" => [],
489+
"path" => "method/path",
490+
"httpMethod" => "POST",
491+
]
492+
]
493+
]
494+
);
495+
$request = $resource->call("testMethod", [['postBody' => ['foo' => 'bar']]]);
496+
$this->assertEquals("https://test.example.com/method/path", (string) $request->getUri());
497+
$this->assertEquals("POST", $request->getMethod());
498+
$this->assertTrue($request->hasHeader('X-Goog-Api-Version'));
499+
$this->assertEquals('v1_20240101', $request->getHeaderLine('X-Goog-Api-Version'));
500+
}
501+
}
502+
503+
class VersionedResource extends GoogleResource
504+
{
505+
protected $apiVersion = 'v1_20240101';
477506
}

0 commit comments

Comments
 (0)