HummingBirdAnimeClient/src/Aviat/AnimeClient/Model/API.php

195 lines
4.0 KiB
PHP
Raw Normal View History

<?php
/**
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 - 2016
2015-11-16 11:40:01 -05:00
* @link https://github.com/timw4mail/HummingBirdAnimeClient
* @license MIT
*/
2015-09-15 13:19:29 -04:00
namespace Aviat\AnimeClient\Model;
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-09-17 23:11:18 -04:00
use Aviat\Ion\Di\ContainerInterface;
use Aviat\AnimeClient\Model as BaseModel;
/**
* 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-09-17 23:11:18 -04:00
class API extends BaseModel {
/**
* Base url for making api requests
* @var string
*/
protected $base_url = '';
/**
* The Guzzle http client object
* @var object
*/
protected $client;
/**
* Cookie jar object for api requests
* @var object
*/
protected $cookieJar;
/**
* Constructor
*
* @param ContainerInterface $container
*/
2015-09-17 23:11:18 -04:00
public function __construct(ContainerInterface $container)
{
parent::__construct($container);
2015-12-08 16:39:49 -05:00
$this->init();
}
/**
* Set up the class properties
*
* @return void
*/
protected function init()
{
$this->cookieJar = new CookieJar();
$this->client = new Client([
'base_uri' => $this->base_url,
'cookies' => TRUE,
2015-10-19 12:50:46 -04:00
'http_errors' => FALSE,
'defaults' => [
'cookies' => $this->cookieJar,
'headers' => [
2015-10-19 12:50:46 -04:00
'User-Agent' => "Tim's Anime Client/2.0",
'Accept-Encoding' => 'application/json'
],
2015-11-11 14:53:09 -05:00
'timeout' => 25,
'connect_timeout' => 25
]
]);
}
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
}
/**
* Get the data for the specified library entry
*
* @param string $id
* @param string $status
* @return array
*/
public function get_library_item($id, $status)
{
$data = $this->_get_list_from_api($status);
$index_array = array_column($data, 'id');
$key = array_search($id, $index_array);
return $key !== FALSE
? $data[$key]
: [];
}
/**
* Sort the manga entries by their title
*
* @codeCoverageIgnore
* @param array $array
* @param string $sort_key
* @return void
*/
protected function sort_by_name(&$array, $sort_key)
{
$sort = array();
foreach ($array as $key => $item)
{
$sort[$key] = $item[$sort_key]['title'];
}
array_multisort($sort, SORT_ASC, $array);
}
/**
* Attempt login via the api
*
* @codeCoverageIgnore
* @param string $username
* @param string $password
2015-10-14 09:20:52 -04:00
* @return string|false
*/
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,
'password' => $password
]
]);
2015-10-19 12:50:46 -04:00
if ($response->getStatusCode() === 201)
{
2015-10-19 12:50:46 -04:00
return json_decode($response->getBody(), TRUE);
}
return FALSE;
}
/**
* Dummy function that should be abstract. Is not abstract because
* this class is used concretely for authorizing API calls
*
* @TODO Refactor, and make this abstract
* @param string $status
* @return array
*/
protected function _get_list_from_api($status)
{
return [];
}
}
// End of BaseApiModel.php