From d115cf7f51eea1f955e7365c51e2587cc854b8aa Mon Sep 17 00:00:00 2001 From: Federico Ricchiuto Date: Fri, 27 Oct 2023 20:28:30 +0200 Subject: [PATCH] feat: enable endpoint version selection - add utility method to set a version in the URL - unit test the new functionality - improve readability with null coalescing operator --- lib/Client.php | 19 +++++++++++++------ test/unit/ClientTest.php | 11 ++++++++++- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/Client.php b/lib/Client.php index bf36535..82adc54 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -32,6 +32,7 @@ * @method Client sums() * @method Client monitor() * @method Client test() + * @method Client _version() * * Access settings * @method Client access_settings() @@ -619,23 +620,29 @@ public function __call($name, $args) { $name = mb_strtolower($name); + // set the API version if ($name === 'version') { $this->version = $args[0]; return $this->_(); } + // set the endpoint version (e.g. client.name.name._version("2.0")) + if ($name === '_version') { + return $this->_($version = $args[0]); + } + // send all saved requests if (($name === 'send') && $this->isConcurrentRequest) { return $this->makeAllRequests(); } if (\in_array($name, $this->methods, true)) { - $body = isset($args[0]) ? $args[0] : null; - $queryParams = isset($args[1]) ? $args[1] : null; - $url = $this->buildUrl($queryParams); - $headers = isset($args[2]) ? $args[2] : null; - $retryOnLimit = isset($args[3]) ? $args[3] : $this->retryOnLimit; + $body = $args[0] ?? null; + $queryParams = $args[1] ?? null; + $url = $this->buildUrl($queryParams); + $headers = $args[2] ?? null; + $retryOnLimit = $args[3] ?? $this->retryOnLimit; if ($this->isConcurrentRequest) { // save request to be sent later @@ -650,4 +657,4 @@ public function __call($name, $args) return $this->_($name); } -} +} \ No newline at end of file diff --git a/test/unit/ClientTest.php b/test/unit/ClientTest.php index cfea449..d0f3ac9 100644 --- a/test/unit/ClientTest.php +++ b/test/unit/ClientTest.php @@ -70,6 +70,15 @@ public function test__call() $this->assertEquals(['path_to_endpoint', 'one_more_segment'], $client->getPath()); } + public function test__callWithEndpointVersion() + { + $endpoint_version = '2.0'; + $object_id = '00000000-0000-0000-0000-000000000000'; + $response = $this->client->segments()->_version($endpoint_version)->_($object_id)->patch(); + + $this->assertEquals("https://localhost:4010/v3/segments/$endpoint_version/$object_id", $response->url); + } + public function testGetHost() { $client = new Client('https://localhost:4010'); @@ -249,4 +258,4 @@ private function callMethod($obj, $name, $args = []) $method->setAccessible(true); return $method->invokeArgs($obj, $args); } -} +} \ No newline at end of file