t411-console/src/Api/Client.php
2017-06-08 21:13:11 +02:00

120 lines
2.9 KiB
PHP

<?php
namespace Api;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\RequestException;
class Client
{
protected $client;
protected $token;
public function __construct()
{
$this->client = new GuzzleClient(['base_url' => 'https://api.t411.al']);
}
public function getAuthorization($username, $password)
{
return $this->post(
false,
'/auth',
array(
'body' => array(
'username' => $username,
'password' => $password,
),
)
);
}
public function setAuthorization($token)
{
$this->token = $token;
}
public function getCategoriesTree()
{
return $this->get(true, '/categories/tree');
}
public function getTermsTree()
{
return $this->get(true, '/terms/tree');
}
public function searchTorrents($query, $options)
{
$url = '/torrents/search/'.urlencode($query);
$query = [];
foreach (['offset', 'limit', 'cat', 'cid'] as $p) {
if (!empty($options[$p])) {
$query[$p] = $options[$p];
}
}
if (!empty($options['terms'])) {
$query['terms'] = $options['terms'];
}
$url.= '?'.http_build_query($query);
return $this->get(true, $url);
}
public function downloadTorrent($id)
{
return $this->get(true, '/torrents/download/'.$id);
}
public function getTorrentDetails($id)
{
return $this->get(true, '/torrents/details/'.$id);
}
public function getTopTorrents($period)
{
return $this->get(true, '/torrents/top/'.$period);
}
public function getUserProfile($id)
{
return $this->get(true, '/users/profile/'.$id);
}
public function get($needAuthorization, $uri, array $options = array())
{
return $this->send($needAuthorization, 'get', $uri, $options);
}
public function post($needAuthorization, $uri, array $options = array())
{
return $this->send($needAuthorization, 'post', $uri, $options);
}
protected function send($needAuthorization, $method, $uri, $options)
{
if ($needAuthorization) {
$options = array_merge(
$options,
array(
'headers' => array(
'Authorization' => $this->token,
),
)
);
}
try {
return new ClientResponse($this->client->{$method}($uri, $options));
} catch (RequestException $e) {
throw new ClientException(sprintf('Request exception (%s): %s', strtoupper($method), $e->getMessage()));
} catch (\Exception $e) {
throw new ClientException(sprintf('Exception: %s', $e->getMessage()));
}
}
}