2017-02-07 13:11:42 -05:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
2017-02-15 16:13:32 -05:00
|
|
|
* Hummingbird Anime List Client
|
2017-02-07 13:11:42 -05:00
|
|
|
*
|
2018-08-22 13:48:27 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2017-02-07 13:11:42 -05:00
|
|
|
*
|
2020-04-10 15:39:39 -04:00
|
|
|
* PHP version 7.4
|
2017-02-07 13:11:42 -05:00
|
|
|
*
|
2017-02-15 16:13:32 -05:00
|
|
|
* @package HummingbirdAnimeClient
|
2017-02-07 13:11:42 -05:00
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2020-01-08 15:39:49 -05:00
|
|
|
* @copyright 2015 - 2020 Timothy J. Warren
|
2017-02-07 13:11:42 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-08-04 09:30:21 -04:00
|
|
|
* @version 5.1
|
2017-03-07 20:53:58 -05:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
2017-02-07 13:11:42 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\API\Kitsu;
|
|
|
|
|
2020-07-31 19:03:27 -04:00
|
|
|
use const Aviat\AnimeClient\SESSION_SEGMENT;
|
|
|
|
use const Aviat\AnimeClient\USER_AGENT;
|
|
|
|
|
|
|
|
use function Amp\Promise\wait;
|
|
|
|
use function Aviat\AnimeClient\getResponse;
|
|
|
|
|
2020-07-28 17:46:18 -04:00
|
|
|
use Amp\Http\Client\Request;
|
|
|
|
use Amp\Http\Client\Response;
|
2020-07-31 19:03:27 -04:00
|
|
|
use Aviat\AnimeClient\API\APIRequestBuilder;
|
|
|
|
use Aviat\AnimeClient\API\FailedResponseException;
|
2020-07-30 09:58:36 -04:00
|
|
|
use Aviat\AnimeClient\API\Kitsu as K;
|
2020-07-31 19:03:27 -04:00
|
|
|
use Aviat\AnimeClient\Enum\EventType;
|
2020-07-28 16:11:13 -04:00
|
|
|
use Aviat\Ion\Di\ContainerAware;
|
|
|
|
use Aviat\Ion\Di\ContainerInterface;
|
2020-07-31 19:03:27 -04:00
|
|
|
use Aviat\Ion\Event;
|
2020-07-28 17:46:18 -04:00
|
|
|
use Aviat\Ion\Json;
|
2020-07-31 19:03:27 -04:00
|
|
|
use Aviat\Ion\JsonException;
|
2017-02-07 13:11:42 -05:00
|
|
|
|
2018-08-08 10:12:45 -04:00
|
|
|
final class KitsuRequestBuilder extends APIRequestBuilder {
|
2020-05-08 19:18:10 -04:00
|
|
|
use ContainerAware;
|
2017-02-08 15:48:20 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
/**
|
|
|
|
* The base url for api requests
|
|
|
|
* @var string $base_url
|
|
|
|
*/
|
2020-07-31 19:03:27 -04:00
|
|
|
protected string $baseUrl = K::JSON_API_ENDPOINT;
|
2017-02-07 13:11:42 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* HTTP headers to send with every request
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2020-04-10 16:35:01 -04:00
|
|
|
protected array $defaultHeaders = [
|
2020-07-31 19:03:27 -04:00
|
|
|
'User-Agent' => USER_AGENT,
|
|
|
|
'Accept' => 'application/vnd.api+json',
|
|
|
|
'Content-Type' => 'application/vnd.api+json',
|
|
|
|
'CLIENT_ID' => 'dd031b32d2f56c990b1425efe6c42ad847e7fe3ab46bf1299f05ecd856bdb7dd',
|
|
|
|
'CLIENT_SECRET' => '54d7307928f63414defd96399fc31ba847961ceaecef3a5fd93144e960c0e151',
|
2017-02-07 13:11:42 -05:00
|
|
|
];
|
2020-05-08 19:18:10 -04:00
|
|
|
|
|
|
|
public function __construct(ContainerInterface $container)
|
|
|
|
{
|
|
|
|
$this->setContainer($container);
|
|
|
|
}
|
2020-07-28 17:46:18 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a request object
|
2020-07-31 19:03:27 -04:00
|
|
|
*
|
|
|
|
* @param string $type
|
2020-07-28 17:46:18 -04:00
|
|
|
* @param string $url
|
|
|
|
* @param array $options
|
|
|
|
* @return Request
|
|
|
|
*/
|
2020-07-31 19:03:27 -04:00
|
|
|
public function setUpRequest(string $type, string $url, array $options = []): Request
|
2020-07-28 17:46:18 -04:00
|
|
|
{
|
2020-07-31 19:03:27 -04:00
|
|
|
$request = $this->newRequest($type, $url);
|
2020-07-28 17:46:18 -04:00
|
|
|
|
2020-07-30 09:58:36 -04:00
|
|
|
$sessionSegment = $this->getContainer()
|
|
|
|
->get('session')
|
|
|
|
->getSegment(SESSION_SEGMENT);
|
|
|
|
|
|
|
|
$cache = $this->getContainer()->get('cache');
|
|
|
|
$token = null;
|
|
|
|
|
|
|
|
if ($cache->has(K::AUTH_TOKEN_CACHE_KEY))
|
|
|
|
{
|
|
|
|
$token = $cache->get(K::AUTH_TOKEN_CACHE_KEY);
|
|
|
|
}
|
|
|
|
else if ($url !== K::AUTH_URL && $sessionSegment->get('auth_token') !== NULL)
|
2020-07-28 17:46:18 -04:00
|
|
|
{
|
2020-07-30 09:58:36 -04:00
|
|
|
$token = $sessionSegment->get('auth_token');
|
|
|
|
if ( ! (empty($token) || $cache->has(K::AUTH_TOKEN_CACHE_KEY)))
|
|
|
|
{
|
|
|
|
$cache->set(K::AUTH_TOKEN_CACHE_KEY, $token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($token !== NULL)
|
|
|
|
{
|
|
|
|
$request = $request->setAuth('bearer', $token);
|
|
|
|
}
|
2020-07-28 17:46:18 -04:00
|
|
|
|
|
|
|
if (array_key_exists('form_params', $options))
|
|
|
|
{
|
|
|
|
$request = $request->setFormFields($options['form_params']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('query', $options))
|
|
|
|
{
|
|
|
|
$request = $request->setQuery($options['query']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('body', $options))
|
|
|
|
{
|
|
|
|
$request = $request->setJsonBody($options['body']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('headers', $options))
|
|
|
|
{
|
|
|
|
$request = $request->setHeaders($options['headers']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $request->getFullRequest();
|
|
|
|
}
|
|
|
|
|
2020-07-31 19:03:27 -04:00
|
|
|
/**
|
|
|
|
* Remove some boilerplate for get requests
|
|
|
|
*
|
|
|
|
* @param mixed ...$args
|
|
|
|
* @throws Throwable
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getRequest(...$args): array
|
|
|
|
{
|
|
|
|
return $this->request('GET', ...$args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove some boilerplate for patch requests
|
|
|
|
*
|
|
|
|
* @param mixed ...$args
|
|
|
|
* @throws Throwable
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function patchRequest(...$args): array
|
|
|
|
{
|
|
|
|
return $this->request('PATCH', ...$args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove some boilerplate for post requests
|
|
|
|
*
|
|
|
|
* @param mixed ...$args
|
|
|
|
* @throws Throwable
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function postRequest(...$args): array
|
|
|
|
{
|
|
|
|
$logger = NULL;
|
|
|
|
if ($this->getContainer())
|
|
|
|
{
|
|
|
|
$logger = $this->container->getLogger('kitsu-request');
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $this->getResponse('POST', ...$args);
|
|
|
|
$validResponseCodes = [200, 201];
|
|
|
|
|
|
|
|
if ( ! in_array($response->getStatus(), $validResponseCodes, TRUE) && $logger)
|
|
|
|
{
|
|
|
|
$logger->warning('Non 2xx response for POST api call', $response->getBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON::decode(wait($response->getBody()->buffer()), TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove some boilerplate for delete requests
|
|
|
|
*
|
|
|
|
* @param mixed ...$args
|
|
|
|
* @throws Throwable
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function deleteRequest(...$args): bool
|
|
|
|
{
|
|
|
|
$response = $this->getResponse('DELETE', ...$args);
|
|
|
|
return ($response->getStatus() === 204);
|
|
|
|
}
|
|
|
|
|
2020-07-28 17:46:18 -04:00
|
|
|
/**
|
|
|
|
* Run a GraphQL API query
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param array $variables
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function runQuery(string $name, array $variables = []): array
|
|
|
|
{
|
2020-08-04 14:25:18 -04:00
|
|
|
$file = __DIR__ . "/Queries/{$name}.graphql";
|
2020-07-28 17:46:18 -04:00
|
|
|
if ( ! file_exists($file))
|
|
|
|
{
|
|
|
|
throw new LogicException('GraphQL query file does not exist.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = file_get_contents($file);
|
|
|
|
$body = [
|
|
|
|
'query' => $query
|
|
|
|
];
|
|
|
|
|
|
|
|
if ( ! empty($variables))
|
|
|
|
{
|
|
|
|
$body['variables'] = [];
|
|
|
|
foreach($variables as $key => $val)
|
|
|
|
{
|
|
|
|
$body['variables'][$key] = $val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 19:03:27 -04:00
|
|
|
return $this->graphResponse([
|
2020-07-28 17:46:18 -04:00
|
|
|
'body' => $body
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param array $variables
|
|
|
|
* @return Request
|
|
|
|
* @throws Throwable
|
|
|
|
*/
|
|
|
|
public function mutateRequest (string $name, array $variables = []): Request
|
|
|
|
{
|
2020-08-04 14:25:18 -04:00
|
|
|
$file = __DIR__ . "/Mutations/{$name}.graphql";
|
2020-07-31 19:03:27 -04:00
|
|
|
if ( ! file_exists($file))
|
2020-07-28 17:46:18 -04:00
|
|
|
{
|
|
|
|
throw new LogicException('GraphQL mutation file does not exist.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = file_get_contents($file);
|
|
|
|
$body = [
|
|
|
|
'query' => $query
|
|
|
|
];
|
|
|
|
|
|
|
|
if (!empty($variables)) {
|
|
|
|
$body['variables'] = [];
|
|
|
|
foreach ($variables as $key => $val)
|
|
|
|
{
|
|
|
|
$body['variables'][$key] = $val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 19:03:27 -04:00
|
|
|
return $this->setUpRequest('POST', K::GRAPHQL_ENDPOINT, [
|
2020-07-28 17:46:18 -04:00
|
|
|
'body' => $body,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param array $variables
|
|
|
|
* @return array
|
|
|
|
* @throws Throwable
|
|
|
|
*/
|
2020-07-31 19:03:27 -04:00
|
|
|
public function mutate(string $name, array $variables = []): array
|
2020-07-28 17:46:18 -04:00
|
|
|
{
|
|
|
|
$request = $this->mutateRequest($name, $variables);
|
|
|
|
$response = $this->getResponseFromRequest($request);
|
|
|
|
|
|
|
|
return Json::decode(wait($response->getBody()->buffer()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make a request
|
|
|
|
*
|
2020-07-31 19:03:27 -04:00
|
|
|
* @param string $type
|
2020-07-28 17:46:18 -04:00
|
|
|
* @param string $url
|
|
|
|
* @param array $options
|
|
|
|
* @return Response
|
|
|
|
* @throws Throwable
|
|
|
|
*/
|
2020-07-31 19:03:27 -04:00
|
|
|
public function getResponse(string $type, string $url, array $options = []): Response
|
2020-07-28 17:46:18 -04:00
|
|
|
{
|
|
|
|
$logger = NULL;
|
|
|
|
if ($this->getContainer())
|
|
|
|
{
|
2020-07-31 19:03:27 -04:00
|
|
|
$logger = $this->container->getLogger('kitsu-request');
|
2020-07-28 17:46:18 -04:00
|
|
|
}
|
|
|
|
|
2020-07-31 19:03:27 -04:00
|
|
|
$request = $this->setUpRequest($type, $url, $options);
|
|
|
|
|
2020-07-28 17:46:18 -04:00
|
|
|
$response = getResponse($request);
|
|
|
|
|
2020-07-31 19:03:27 -04:00
|
|
|
if ($logger)
|
|
|
|
{
|
|
|
|
$logger->debug('Kitsu API Response', [
|
|
|
|
'status' => $response->getStatus(),
|
|
|
|
'reason' => $response->getReason(),
|
|
|
|
'body' => $response->getBody(),
|
|
|
|
'headers' => $response->getHeaders(),
|
|
|
|
'requestHeaders' => $request->getHeaders(),
|
|
|
|
]);
|
|
|
|
}
|
2020-07-28 17:46:18 -04:00
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @return Response
|
|
|
|
* @throws Throwable
|
|
|
|
*/
|
|
|
|
private function getResponseFromRequest(Request $request): Response
|
|
|
|
{
|
|
|
|
$logger = NULL;
|
|
|
|
if ($this->getContainer())
|
|
|
|
{
|
2020-07-31 19:03:27 -04:00
|
|
|
$logger = $this->container->getLogger('kitsu-request');
|
2020-07-28 17:46:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$response = getResponse($request);
|
|
|
|
|
2020-07-31 19:03:27 -04:00
|
|
|
$logger->debug('Kitsu GraphQL response', [
|
2020-07-28 17:46:18 -04:00
|
|
|
'status' => $response->getStatus(),
|
|
|
|
'reason' => $response->getReason(),
|
|
|
|
'body' => $response->getBody(),
|
|
|
|
'headers' => $response->getHeaders(),
|
|
|
|
'requestHeaders' => $request->getHeaders(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-07-31 19:03:27 -04:00
|
|
|
* Remove some boilerplate for GraphQL requests
|
2020-07-28 17:46:18 -04:00
|
|
|
*
|
|
|
|
* @param array $options
|
|
|
|
* @return array
|
2020-07-31 19:03:27 -04:00
|
|
|
* @throws \Throwable
|
2020-07-28 17:46:18 -04:00
|
|
|
*/
|
2020-07-31 19:03:27 -04:00
|
|
|
protected function graphResponse(array $options = []): array
|
2020-07-28 17:46:18 -04:00
|
|
|
{
|
2020-07-31 19:03:27 -04:00
|
|
|
$response = $this->getResponse('POST', K::GRAPHQL_ENDPOINT, $options);
|
2020-07-28 17:46:18 -04:00
|
|
|
$validResponseCodes = [200, 201];
|
|
|
|
|
|
|
|
$logger = NULL;
|
|
|
|
if ($this->getContainer())
|
|
|
|
{
|
|
|
|
$logger = $this->container->getLogger('kitsu-request');
|
2020-07-31 19:03:27 -04:00
|
|
|
$logger->debug('Kitsu GraphQL response', [
|
2020-07-28 17:46:18 -04:00
|
|
|
'status' => $response->getStatus(),
|
|
|
|
'reason' => $response->getReason(),
|
|
|
|
'body' => $response->getBody(),
|
|
|
|
'headers' => $response->getHeaders(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! \in_array($response->getStatus(), $validResponseCodes, TRUE))
|
|
|
|
{
|
|
|
|
if ($logger !== NULL)
|
|
|
|
{
|
2020-07-31 19:03:27 -04:00
|
|
|
$logger->warning('Non 200 response for GraphQL call', (array)$response->getBody());
|
2020-07-28 17:46:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Json::decode(wait($response->getBody()->buffer()));
|
|
|
|
}
|
2020-07-31 19:03:27 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make a request
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param string $url
|
|
|
|
* @param array $options
|
|
|
|
* @throws JsonException
|
|
|
|
* @throws FailedResponseException
|
|
|
|
* @throws Throwable
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function request(string $type, string $url, array $options = []): array
|
|
|
|
{
|
|
|
|
$logger = NULL;
|
|
|
|
if ($this->getContainer())
|
|
|
|
{
|
|
|
|
$logger = $this->container->getLogger('kitsu-request');
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $this->getResponse($type, $url, $options);
|
|
|
|
$statusCode = $response->getStatus();
|
|
|
|
|
|
|
|
// Check for requests that are unauthorized
|
|
|
|
if ($statusCode === 401 || $statusCode === 403)
|
|
|
|
{
|
|
|
|
Event::emit(EventType::UNAUTHORIZED);
|
|
|
|
}
|
|
|
|
|
2020-08-05 20:57:01 -04:00
|
|
|
$rawBody = wait($response->getBody()->buffer());
|
|
|
|
|
2020-07-31 19:03:27 -04:00
|
|
|
// Any other type of failed request
|
|
|
|
if ($statusCode > 299 || $statusCode < 200)
|
|
|
|
{
|
|
|
|
if ($logger)
|
|
|
|
{
|
|
|
|
$logger->warning('Non 2xx response for api call', (array)$response);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new FailedResponseException('Failed to get the proper response from the API');
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return Json::decode(wait($response->getBody()->buffer()));
|
|
|
|
}
|
|
|
|
catch (JsonException $e)
|
|
|
|
{
|
|
|
|
print_r($e);
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-08 15:48:20 -05:00
|
|
|
}
|