From e14f758e668604ae912891911329832232a34e45 Mon Sep 17 00:00:00 2001 From: Marc Carmier Date: Mon, 18 Dec 2017 12:29:51 +0100 Subject: [PATCH 1/2] Allow URL for api_endpoint specification --- src/Api.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Api.php b/src/Api.php index e7b5cc2..5fd0538 100644 --- a/src/Api.php +++ b/src/Api.php @@ -137,8 +137,19 @@ class Api throw new Exceptions\InvalidParameterException("Endpoint parameter is empty"); } - if (!array_key_exists($api_endpoint, $this->endpoints)) { - throw new Exceptions\InvalidParameterException("Unknown provided endpoint"); + if (preg_match('/^https?:\/\/..*/',$api_endpoint)) + { + $this->endpoint = $api_endpoint; + } + else + { + if (!array_key_exists($api_endpoint, $this->endpoints)) { + throw new Exceptions\InvalidParameterException("Unknown provided endpoint"); + } + else + { + $this->endpoint = $this->endpoints[$api_endpoint]; + } } if (!isset($http_client)) { @@ -149,7 +160,6 @@ class Api } $this->application_key = $application_key; - $this->endpoint = $this->endpoints[$api_endpoint]; $this->application_secret = $application_secret; $this->http_client = $http_client; $this->consumer_key = $consumer_key; From 49d607e28d649b08e191fce553bbfc673887fd9a Mon Sep 17 00:00:00 2001 From: Marc Carmier Date: Thu, 28 Dec 2017 11:34:28 +0100 Subject: [PATCH 2/2] Added unit test for endpoint specification --- tests/ApiTest.php | 102 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/tests/ApiTest.php b/tests/ApiTest.php index 4e24fd6..fdb464e 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -367,4 +367,106 @@ class ApiTest extends \PHPUnit_Framework_TestCase $api->get('/me/api/credential', ['dryRun' => true, 'notDryRun' => false]); } + /** + * Test valid predefined endpoint + */ + public function testPredefinedEndPoint() + { + $handlerStack = $this->client->getConfig('handler'); + $handlerStack->push(Middleware::mapRequest(function (Request $request) { + if($request->getUri()->getPath() == "/1.0/auth/time") { + return $request; + } + + $host = $request->getUri()->getHost(); + $this->assertEquals($host, 'ca.api.ovh.com'); + + $resource = $request->getUri()->getPath(); + $this->assertEquals($resource, '/1.0/me/api/credential'); + + $resource = $request->getUri()->getScheme(); + $this->assertEquals($resource, 'https'); + + $request = $request->withUri($request->getUri() + ->withHost('httpbin.org') + ->withPath('/') + ->withQuery('')); + return $request; + })); + //$handlerStack->push(Middleware::mapResponse(function (Response $response) { + // return $response; + //})); + + $api = new Api($this->application_key, $this->application_secret, 'ovh-ca', $this->consumer_key, $this->client); + $api->get('/me/api/credential'); + } + + /** + * Test valid provided HTTP endpoint + */ + public function testProvidedHttpEndPoint() + { + $handlerStack = $this->client->getConfig('handler'); + $handlerStack->push(Middleware::mapRequest(function (Request $request) { + if($request->getUri()->getPath() == "/1.0/auth/time") { + return $request; + } + + $host = $request->getUri()->getHost(); + $this->assertEquals($host, 'api.ovh.com'); + + $resource = $request->getUri()->getPath(); + $this->assertEquals($resource, '/1.0/me/api/credential'); + + $resource = $request->getUri()->getScheme(); + $this->assertEquals($resource, 'http'); + + $request = $request->withUri($request->getUri() + ->withHost('httpbin.org') + ->withPath('/') + ->withQuery('')); + return $request; + })); + //$handlerStack->push(Middleware::mapResponse(function (Response $response) { + // return $response; + //})); + + $api = new Api($this->application_key, $this->application_secret, 'http://api.ovh.com/1.0', $this->consumer_key, $this->client); + $api->get('/me/api/credential'); + } + + /** + * Test valid provided HTTPS endpoint + */ + public function testProvidedHttpsEndPoint() + { + $handlerStack = $this->client->getConfig('handler'); + $handlerStack->push(Middleware::mapRequest(function (Request $request) { + if($request->getUri()->getPath() == "/1.0/auth/time") { + return $request; + } + + $host = $request->getUri()->getHost(); + $this->assertEquals($host, 'api.ovh.com'); + + $resource = $request->getUri()->getPath(); + $this->assertEquals($resource, '/1.0/me/api/credential'); + + $resource = $request->getUri()->getScheme(); + $this->assertEquals($resource, 'https'); + + $request = $request->withUri($request->getUri() + ->withHost('httpbin.org') + ->withPath('/') + ->withQuery('')); + return $request; + })); + //$handlerStack->push(Middleware::mapResponse(function (Response $response) { + // return $response; + //})); + + $api = new Api($this->application_key, $this->application_secret, 'https://api.ovh.com/1.0', $this->consumer_key, $this->client); + $api->get('/me/api/credential'); + } + }