Skip to content

Commit d05024a

Browse files
committed
feat: Add the protected apiVersion property
1 parent 633d41f commit d05024a

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

src/Service/Resource.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class Resource
4848
/** @var string $rootUrl */
4949
private $rootUrl;
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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public function testCall()
104104
$this->assertEquals("https://test.example.com/method/path", (string) $request->getUri());
105105
$this->assertEquals("POST", $request->getMethod());
106106
$this->assertFalse($request->hasHeader('Content-Type'));
107+
$this->assertFalse($request->hasHeader('X-Goog-Api-Version'));
107108
}
108109

109110
public function testCallWithPostBody()
@@ -441,4 +442,30 @@ public function testExceptionMessage()
441442
$this->assertEquals($errors, $e->getErrors());
442443
}
443444
}
445+
446+
public function testVersionedResource() {
447+
$resource = new VersionedResource(
448+
$this->service,
449+
"test",
450+
"testResource",
451+
[
452+
"methods" => [
453+
"testMethod" => [
454+
"parameters" => [],
455+
"path" => "method/path",
456+
"httpMethod" => "POST",
457+
]
458+
]
459+
]
460+
);
461+
$request = $resource->call("testMethod", [['postBody' => ['foo' => 'bar']]]);
462+
$this->assertEquals("https://test.example.com/method/path", (string) $request->getUri());
463+
$this->assertEquals("POST", $request->getMethod());
464+
$this->assertTrue($request->hasHeader('X-Goog-Api-Version'));
465+
}
444466
}
467+
468+
class VersionedResource extends GoogleResource
469+
{
470+
protected $apiVersion = 'v1_20240101';
471+
}

0 commit comments

Comments
 (0)