Config and api client
This commit is contained in:
parent
a02ba75555
commit
b844195ff3
|
@ -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()));
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue