2016-12-21 12:46:20 -05:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
2017-02-15 16:13:32 -05:00
|
|
|
* Hummingbird Anime List Client
|
2016-12-21 12:46:20 -05:00
|
|
|
*
|
2018-08-22 13:48:27 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2016-12-21 12:46:20 -05:00
|
|
|
*
|
2020-04-10 15:39:39 -04:00
|
|
|
* PHP version 7.4
|
2016-12-21 12:46:20 -05:00
|
|
|
*
|
2017-02-15 16:13:32 -05:00
|
|
|
* @package HummingbirdAnimeClient
|
2017-01-06 23:34:56 -05:00
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2020-01-08 15:39:49 -05:00
|
|
|
* @copyright 2015 - 2020 Timothy J. Warren
|
2017-01-06 23:34:56 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-04-10 15:39:39 -04:00
|
|
|
* @version 5
|
2017-03-07 20:53:58 -05:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
2017-01-11 10:34:24 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\API\Kitsu;
|
2016-12-21 12:46:20 -05:00
|
|
|
|
2017-02-08 15:48:20 -05:00
|
|
|
use const Aviat\AnimeClient\SESSION_SEGMENT;
|
|
|
|
|
2017-12-08 22:32:00 -05:00
|
|
|
use function Amp\Promise\wait;
|
2018-11-29 11:00:50 -05:00
|
|
|
use function Aviat\AnimeClient\getResponse;
|
2017-02-08 15:48:20 -05:00
|
|
|
|
2020-03-11 16:26:17 -04:00
|
|
|
use Amp\Http\Client\Request;
|
|
|
|
use Amp\Http\Client\Response;
|
2017-12-08 22:32:00 -05:00
|
|
|
use Aviat\AnimeClient\API\{
|
|
|
|
FailedResponseException,
|
|
|
|
Kitsu as K
|
|
|
|
};
|
2016-12-22 21:36:23 -05:00
|
|
|
use Aviat\Ion\Json;
|
2017-12-08 22:32:00 -05:00
|
|
|
use Aviat\Ion\JsonException;
|
2016-12-21 12:46:20 -05:00
|
|
|
|
2019-12-09 14:34:23 -05:00
|
|
|
use Throwable;
|
|
|
|
|
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
|
|
|
/**
|
2018-08-22 13:43:04 -04:00
|
|
|
* The request builder for the Kitsu API
|
2017-03-20 19:08:33 -04:00
|
|
|
* @var KitsuRequestBuilder
|
2017-02-07 13:11:42 -05:00
|
|
|
*/
|
2020-04-10 20:01:46 -04:00
|
|
|
protected KitsuRequestBuilder $requestBuilder;
|
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
|
|
|
/**
|
2017-02-08 15:48:20 -05:00
|
|
|
* Create a request object
|
2016-12-22 21:36:23 -05:00
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param string $url
|
|
|
|
* @param array $options
|
2019-12-09 14:34:23 -05:00
|
|
|
* @return Request
|
2016-12-22 21:36:23 -05:00
|
|
|
*/
|
2017-02-14 15:29:13 -05:00
|
|
|
public function setUpRequest(string $type, string $url, array $options = []): Request
|
2016-12-22 21:36:23 -05:00
|
|
|
{
|
2017-02-08 15:48:20 -05:00
|
|
|
$request = $this->requestBuilder->newRequest($type, $url);
|
2016-12-22 21:36:23 -05:00
|
|
|
|
2017-01-09 20:36:48 -05:00
|
|
|
$sessionSegment = $this->getContainer()
|
|
|
|
->get('session')
|
2017-02-08 15:48:20 -05:00
|
|
|
->getSegment(SESSION_SEGMENT);
|
2017-01-05 13:41:32 -05:00
|
|
|
|
2017-03-22 11:41:25 -04:00
|
|
|
$cache = $this->getContainer()->get('cache');
|
|
|
|
$cacheItem = $cache->getItem('kitsu-auth-token');
|
|
|
|
$token = null;
|
|
|
|
|
|
|
|
|
2017-02-17 11:37:22 -05:00
|
|
|
if ($sessionSegment->get('auth_token') !== NULL && $url !== K::AUTH_URL)
|
2017-01-09 20:36:48 -05:00
|
|
|
{
|
|
|
|
$token = $sessionSegment->get('auth_token');
|
2017-03-22 12:28:19 -04:00
|
|
|
if ( ! $cacheItem->isHit())
|
|
|
|
{
|
|
|
|
$cacheItem->set($token);
|
|
|
|
$cacheItem->save();
|
|
|
|
}
|
2017-03-22 11:41:25 -04:00
|
|
|
}
|
|
|
|
else if ($sessionSegment->get('auth_token') === NULL && $cacheItem->isHit())
|
|
|
|
{
|
|
|
|
$token = $cacheItem->get();
|
|
|
|
}
|
|
|
|
|
2017-12-08 22:32:00 -05:00
|
|
|
if (NULL !== $token)
|
2017-03-22 11:41:25 -04:00
|
|
|
{
|
2017-02-08 15:48:20 -05:00
|
|
|
$request = $request->setAuth('bearer', $token);
|
|
|
|
}
|
2017-03-22 11:41:25 -04:00
|
|
|
|
2017-02-09 20:10:13 -05:00
|
|
|
if (array_key_exists('form_params', $options))
|
|
|
|
{
|
2017-12-08 22:32:00 -05:00
|
|
|
$request = $request->setFormFields($options['form_params']);
|
2017-02-09 20:10:13 -05:00
|
|
|
}
|
2017-02-08 15:48:20 -05:00
|
|
|
|
|
|
|
if (array_key_exists('query', $options))
|
|
|
|
{
|
2017-12-08 22:32:00 -05:00
|
|
|
$request = $request->setQuery($options['query']);
|
2017-02-08 15:48:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('body', $options))
|
|
|
|
{
|
2017-12-08 22:32:00 -05:00
|
|
|
$request = $request->setJsonBody($options['body']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('headers', $options))
|
|
|
|
{
|
|
|
|
$request = $request->setHeaders($options['headers']);
|
2017-01-05 13:41:32 -05:00
|
|
|
}
|
|
|
|
|
2017-02-08 15:48:20 -05:00
|
|
|
return $request->getFullRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make a request
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param string $url
|
|
|
|
* @param array $options
|
|
|
|
* @return Response
|
2019-12-09 14:34:23 -05:00
|
|
|
* @throws Throwable
|
2017-02-08 15:48:20 -05:00
|
|
|
*/
|
2018-11-09 10:38:35 -05:00
|
|
|
private function getResponse(string $type, string $url, array $options = []): Response
|
2017-02-08 15:48:20 -05:00
|
|
|
{
|
2017-12-08 22:32:00 -05:00
|
|
|
$logger = NULL;
|
|
|
|
if ($this->getContainer())
|
|
|
|
{
|
|
|
|
$logger = $this->container->getLogger('kitsu-request');
|
|
|
|
}
|
|
|
|
|
2017-02-08 15:48:20 -05:00
|
|
|
$request = $this->setUpRequest($type, $url, $options);
|
2016-12-22 21:36:23 -05:00
|
|
|
|
2018-11-29 11:00:50 -05:00
|
|
|
$response = getResponse($request);
|
2017-12-08 22:32:00 -05:00
|
|
|
|
|
|
|
if ($logger)
|
|
|
|
{
|
|
|
|
$logger->debug('Kitsu API Response', [
|
|
|
|
'response_status' => $response->getStatus(),
|
|
|
|
'request_headers' => $response->getOriginalRequest()->getHeaders(),
|
|
|
|
'response_headers' => $response->getHeaders()
|
|
|
|
]);
|
|
|
|
}
|
2017-01-09 20:36:48 -05:00
|
|
|
|
2017-02-04 15:18:34 -05:00
|
|
|
return $response;
|
2017-01-06 21:39:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-08 15:48:20 -05:00
|
|
|
* Make a request
|
2017-01-06 21:39:01 -05:00
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param string $url
|
|
|
|
* @param array $options
|
2019-12-09 14:34:23 -05:00
|
|
|
* @throws JsonException
|
2017-12-08 22:32:00 -05:00
|
|
|
* @throws FailedResponseException
|
2019-12-09 14:34:23 -05:00
|
|
|
* @throws Throwable
|
2017-01-06 21:39:01 -05:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function request(string $type, string $url, array $options = []): array
|
|
|
|
{
|
2017-02-17 11:37:22 -05:00
|
|
|
$logger = NULL;
|
2017-01-06 21:39:01 -05:00
|
|
|
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
|
|
|
|
2019-12-09 14:34:23 -05:00
|
|
|
if ((int) $response->getStatus() > 299 || (int) $response->getStatus() < 200)
|
2016-12-22 21:36:23 -05:00
|
|
|
{
|
|
|
|
if ($logger)
|
|
|
|
{
|
2017-04-05 13:01:51 -04:00
|
|
|
$logger->warning('Non 200 response for api call', (array)$response);
|
2016-12-22 21:36:23 -05:00
|
|
|
}
|
2017-04-05 13:01:51 -04:00
|
|
|
|
2020-04-21 19:22:56 -04:00
|
|
|
// throw new FailedResponseException('Failed to get the proper response from the API');
|
2016-12-22 21:36:23 -05:00
|
|
|
}
|
|
|
|
|
2017-12-08 22:32:00 -05:00
|
|
|
try
|
|
|
|
{
|
2020-03-11 16:26:17 -04:00
|
|
|
return Json::decode(wait($response->getBody()->buffer()));
|
2017-12-08 22:32:00 -05:00
|
|
|
}
|
|
|
|
catch (JsonException $e)
|
|
|
|
{
|
|
|
|
print_r($e);
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
2016-12-22 21:36:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove some boilerplate for get requests
|
|
|
|
*
|
2017-02-17 11:37:22 -05:00
|
|
|
* @param mixed ...$args
|
2019-12-09 14:34:23 -05:00
|
|
|
* @throws Throwable
|
2016-12-22 21:36:23 -05:00
|
|
|
* @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
|
|
|
|
*
|
2017-02-17 11:37:22 -05:00
|
|
|
* @param mixed ...$args
|
2019-12-09 14:34:23 -05:00
|
|
|
* @throws Throwable
|
2017-01-06 21:39:01 -05:00
|
|
|
* @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
|
|
|
*
|
2017-02-17 11:37:22 -05:00
|
|
|
* @param mixed ...$args
|
2019-12-09 14:34:23 -05:00
|
|
|
* @throws Throwable
|
2016-12-22 21:36:23 -05:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function postRequest(...$args): array
|
|
|
|
{
|
2017-02-17 11:37:22 -05:00
|
|
|
$logger = NULL;
|
2017-01-06 21:39:01 -05:00
|
|
|
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];
|
|
|
|
|
2018-01-18 16:21:45 -05:00
|
|
|
if ( ! \in_array((int) $response->getStatus(), $validResponseCodes, TRUE))
|
2017-01-06 21:39:01 -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-06 21:39:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-11 16:26:17 -04:00
|
|
|
return JSON::decode(wait($response->getBody()->buffer()), TRUE);
|
2016-12-22 21:36:23 -05:00
|
|
|
}
|
2017-01-03 20:29:43 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove some boilerplate for delete requests
|
|
|
|
*
|
2017-02-17 11:37:22 -05:00
|
|
|
* @param mixed ...$args
|
2019-12-09 14:34:23 -05:00
|
|
|
* @throws Throwable
|
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);
|
2017-02-08 15:48:20 -05:00
|
|
|
return ((int) $response->getStatus() === 204);
|
2017-01-03 20:29:43 -05:00
|
|
|
}
|
2016-12-21 12:46:20 -05:00
|
|
|
}
|