From 7e79f3d7be4811f760e19cc4a2c558e04196ec1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Mendoza?= Date: Thu, 2 May 2024 12:34:51 -0400 Subject: [PATCH] feat: add the protected apiVersion property (#2588) --- src/Service/Resource.php | 10 +++++++++ tests/Google/Service/ResourceTest.php | 29 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/Service/Resource.php b/src/Service/Resource.php index edc3a36ee..693aaa781 100644 --- a/src/Service/Resource.php +++ b/src/Service/Resource.php @@ -48,6 +48,9 @@ class Resource /** @var string $rootUrlTemplate */ private $rootUrlTemplate; + /** @var string $apiVersion */ + protected $apiVersion; + /** @var \Google\Client $client */ private $client; @@ -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()) { diff --git a/tests/Google/Service/ResourceTest.php b/tests/Google/Service/ResourceTest.php index 86e0bef24..ccf29a7f7 100644 --- a/tests/Google/Service/ResourceTest.php +++ b/tests/Google/Service/ResourceTest.php @@ -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() @@ -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'; }