From fd01972575ec5f1e1d70d7176694e4651e2d1f0f Mon Sep 17 00:00:00 2001 From: Hector Mendoza Jacobo Date: Mon, 22 Apr 2024 20:51:41 +0000 Subject: [PATCH] feat: Add the protected apiVersion property --- src/Service/Resource.php | 10 ++++++++++ tests/Google/Service/ResourceTest.php | 28 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/Service/Resource.php b/src/Service/Resource.php index ecf402b18..b21d5879e 100644 --- a/src/Service/Resource.php +++ b/src/Service/Resource.php @@ -48,6 +48,9 @@ class Resource /** @var string $rootUrl */ private $rootUrl; + /** @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 17000880a..d9e4dfd37 100644 --- a/tests/Google/Service/ResourceTest.php +++ b/tests/Google/Service/ResourceTest.php @@ -104,6 +104,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 testCallWithPostBody() @@ -441,4 +442,31 @@ 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')); + } +} + +class VersionedResource extends GoogleResource +{ + protected $apiVersion = 'v1_20240101'; }