From 940ec307fb0638e3e2b2021b08e7eb0a108f3070 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20HULARD?= Date: Sat, 27 Feb 2016 20:30:17 +0100 Subject: [PATCH 1/2] Introduce a `decodeResponse` method. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This new method allow to perform a `rawCall` call and retrieve the full response instance. It'll help to manipulate response more easily. Signed-off-by: Stéphane HULARD --- src/Api.php | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/Api.php b/src/Api.php index 139269d..2084f84 100644 --- a/src/Api.php +++ b/src/Api.php @@ -192,11 +192,13 @@ class Api $parameters->redirection = $redirection; //bypass authentication for this call - $response = $this->rawCall( - 'POST', - '/auth/credential', - $parameters, - false + $response = $this->decodeResponse( + $this->rawCall( + 'POST', + '/auth/credential', + $parameters, + false + ) ); $this->consumer_key = $response["consumerKey"]; @@ -220,7 +222,6 @@ class Api { $url = $this->endpoint . $path; $request = new Request($method, $url); - if (isset($content) && $method == 'GET') { $query_string = $request->getUri()->getQuery(); @@ -261,7 +262,6 @@ class Api } else { $body = ""; } - $headers = [ 'Content-Type' => 'application/json; charset=utf-8', 'X-Ovh-Application' => $this->application_key, @@ -285,8 +285,18 @@ class Api } /** @var Response $response */ - $response = $this->http_client->send($request, ['headers' => $headers]); + return $this->http_client->send($request, ['headers' => $headers]); + } + /** + * Decode a Response object body to an Array + * + * @param Response $response + * + * @return array + */ + private function decodeResponse(Response $response) + { return json_decode($response->getBody(), true); } @@ -301,7 +311,9 @@ class Api */ public function get($path, $content = null) { - return $this->rawCall("GET", $path, $content); + return $this->decodeResponse( + $this->rawCall("GET", $path, $content) + ); } /** @@ -315,7 +327,9 @@ class Api */ public function post($path, $content = null) { - return $this->rawCall("POST", $path, $content); + return $this->decodeResponse( + $this->rawCall("POST", $path, $content) + ); } /** @@ -329,7 +343,9 @@ class Api */ public function put($path, $content) { - return $this->rawCall("PUT", $path, $content); + return $this->decodeResponse( + $this->rawCall("PUT", $path, $content) + ); } /** @@ -343,7 +359,9 @@ class Api */ public function delete($path, $content = null) { - return $this->rawCall("DELETE", $path, $content); + return $this->decodeResponse( + $this->rawCall("DELETE", $path, $content) + ); } /** From 9703ee3cdd71454936c647ef292066d8a2597993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20HULARD?= Date: Sat, 27 Feb 2016 20:31:32 +0100 Subject: [PATCH 2/2] Remove a direct client call, use API `get` method. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove the `$this->http_client->get()` call used in the `calculateTimeDelta` method. This call is too coupled to the HttpClient, using the same method for all the calls is always better. Signed-off-by: Stéphane HULARD --- src/Api.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Api.php b/src/Api.php index 2084f84..3752146 100644 --- a/src/Api.php +++ b/src/Api.php @@ -165,8 +165,13 @@ class Api private function calculateTimeDelta() { if (!isset($this->time_delta)) { - $response = $this->http_client->get($this->endpoint . "/auth/time"); - $serverTimestamp = (int)(String)$response->getBody(); + $response = $this->rawCall( + 'GET', + "/auth/time", + null, + false + ); + $serverTimestamp = (int)(string)$response->getBody(); $this->time_delta = $serverTimestamp - (int)\time(); }