forked from deblan/t411-console
Config and api client
This commit is contained in:
parent
a02ba75555
commit
b844195ff3
|
@ -7,43 +7,47 @@ use GuzzleHttp\Exception\RequestException;
|
||||||
|
|
||||||
class Client
|
class Client
|
||||||
{
|
{
|
||||||
const API_HOST = 'https://api.t411.me/';
|
|
||||||
|
|
||||||
protected $client;
|
protected $client;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->client = new GuzzleClient();
|
$this->client = new GuzzleClient(array('base_url' => 'https://api.t411.me'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAuthorization($username, $password)
|
public function getAuthorization($username, $password)
|
||||||
{
|
{
|
||||||
return new ClientResponse($this->get(
|
return $this->post(
|
||||||
'auth/',
|
'/auth',
|
||||||
array(
|
array(
|
||||||
'username' => $username,
|
'body' => array(
|
||||||
'password' => $password,
|
'username' => $username,
|
||||||
|
'password' => $password,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getCategoriesTree()
|
||||||
|
{
|
||||||
|
return $this->get('/categories/tree');
|
||||||
|
}
|
||||||
|
|
||||||
public function get($uri, array $options = array())
|
public function get($uri, array $options = array())
|
||||||
{
|
{
|
||||||
try {
|
return $this->send('get', $uri, $options);
|
||||||
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()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function post($uri, array $options = array())
|
public function post($uri, array $options = array())
|
||||||
|
{
|
||||||
|
return $this->send('post', $uri, $options);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function send($method, $uri, $options)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return new ClientResponse($this->client->post(API_HOST.$uri, $options));
|
return new ClientResponse($this->client->{$method}($uri, $options));
|
||||||
} catch (RequestException $e) {
|
} 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) {
|
} catch (HttpConnectException $e) {
|
||||||
throw new ApiClientException(sprintf('HTTP Connection exception: %s', $e->getMessage()));
|
throw new ApiClientException(sprintf('HTTP Connection exception: %s', $e->getMessage()));
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,11 +2,13 @@
|
||||||
|
|
||||||
namespace Api;
|
namespace Api;
|
||||||
|
|
||||||
|
use GuzzleHttp\Message\Response;
|
||||||
|
|
||||||
class ClientResponse
|
class ClientResponse
|
||||||
{
|
{
|
||||||
protected $response = null;
|
protected $response = null;
|
||||||
|
|
||||||
public function __construct(FutureInterface $response)
|
public function __construct(Response $response)
|
||||||
{
|
{
|
||||||
$this->response = $response;
|
$this->response = $response;
|
||||||
}
|
}
|
||||||
|
@ -18,7 +20,7 @@ class ClientResponse
|
||||||
|
|
||||||
public function getErrorCode()
|
public function getErrorCode()
|
||||||
{
|
{
|
||||||
if ($this->hasError()) {
|
if (!$this->hasError()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +29,7 @@ class ClientResponse
|
||||||
|
|
||||||
public function getErrorMessage()
|
public function getErrorMessage()
|
||||||
{
|
{
|
||||||
if ($this->hasError()) {
|
if (!$this->hasError()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue