2018-08-10 20:10:19 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Hummingbird Anime List Client
|
|
|
|
*
|
|
|
|
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
|
|
|
|
*
|
|
|
|
* PHP version 7
|
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
|
|
|
* @copyright 2015 - 2018 Timothy J. Warren
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @version 4.0
|
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
|
|
|
*/
|
|
|
|
|
2018-08-15 08:51:37 -04:00
|
|
|
namespace Aviat\AnimeClient\API\Anilist;
|
2018-08-10 20:10:19 -04:00
|
|
|
|
|
|
|
use function Amp\Promise\wait;
|
|
|
|
|
|
|
|
use Aviat\AnimeClient\API\{
|
|
|
|
Anilist,
|
|
|
|
HummingbirdClient
|
|
|
|
};
|
|
|
|
|
|
|
|
trait AnilistTrait {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The request builder for the MAL API
|
|
|
|
* @var AnilistRequestBuilder
|
|
|
|
*/
|
|
|
|
protected $requestBuilder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The base url for api requests
|
|
|
|
* @var string $base_url
|
|
|
|
*/
|
|
|
|
protected $baseUrl = Anilist::BASE_URL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HTTP headers to send with every request
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $defaultHeaders = [
|
|
|
|
'Accept' => 'application/json',
|
|
|
|
'Accept-Encoding' => 'gzip',
|
|
|
|
'Content-type' => 'application/json',
|
|
|
|
'User-Agent' => "Tim's Anime Client/4.0"
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the request builder object
|
|
|
|
*
|
2018-08-15 08:51:37 -04:00
|
|
|
* @param AnilistRequestBuilder $requestBuilder
|
2018-08-10 20:10:19 -04:00
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setRequestBuilder($requestBuilder): self
|
|
|
|
{
|
|
|
|
$this->requestBuilder = $requestBuilder;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a request object
|
2018-08-15 08:51:37 -04:00
|
|
|
|
2018-08-10 20:10:19 -04:00
|
|
|
* @param string $url
|
|
|
|
* @param array $options
|
|
|
|
* @return \Amp\Artax\Response
|
|
|
|
*/
|
2018-08-15 08:51:37 -04:00
|
|
|
public function setUpRequest(string $url, array $options = [])
|
2018-08-10 20:10:19 -04:00
|
|
|
{
|
2018-08-15 08:51:37 -04:00
|
|
|
// @TODO Implement
|
2018-08-10 20:10:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make a request
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param string $url
|
|
|
|
* @param array $options
|
|
|
|
* @return \Amp\Artax\Response
|
|
|
|
*/
|
|
|
|
private function getResponse(string $type, string $url, array $options = [])
|
|
|
|
{
|
|
|
|
$logger = NULL;
|
|
|
|
if ($this->getContainer())
|
|
|
|
{
|
|
|
|
$logger = $this->container->getLogger('mal-request');
|
|
|
|
}
|
|
|
|
|
2018-08-15 08:51:37 -04:00
|
|
|
$request = $this->setUpRequest($url, $options);
|
2018-08-10 20:10:19 -04:00
|
|
|
$response = wait((new HummingbirdClient)->request($request));
|
|
|
|
|
2018-08-15 08:51:37 -04:00
|
|
|
$logger->debug('Anilist response', [
|
2018-08-10 20:10:19 -04:00
|
|
|
'status' => $response->getStatus(),
|
|
|
|
'reason' => $response->getReason(),
|
|
|
|
'body' => $response->getBody(),
|
|
|
|
'headers' => $response->getHeaders(),
|
|
|
|
'requestHeaders' => $request->getHeaders(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make a request
|
|
|
|
*
|
|
|
|
* @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())
|
|
|
|
{
|
|
|
|
$logger = $this->container->getLogger('anilist-request');
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $this->getResponse($type, $url, $options);
|
|
|
|
|
|
|
|
if ((int) $response->getStatus() > 299 OR (int) $response->getStatus() < 200)
|
|
|
|
{
|
|
|
|
if ($logger)
|
|
|
|
{
|
|
|
|
$logger->warning('Non 200 response for api call', (array)$response->getBody());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return XML::toArray(wait($response->getBody()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove some boilerplate for post requests
|
|
|
|
*
|
|
|
|
* @param mixed ...$args
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function postRequest(...$args): array
|
|
|
|
{
|
|
|
|
$logger = NULL;
|
|
|
|
if ($this->getContainer())
|
|
|
|
{
|
|
|
|
$logger = $this->container->getLogger('anilist-request');
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $this->getResponse('POST', ...$args);
|
|
|
|
$validResponseCodes = [200, 201];
|
|
|
|
|
|
|
|
if ( ! \in_array((int) $response->getStatus(), $validResponseCodes, TRUE))
|
|
|
|
{
|
|
|
|
if ($logger)
|
|
|
|
{
|
|
|
|
$logger->warning('Non 201 response for POST api call', (array)$response->getBody());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return XML::toArray($response->getBody());
|
|
|
|
}
|
|
|
|
}
|