2017-01-12 15:41:20 -05:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
2017-02-15 16:13:32 -05:00
|
|
|
* Hummingbird Anime List Client
|
2017-01-12 15:41:20 -05:00
|
|
|
*
|
|
|
|
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
|
|
|
|
*
|
|
|
|
* PHP version 7
|
|
|
|
*
|
2017-02-15 16:13:32 -05:00
|
|
|
* @package HummingbirdAnimeClient
|
2017-01-12 15:41:20 -05:00
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
|
|
|
* @copyright 2015 - 2017 Timothy J. Warren
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @version 4.0
|
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\API\MAL;
|
|
|
|
|
2017-02-04 15:18:34 -05:00
|
|
|
use Amp\Artax\{Client, FormBody, Request};
|
2017-01-12 15:41:20 -05:00
|
|
|
use Aviat\AnimeClient\API\{
|
|
|
|
MAL as M,
|
2017-02-07 13:11:42 -05:00
|
|
|
APIRequestBuilder,
|
2017-01-12 15:41:20 -05:00
|
|
|
XML
|
|
|
|
};
|
2017-02-07 13:11:42 -05:00
|
|
|
use Aviat\AnimeClient\API\MALRequestBuilder;
|
2017-02-01 09:53:02 -05:00
|
|
|
use Aviat\Ion\Json;
|
2017-01-12 15:41:20 -05:00
|
|
|
use InvalidArgumentException;
|
|
|
|
|
|
|
|
trait MALTrait {
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
/**
|
|
|
|
* The request builder for the MAL API
|
|
|
|
* @var MALRequestBuilder
|
|
|
|
*/
|
|
|
|
protected $requestBuilder;
|
2017-01-12 15:41:20 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The base url for api requests
|
|
|
|
* @var string $base_url
|
|
|
|
*/
|
|
|
|
protected $baseUrl = M::BASE_URL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HTTP headers to send with every request
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $defaultHeaders = [
|
2017-02-04 15:18:34 -05:00
|
|
|
'Accept' => 'text/xml',
|
|
|
|
'Accept-Encoding' => 'gzip',
|
|
|
|
'Content-type' => 'application/x-www-form-urlencoded',
|
2017-01-12 15:41:20 -05:00
|
|
|
'User-Agent' => "Tim's Anime Client/4.0"
|
|
|
|
];
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
/**
|
|
|
|
* Set the request builder object
|
|
|
|
*
|
|
|
|
* @param MALRequestBuilder $requestBuilder
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setRequestBuilder($requestBuilder): self
|
|
|
|
{
|
|
|
|
$this->requestBuilder = $requestBuilder;
|
|
|
|
return $this;
|
|
|
|
}
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-04 15:18:34 -05:00
|
|
|
/**
|
|
|
|
* Unencode the dual-encoded ampersands in the body
|
|
|
|
*
|
|
|
|
* This is a dirty hack until I can fully track down where
|
|
|
|
* the dual-encoding happens
|
|
|
|
*
|
|
|
|
* @param FormBody $formBody The form builder object to fix
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function fixBody(FormBody $formBody): string
|
|
|
|
{
|
|
|
|
$rawBody = \Amp\wait($formBody->getBody());
|
|
|
|
return html_entity_decode($rawBody, \ENT_HTML5, 'UTF-8');
|
|
|
|
}
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-01-12 15:41:20 -05:00
|
|
|
/**
|
2017-02-07 13:11:42 -05:00
|
|
|
* Create a request object
|
2017-01-12 15:41:20 -05:00
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param string $url
|
|
|
|
* @param array $options
|
2017-02-08 15:48:20 -05:00
|
|
|
* @return \Amp\Artax\Response
|
2017-01-12 15:41:20 -05:00
|
|
|
*/
|
2017-02-07 13:11:42 -05:00
|
|
|
public function setUpRequest(string $type, string $url, array $options = [])
|
2017-01-12 15:41:20 -05:00
|
|
|
{
|
|
|
|
$config = $this->container->get('config');
|
2017-02-04 15:18:34 -05:00
|
|
|
|
2017-02-08 00:44:57 -05:00
|
|
|
$request = $this->requestBuilder
|
|
|
|
->newRequest($type, $url)
|
|
|
|
->setBasicAuth($config->get(['mal','username']), $config->get(['mal','password']));
|
2017-02-04 15:18:34 -05:00
|
|
|
|
2017-02-08 00:44:57 -05:00
|
|
|
if (array_key_exists('query', $options))
|
2017-02-04 15:18:34 -05:00
|
|
|
{
|
2017-02-08 00:44:57 -05:00
|
|
|
$request->setQuery($options['query']);
|
2017-02-04 15:18:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('body', $options))
|
|
|
|
{
|
|
|
|
$request->setBody($options['body']);
|
|
|
|
}
|
2017-02-08 00:44:57 -05:00
|
|
|
|
|
|
|
return $request->getFullRequest();
|
2017-02-07 13:11:42 -05:00
|
|
|
}
|
2017-02-04 15:18:34 -05:00
|
|
|
|
2017-02-07 13:11:42 -05: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');
|
|
|
|
}
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
$request = $this->setUpRequest($type, $url, $options);
|
2017-02-04 15:18:34 -05:00
|
|
|
$response = \Amp\wait((new Client)->request($request));
|
|
|
|
|
2017-02-08 00:44:57 -05:00
|
|
|
$logger->debug('MAL api response', [
|
2017-02-04 15:18:34 -05:00
|
|
|
'status' => $response->getStatus(),
|
|
|
|
'reason' => $response->getReason(),
|
2017-02-08 00:44:57 -05:00
|
|
|
'body' => $response->getBody(),
|
2017-02-04 15:18:34 -05:00
|
|
|
'headers' => $response->getAllHeaders(),
|
|
|
|
'requestHeaders' => $request->getAllHeaders(),
|
|
|
|
]);
|
2017-01-12 15:41:20 -05:00
|
|
|
|
2017-02-04 15:18:34 -05:00
|
|
|
return $response;
|
2017-01-12 15:41:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-07 13:11:42 -05:00
|
|
|
* Make a request
|
2017-01-12 15:41:20 -05:00
|
|
|
*
|
|
|
|
* @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('mal-request');
|
2017-01-12 15:41:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$response = $this->getResponse($type, $url, $options);
|
|
|
|
|
2017-02-01 09:53:02 -05:00
|
|
|
if ((int) $response->getStatus() > 299 || (int) $response->getStatus() < 200)
|
2017-01-12 15:41:20 -05:00
|
|
|
{
|
|
|
|
if ($logger)
|
|
|
|
{
|
2017-02-04 15:18:34 -05:00
|
|
|
$logger->warning('Non 200 response for api call', $response->getBody());
|
2017-01-12 15:41:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return XML::toArray((string) $response->getBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove some boilerplate for get requests
|
|
|
|
*
|
|
|
|
* @param array $args
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getRequest(...$args): array
|
|
|
|
{
|
|
|
|
return $this->request('GET', ...$args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove some boilerplate for post requests
|
|
|
|
*
|
|
|
|
* @param array $args
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function postRequest(...$args): array
|
|
|
|
{
|
|
|
|
$logger = null;
|
|
|
|
if ($this->getContainer())
|
|
|
|
{
|
2017-02-07 13:11:42 -05:00
|
|
|
$logger = $this->container->getLogger('mal-request');
|
2017-01-12 15:41:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$response = $this->getResponse('POST', ...$args);
|
|
|
|
$validResponseCodes = [200, 201];
|
|
|
|
|
2017-02-01 09:53:02 -05:00
|
|
|
if ( ! in_array((int) $response->getStatus(), $validResponseCodes))
|
2017-01-12 15:41:20 -05:00
|
|
|
{
|
|
|
|
if ($logger)
|
|
|
|
{
|
2017-02-04 15:18:34 -05:00
|
|
|
$logger->warning('Non 201 response for POST api call', $response->getBody());
|
2017-01-12 15:41:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 09:53:02 -05:00
|
|
|
return XML::toArray($response->getBody());
|
2017-01-12 15:41:20 -05:00
|
|
|
}
|
|
|
|
}
|