t411-console/src/Api/Client.php

120 lines
2.9 KiB
PHP
Raw Normal View History

2015-02-10 01:17:29 +01:00
<?php
namespace Api;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\RequestException;
class Client
{
protected $client;
2015-02-10 02:21:03 +01:00
protected $token;
2015-02-10 01:17:29 +01:00
public function __construct()
{
2017-03-25 22:14:50 +01:00
$this->client = new GuzzleClient(['base_url' => 'https://api.t411.ai']);
2015-02-10 01:17:29 +01:00
}
public function getAuthorization($username, $password)
{
2015-02-10 02:04:56 +01:00
return $this->post(
2015-02-10 02:21:03 +01:00
false,
2015-02-10 02:04:56 +01:00
'/auth',
2015-02-10 01:17:29 +01:00
array(
2015-02-10 02:04:56 +01:00
'body' => array(
'username' => $username,
'password' => $password,
),
2015-02-10 01:17:29 +01:00
)
);
}
2015-02-10 02:21:03 +01:00
public function setAuthorization($token)
{
$this->token = $token;
}
2015-02-10 02:04:56 +01:00
public function getCategoriesTree()
{
2015-02-10 02:21:03 +01:00
return $this->get(true, '/categories/tree');
2015-02-10 02:04:56 +01:00
}
2015-02-10 02:49:23 +01:00
public function getTermsTree()
{
return $this->get(true, '/terms/tree');
}
public function searchTorrents($query, $options)
{
$url = '/torrents/search/'.urlencode($query);
2015-02-10 14:09:40 +01:00
$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);
}
2015-02-10 09:53:19 +01:00
public function getTorrentDetails($id)
{
return $this->get(true, '/torrents/details/'.$id);
}
2015-02-10 14:49:16 +01:00
public function getTopTorrents($period)
{
return $this->get(true, '/torrents/top/'.$period);
}
2015-02-11 01:28:43 +01:00
public function getUserProfile($id)
{
return $this->get(true, '/users/profile/'.$id);
}
2015-02-10 02:21:03 +01:00
public function get($needAuthorization, $uri, array $options = array())
2015-02-10 01:17:29 +01:00
{
2015-02-10 02:21:03 +01:00
return $this->send($needAuthorization, 'get', $uri, $options);
2015-02-10 01:17:29 +01:00
}
2015-02-10 02:21:03 +01:00
public function post($needAuthorization, $uri, array $options = array())
2015-02-10 02:04:56 +01:00
{
2015-02-10 02:21:03 +01:00
return $this->send($needAuthorization, 'post', $uri, $options);
2015-02-10 02:04:56 +01:00
}
2015-02-10 02:21:03 +01:00
protected function send($needAuthorization, $method, $uri, $options)
2015-02-10 01:17:29 +01:00
{
2015-02-10 02:21:03 +01:00
if ($needAuthorization) {
$options = array_merge(
$options,
array(
'headers' => array(
'Authorization' => $this->token,
),
)
);
}
2015-02-10 01:17:29 +01:00
try {
2015-02-10 02:04:56 +01:00
return new ClientResponse($this->client->{$method}($uri, $options));
2015-02-10 01:17:29 +01:00
} catch (RequestException $e) {
2015-02-10 09:12:29 +01:00
throw new ClientException(sprintf('Request exception (%s): %s', strtoupper($method), $e->getMessage()));
2015-03-05 15:04:08 +01:00
} catch (\Exception $e) {
throw new ClientException(sprintf('Exception: %s', $e->getMessage()));
2015-02-10 01:17:29 +01:00
}
}
}