2016-12-21 12:46:20 -05:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Anime List Client
|
|
|
|
*
|
|
|
|
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
|
|
|
|
*
|
|
|
|
* PHP version 7
|
|
|
|
*
|
2017-01-06 23:34:56 -05:00
|
|
|
* @package AnimeListClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2017-01-11 10:30:53 -05:00
|
|
|
* @copyright 2015 - 2017 Timothy J. Warren
|
2017-01-06 23:34:56 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @version 4.0
|
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient
|
2017-01-11 10:34:24 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\API\Kitsu;
|
2016-12-21 12:46:20 -05:00
|
|
|
|
2017-01-05 13:41:32 -05:00
|
|
|
use Aviat\AnimeClient\AnimeClient;
|
2016-12-21 12:46:20 -05:00
|
|
|
use Aviat\AnimeClient\API\GuzzleTrait;
|
2017-01-09 20:36:48 -05:00
|
|
|
use Aviat\AnimeClient\API\Kitsu as K;
|
2016-12-22 21:36:23 -05:00
|
|
|
use Aviat\Ion\Json;
|
2016-12-21 12:46:20 -05:00
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use GuzzleHttp\Cookie\CookieJar;
|
2017-01-06 21:39:01 -05:00
|
|
|
use GuzzleHttp\Psr7\Response;
|
2016-12-22 21:36:23 -05:00
|
|
|
use InvalidArgumentException;
|
|
|
|
use RuntimeException;
|
2016-12-21 12:46:20 -05:00
|
|
|
|
|
|
|
trait KitsuTrait {
|
2017-02-07 13:27:41 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
/**
|
|
|
|
* The request builder for the MAL API
|
|
|
|
* @var MALRequestBuilder
|
|
|
|
*/
|
|
|
|
protected $requestBuilder;
|
2017-02-07 13:27:41 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
/**
|
2017-02-07 13:27:41 -05:00
|
|
|
* The Guzzle http client object
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
protected $client;
|
2017-02-07 13:11:42 -05:00
|
|
|
|
2017-02-07 13:27:41 -05:00
|
|
|
/**
|
|
|
|
* Cookie jar object for api requests
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
protected $cookieJar;
|
2016-12-21 12:46:20 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The base url for api requests
|
|
|
|
* @var string $base_url
|
|
|
|
*/
|
|
|
|
protected $baseUrl = "https://kitsu.io/api/edge/";
|
|
|
|
|
2017-01-06 21:39:01 -05:00
|
|
|
/**
|
|
|
|
* HTTP headers to send with every request
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $defaultHeaders = [
|
|
|
|
'User-Agent' => "Tim's Anime Client/4.0",
|
|
|
|
'Accept-Encoding' => 'application/vnd.api+json',
|
2017-01-09 20:36:48 -05:00
|
|
|
'Content-Type' => 'application/vnd.api+json',
|
2017-01-06 21:39:01 -05:00
|
|
|
'client_id' => 'dd031b32d2f56c990b1425efe6c42ad847e7fe3ab46bf1299f05ecd856bdb7dd',
|
|
|
|
'client_secret' => '54d7307928f63414defd96399fc31ba847961ceaecef3a5fd93144e960c0e151',
|
|
|
|
];
|
2017-02-07 13:27:41 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
/**
|
|
|
|
* Set the request builder object
|
|
|
|
*
|
|
|
|
* @param KitsuRequestBuilder $requestBuilder
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setRequestBuilder($requestBuilder): self
|
|
|
|
{
|
|
|
|
$this->requestBuilder = $requestBuilder;
|
|
|
|
return $this;
|
|
|
|
}
|
2017-01-06 21:39:01 -05:00
|
|
|
|
2016-12-21 12:46:20 -05:00
|
|
|
/**
|
|
|
|
* Set up the class properties
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function init()
|
|
|
|
{
|
2017-01-05 13:41:32 -05:00
|
|
|
$defaults = [
|
|
|
|
'cookies' => $this->cookieJar,
|
2017-01-06 21:39:01 -05:00
|
|
|
'headers' => $this->defaultHeaders,
|
2017-01-05 13:41:32 -05:00
|
|
|
'timeout' => 25,
|
|
|
|
'connect_timeout' => 25
|
|
|
|
];
|
|
|
|
|
2016-12-21 12:46:20 -05:00
|
|
|
$this->cookieJar = new CookieJar();
|
|
|
|
$this->client = new Client([
|
|
|
|
'base_uri' => $this->baseUrl,
|
|
|
|
'cookies' => TRUE,
|
|
|
|
'http_errors' => TRUE,
|
2017-01-05 13:41:32 -05:00
|
|
|
'defaults' => $defaults
|
2016-12-21 12:46:20 -05:00
|
|
|
]);
|
|
|
|
}
|
2016-12-22 21:36:23 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make a request via Guzzle
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param string $url
|
|
|
|
* @param array $options
|
2017-01-06 21:39:01 -05:00
|
|
|
* @return Response
|
2016-12-22 21:36:23 -05:00
|
|
|
*/
|
2017-01-06 21:39:01 -05:00
|
|
|
private function getResponse(string $type, string $url, array $options = [])
|
2016-12-22 21:36:23 -05:00
|
|
|
{
|
2017-01-06 21:39:01 -05:00
|
|
|
$logger = null;
|
2016-12-22 21:36:23 -05:00
|
|
|
$validTypes = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'];
|
|
|
|
|
|
|
|
if ( ! in_array($type, $validTypes))
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException('Invalid http request type');
|
|
|
|
}
|
|
|
|
|
|
|
|
$defaultOptions = [
|
2017-01-06 21:39:01 -05:00
|
|
|
'headers' => $this->defaultHeaders
|
2016-12-22 21:36:23 -05:00
|
|
|
];
|
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
$logger = $this->container->getLogger('kitsu-request');
|
2017-01-09 20:36:48 -05:00
|
|
|
$sessionSegment = $this->getContainer()
|
|
|
|
->get('session')
|
|
|
|
->getSegment(AnimeClient::SESSION_SEGMENT);
|
2017-01-05 13:41:32 -05:00
|
|
|
|
2017-01-09 20:36:48 -05:00
|
|
|
if ($sessionSegment->get('auth_token') !== null && $url !== K::AUTH_URL)
|
|
|
|
{
|
|
|
|
$token = $sessionSegment->get('auth_token');
|
|
|
|
$defaultOptions['headers']['Authorization'] = "bearer {$token}";
|
2017-01-05 13:41:32 -05:00
|
|
|
}
|
|
|
|
|
2016-12-22 21:36:23 -05:00
|
|
|
$options = array_merge($defaultOptions, $options);
|
|
|
|
|
2017-02-04 15:18:34 -05:00
|
|
|
$response = $this->client->request($type, $url, $options);
|
2017-01-09 20:36:48 -05:00
|
|
|
|
2017-02-04 15:18:34 -05:00
|
|
|
$logger->debug('Kitsu API request', [
|
|
|
|
'requestParams' => [
|
|
|
|
'type' => $type,
|
|
|
|
'url' => $url,
|
|
|
|
],
|
|
|
|
'responseValues' => [
|
|
|
|
'status' => $response->getStatusCode()
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $response;
|
2017-01-06 21:39:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make a request via Guzzle
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param string $url
|
|
|
|
* @param array $options
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function request(string $type, string $url, array $options = []): array
|
|
|
|
{
|
|
|
|
$logger = null;
|
|
|
|
if ($this->getContainer())
|
|
|
|
{
|
2017-02-07 13:11:42 -05:00
|
|
|
$logger = $this->container->getLogger('kitsu-request');
|
2017-01-06 21:39:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$response = $this->getResponse($type, $url, $options);
|
2016-12-22 21:36:23 -05:00
|
|
|
|
2017-01-09 20:36:48 -05:00
|
|
|
if ((int) $response->getStatusCode() > 299 || (int) $response->getStatusCode() < 200)
|
2016-12-22 21:36:23 -05:00
|
|
|
{
|
|
|
|
if ($logger)
|
|
|
|
{
|
2017-02-04 15:18:34 -05:00
|
|
|
$logger->warning('Non 200 response for api call', $response->getBody());
|
2016-12-22 21:36:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON::decode($response->getBody(), TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove some boilerplate for get requests
|
|
|
|
*
|
|
|
|
* @param array $args
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getRequest(...$args): array
|
|
|
|
{
|
|
|
|
return $this->request('GET', ...$args);
|
|
|
|
}
|
|
|
|
|
2017-01-06 21:39:01 -05:00
|
|
|
/**
|
|
|
|
* Remove some boilerplate for patch requests
|
|
|
|
*
|
|
|
|
* @param array $args
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function patchRequest(...$args): array
|
|
|
|
{
|
|
|
|
return $this->request('PATCH', ...$args);
|
|
|
|
}
|
|
|
|
|
2016-12-22 21:36:23 -05:00
|
|
|
/**
|
2017-01-03 20:29:43 -05:00
|
|
|
* Remove some boilerplate for post requests
|
2016-12-22 21:36:23 -05:00
|
|
|
*
|
|
|
|
* @param array $args
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function postRequest(...$args): array
|
|
|
|
{
|
2017-01-06 21:39:01 -05:00
|
|
|
$logger = null;
|
|
|
|
if ($this->getContainer())
|
|
|
|
{
|
2017-02-07 13:11:42 -05:00
|
|
|
$logger = $this->container->getLogger('kitsu-request');
|
2017-01-06 21:39:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$response = $this->getResponse('POST', ...$args);
|
|
|
|
$validResponseCodes = [200, 201];
|
|
|
|
|
|
|
|
if ( ! in_array((int) $response->getStatusCode(), $validResponseCodes))
|
|
|
|
{
|
|
|
|
if ($logger)
|
|
|
|
{
|
2017-02-04 15:18:34 -05:00
|
|
|
$logger->warning('Non 201 response for POST api call', $response->getBody());
|
2017-01-06 21:39:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON::decode($response->getBody(), TRUE);
|
2016-12-22 21:36:23 -05:00
|
|
|
}
|
2017-01-03 20:29:43 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove some boilerplate for delete requests
|
|
|
|
*
|
|
|
|
* @param array $args
|
2017-01-06 21:39:01 -05:00
|
|
|
* @return bool
|
2017-01-03 20:29:43 -05:00
|
|
|
*/
|
2017-01-06 21:39:01 -05:00
|
|
|
protected function deleteRequest(...$args): bool
|
2017-01-03 20:29:43 -05:00
|
|
|
{
|
2017-01-06 21:39:01 -05:00
|
|
|
$response = $this->getResponse('DELETE', ...$args);
|
|
|
|
return ((int) $response->getStatusCode() === 204);
|
2017-01-03 20:29:43 -05:00
|
|
|
}
|
2016-12-21 12:46:20 -05:00
|
|
|
}
|