From b844195ff3c5e5d4544c39aea537c56a47a92530 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 10 Feb 2015 02:04:56 +0100 Subject: [PATCH] Config and api client --- src/Api/Client.php | 36 ++++++++++++++++++++---------------- src/Api/ClientResponse.php | 8 +++++--- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/Api/Client.php b/src/Api/Client.php index ce29504..e7be74e 100644 --- a/src/Api/Client.php +++ b/src/Api/Client.php @@ -7,43 +7,47 @@ use GuzzleHttp\Exception\RequestException; class Client { - const API_HOST = 'https://api.t411.me/'; - protected $client; public function __construct() { - $this->client = new GuzzleClient(); + $this->client = new GuzzleClient(array('base_url' => 'https://api.t411.me')); } public function getAuthorization($username, $password) { - return new ClientResponse($this->get( - 'auth/', + return $this->post( + '/auth', array( - 'username' => $username, - 'password' => $password, + 'body' => array( + 'username' => $username, + 'password' => $password, + ), ) ); } + public function getCategoriesTree() + { + return $this->get('/categories/tree'); + } + public function get($uri, array $options = array()) { - try { - return new ClientResponse($this->client->get(API_HOST.$uri, $options)); - } catch (RequestException $e) { - throw new ApiClientException(sprintf('Request exception (GET): %s', $e->getMessage())); - } catch (HttpConnectException $e) { - throw new ApiClientException(sprintf('HTTP Connection exception: %s', $e->getMessage())); - } + return $this->send('get', $uri, $options); } public function post($uri, array $options = array()) + { + return $this->send('post', $uri, $options); + } + + protected function send($method, $uri, $options) { try { - return new ClientResponse($this->client->post(API_HOST.$uri, $options)); + return new ClientResponse($this->client->{$method}($uri, $options)); } catch (RequestException $e) { - throw new ApiClientException(sprintf('Request exception (POST): %s', $e->getMessage())); + throw new ApiClientException(sprintf('Request exception (%s): %s', strtoupper($method), $e->getMessage())); } catch (HttpConnectException $e) { throw new ApiClientException(sprintf('HTTP Connection exception: %s', $e->getMessage())); } diff --git a/src/Api/ClientResponse.php b/src/Api/ClientResponse.php index 50970c1..e374be7 100644 --- a/src/Api/ClientResponse.php +++ b/src/Api/ClientResponse.php @@ -2,11 +2,13 @@ namespace Api; +use GuzzleHttp\Message\Response; + class ClientResponse { protected $response = null; - public function __construct(FutureInterface $response) + public function __construct(Response $response) { $this->response = $response; } @@ -18,7 +20,7 @@ class ClientResponse public function getErrorCode() { - if ($this->hasError()) { + if (!$this->hasError()) { return null; } @@ -27,7 +29,7 @@ class ClientResponse public function getErrorMessage() { - if ($this->hasError()) { + if (!$this->hasError()) { return null; }