2015-06-11 16:44:52 -04:00
|
|
|
<?php
|
2015-06-16 11:11:35 -04:00
|
|
|
/**
|
|
|
|
* Base API Model
|
|
|
|
*/
|
2015-09-15 13:19:29 -04:00
|
|
|
namespace Aviat\AnimeClient\Model;
|
2015-06-11 16:44:52 -04:00
|
|
|
|
|
|
|
use \GuzzleHttp\Client;
|
|
|
|
use \GuzzleHttp\Cookie\CookieJar;
|
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-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
|
|
|
*
|
|
|
|
* @param Container $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([
|
|
|
|
'base_url' => $this->base_url,
|
|
|
|
'defaults' => [
|
|
|
|
'cookies' => $this->cookieJar,
|
|
|
|
'headers' => [
|
|
|
|
'User-Agent' => $_SERVER['HTTP_USER_AGENT'],
|
|
|
|
'Accept-Encoding' => 'application/json'
|
|
|
|
],
|
|
|
|
'timeout' => 5,
|
|
|
|
'connect_timeout' => 5
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
}
|
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
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function authenticate($username, $password)
|
|
|
|
{
|
|
|
|
$result = $this->client->post('https://hummingbird.me/api/v1/users/authenticate', [
|
|
|
|
'body' => [
|
2015-06-26 16:39:10 -04:00
|
|
|
'username' => $username,
|
2015-06-24 16:01:35 -04:00
|
|
|
'password' => $password
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($result->getStatusCode() === 201)
|
|
|
|
{
|
|
|
|
$_SESSION['hummingbird_anime_token'] = $result->json();
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
2015-06-11 16:44:52 -04:00
|
|
|
}
|
|
|
|
// End of BaseApiModel.php
|