2015-06-11 16:44:52 -04:00
|
|
|
<?php
|
2015-06-16 11:11:35 -04:00
|
|
|
/**
|
2015-11-16 11:40:01 -05:00
|
|
|
* Hummingbird Anime Client
|
|
|
|
*
|
|
|
|
* An API client for Hummingbird to manage anime and manga watch lists
|
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren
|
|
|
|
* @copyright Copyright (c) 2015
|
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient
|
|
|
|
* @license MIT
|
2015-06-16 11:11:35 -04:00
|
|
|
*/
|
2015-09-15 13:19:29 -04:00
|
|
|
namespace Aviat\AnimeClient\Model;
|
2015-06-11 16:44:52 -04:00
|
|
|
|
2015-10-05 16:54:25 -04:00
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use GuzzleHttp\Cookie\CookieJar;
|
|
|
|
use GuzzleHttp\Psr7\Request;
|
2015-10-15 09:25:30 -04:00
|
|
|
use GuzzleHttp\Psr7\ResponseInterface;
|
2015-10-19 12:50:46 -04:00
|
|
|
use GuzzleHttp\Exception\ClientException;
|
2015-10-05 16:54:25 -04:00
|
|
|
|
2015-09-17 23:11:18 -04:00
|
|
|
use Aviat\Ion\Di\ContainerInterface;
|
|
|
|
use Aviat\AnimeClient\Model as BaseModel;
|
2015-06-11 16:44:52 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base model for api interaction
|
2015-10-15 09:25:30 -04:00
|
|
|
*
|
|
|
|
* @method ResponseInterface get(string $uri, array $options);
|
|
|
|
* @method ResponseInterface delete(string $uri, array $options);
|
|
|
|
* @method ResponseInterface head(string $uri, array $options);
|
|
|
|
* @method ResponseInterface options(string $uri, array $options);
|
|
|
|
* @method ResponseInterface patch(string $uri, array $options);
|
|
|
|
* @method ResponseInterface post(string $uri, array $options);
|
|
|
|
* @method ResponseInterface put(string $uri, array $options);
|
2015-06-11 16:44:52 -04:00
|
|
|
*/
|
2015-09-17 23:11:18 -04:00
|
|
|
class API extends BaseModel {
|
2015-06-11 16:44:52 -04:00
|
|
|
|
2015-06-26 12:03:42 -04:00
|
|
|
/**
|
|
|
|
* Base url for making api requests
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $base_url = '';
|
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* The Guzzle http client object
|
2015-06-26 12:03:42 -04:00
|
|
|
* @var object
|
2015-06-11 16:44:52 -04:00
|
|
|
*/
|
|
|
|
protected $client;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cookie jar object for api requests
|
2015-06-26 12:03:42 -04:00
|
|
|
* @var object
|
2015-06-11 16:44:52 -04:00
|
|
|
*/
|
|
|
|
protected $cookieJar;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
2015-09-16 12:25:35 -04:00
|
|
|
*
|
2015-10-06 10:24:48 -04:00
|
|
|
* @param ContainerInterface $container
|
2015-06-11 16:44:52 -04:00
|
|
|
*/
|
2015-09-17 23:11:18 -04:00
|
|
|
public function __construct(ContainerInterface $container)
|
2015-06-11 16:44:52 -04:00
|
|
|
{
|
2015-09-14 10:54:50 -04:00
|
|
|
parent::__construct($container);
|
2015-06-11 16:44:52 -04:00
|
|
|
$this->cookieJar = new CookieJar();
|
|
|
|
$this->client = new Client([
|
2015-10-05 16:54:25 -04:00
|
|
|
'base_uri' => $this->base_url,
|
|
|
|
'cookies' => TRUE,
|
2015-10-19 12:50:46 -04:00
|
|
|
'http_errors' => FALSE,
|
2015-06-11 16:44:52 -04:00
|
|
|
'defaults' => [
|
|
|
|
'cookies' => $this->cookieJar,
|
|
|
|
'headers' => [
|
2015-10-19 12:50:46 -04:00
|
|
|
'User-Agent' => "Tim's Anime Client/2.0",
|
2015-06-11 16:44:52 -04:00
|
|
|
'Accept-Encoding' => 'application/json'
|
|
|
|
],
|
2015-11-11 14:53:09 -05:00
|
|
|
'timeout' => 25,
|
|
|
|
'connect_timeout' => 25
|
2015-06-11 16:44:52 -04:00
|
|
|
]
|
|
|
|
]);
|
|
|
|
}
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2015-10-15 09:25:30 -04:00
|
|
|
/**
|
|
|
|
* Magic methods to call guzzle api client
|
|
|
|
*
|
|
|
|
* @param string $method
|
|
|
|
* @param array $args
|
|
|
|
* @return ResponseInterface|null
|
|
|
|
*/
|
|
|
|
public function __call($method, $args)
|
|
|
|
{
|
|
|
|
$valid_methods = [
|
|
|
|
'get',
|
|
|
|
'delete',
|
|
|
|
'head',
|
|
|
|
'options',
|
|
|
|
'patch',
|
|
|
|
'post',
|
|
|
|
'put'
|
|
|
|
];
|
|
|
|
|
|
|
|
if ( ! in_array($method, $valid_methods))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
array_unshift($args, strtoupper($method));
|
2015-11-05 11:26:03 -05:00
|
|
|
return call_user_func_array([$this->client, 'request'], $args);
|
2015-10-15 09:25:30 -04:00
|
|
|
}
|
|
|
|
|
2015-06-24 16:01:35 -04:00
|
|
|
/**
|
|
|
|
* Attempt login via the api
|
|
|
|
*
|
2015-06-26 12:03:42 -04:00
|
|
|
* @codeCoverageIgnore
|
2015-06-24 16:01:35 -04:00
|
|
|
* @param string $username
|
|
|
|
* @param string $password
|
2015-10-14 09:20:52 -04:00
|
|
|
* @return string|false
|
2015-06-24 16:01:35 -04:00
|
|
|
*/
|
|
|
|
public function authenticate($username, $password)
|
|
|
|
{
|
2015-10-19 12:50:46 -04:00
|
|
|
$response = $this->post('https://hummingbird.me/api/v1/users/authenticate', [
|
|
|
|
'form_params' => [
|
2015-06-26 16:39:10 -04:00
|
|
|
'username' => $username,
|
2015-06-24 16:01:35 -04:00
|
|
|
'password' => $password
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
2015-10-19 12:50:46 -04:00
|
|
|
if ($response->getStatusCode() === 201)
|
2015-06-24 16:01:35 -04:00
|
|
|
{
|
2015-10-19 12:50:46 -04:00
|
|
|
return json_decode($response->getBody(), TRUE);
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
2015-06-11 16:44:52 -04:00
|
|
|
}
|
|
|
|
// End of BaseApiModel.php
|