Version 5.1 - All the GraphQL #32

Closed
timw4mail wants to merge 1160 commits from develop into master
6 changed files with 55 additions and 81 deletions
Showing only changes of commit e7e70a1541 - Show all commits

View File

@ -20,37 +20,37 @@ use Aviat\AnimeClient\Types\AbstractType;
class MediaListEntry extends AbstractType { class MediaListEntry extends AbstractType {
/** /**
* @var int * @var int|string
*/ */
public $id; public $id;
/** /**
* @var string * @var string|null
*/ */
public $notes; public ?string $notes;
/** /**
* @var bool * @var bool
*/ */
public $private; public ?bool $private;
/** /**
* @var int * @var int
*/ */
public $progress; public int $progress;
/** /**
* @var int * @var int
*/ */
public $repeat; public ?int $repeat;
/** /**
* @var string * @var string
*/ */
public $status; public string $status;
/** /**
* @var int * @var int
*/ */
public $score; public ?int $score;
} }

View File

@ -98,8 +98,11 @@ final class Model {
if (array_key_exists('error', $data)) if (array_key_exists('error', $data))
{ {
dump($data['error']); dump([
dump($response); 'method' => __CLASS__ . '\\' . __METHOD__,
'error' => $data['error'],
'response' => $response,
]);
die(); die();
} }
@ -135,8 +138,11 @@ final class Model {
if (array_key_exists('error', $data)) if (array_key_exists('error', $data))
{ {
dump($data['error']); dump([
dump($response); 'method' => __CLASS__ . '\\' . __METHOD__,
'error' => $data['error'],
'response' => $response,
]);
die(); die();
} }

View File

@ -153,17 +153,13 @@ final class RequestBuilder extends APIRequestBuilder {
* @return array * @return array
*/ */
public function postRequest(...$args): array public function postRequest(...$args): array
{
$logger = NULL;
if ($this->getContainer())
{ {
$logger = $this->container->getLogger('kitsu-request'); $logger = $this->container->getLogger('kitsu-request');
}
$response = $this->getResponse('POST', ...$args); $response = $this->getResponse('POST', ...$args);
$validResponseCodes = [200, 201]; $validResponseCodes = [200, 201];
if ( ! in_array($response->getStatus(), $validResponseCodes, TRUE) && $logger) if ( ! in_array($response->getStatus(), $validResponseCodes, TRUE))
{ {
$logger->warning('Non 2xx response for POST api call', $response->getBody()); $logger->warning('Non 2xx response for POST api call', $response->getBody());
} }
@ -274,19 +270,11 @@ final class RequestBuilder extends APIRequestBuilder {
* @throws Throwable * @throws Throwable
*/ */
public function getResponse(string $type, string $url, array $options = []): Response public function getResponse(string $type, string $url, array $options = []): Response
{
$logger = NULL;
if ($this->getContainer())
{ {
$logger = $this->container->getLogger('kitsu-request'); $logger = $this->container->getLogger('kitsu-request');
}
$request = $this->setUpRequest($type, $url, $options); $request = $this->setUpRequest($type, $url, $options);
$response = getResponse($request); $response = getResponse($request);
if ($logger)
{
$logger->debug('Kitsu API Response', [ $logger->debug('Kitsu API Response', [
'status' => $response->getStatus(), 'status' => $response->getStatus(),
'reason' => $response->getReason(), 'reason' => $response->getReason(),
@ -294,7 +282,6 @@ final class RequestBuilder extends APIRequestBuilder {
'headers' => $response->getHeaders(), 'headers' => $response->getHeaders(),
'requestHeaders' => $request->getHeaders(), 'requestHeaders' => $request->getHeaders(),
]); ]);
}
return $response; return $response;
} }
@ -305,13 +292,8 @@ final class RequestBuilder extends APIRequestBuilder {
* @throws Throwable * @throws Throwable
*/ */
private function getResponseFromRequest(Request $request): Response private function getResponseFromRequest(Request $request): Response
{
$logger = NULL;
if ($this->getContainer())
{ {
$logger = $this->container->getLogger('kitsu-request'); $logger = $this->container->getLogger('kitsu-request');
}
$response = getResponse($request); $response = getResponse($request);
$logger->debug('Kitsu GraphQL response', [ $logger->debug('Kitsu GraphQL response', [
@ -337,9 +319,6 @@ final class RequestBuilder extends APIRequestBuilder {
$response = $this->getResponse('POST', K::GRAPHQL_ENDPOINT, $options); $response = $this->getResponse('POST', K::GRAPHQL_ENDPOINT, $options);
$validResponseCodes = [200, 201]; $validResponseCodes = [200, 201];
$logger = NULL;
if ($this->getContainer())
{
$logger = $this->container->getLogger('kitsu-request'); $logger = $this->container->getLogger('kitsu-request');
$logger->debug('Kitsu GraphQL response', [ $logger->debug('Kitsu GraphQL response', [
'status' => $response->getStatus(), 'status' => $response->getStatus(),
@ -347,15 +326,11 @@ final class RequestBuilder extends APIRequestBuilder {
'body' => $response->getBody(), 'body' => $response->getBody(),
'headers' => $response->getHeaders(), 'headers' => $response->getHeaders(),
]); ]);
}
if ( ! \in_array($response->getStatus(), $validResponseCodes, TRUE)) if ( ! \in_array($response->getStatus(), $validResponseCodes, TRUE))
{
if ($logger !== NULL)
{ {
$logger->warning('Non 200 response for GraphQL call', (array)$response->getBody()); $logger->warning('Non 200 response for GraphQL call', (array)$response->getBody());
} }
}
return Json::decode(wait($response->getBody()->buffer())); return Json::decode(wait($response->getBody()->buffer()));
} }
@ -372,13 +347,8 @@ final class RequestBuilder extends APIRequestBuilder {
* @return array * @return array
*/ */
private function request(string $type, string $url, array $options = []): array private function request(string $type, string $url, array $options = []): array
{
$logger = NULL;
if ($this->getContainer())
{ {
$logger = $this->container->getLogger('kitsu-request'); $logger = $this->container->getLogger('kitsu-request');
}
$response = $this->getResponse($type, $url, $options); $response = $this->getResponse($type, $url, $options);
$statusCode = $response->getStatus(); $statusCode = $response->getStatus();

View File

@ -105,7 +105,7 @@ final class SyncLists extends BaseCommand {
if ( ! $anilistEnabled) if ( ! $anilistEnabled)
{ {
$this->echoErrorBox('Anlist API is not enabled. Can not sync.'); $this->echoErrorBox('Anlist API is not enabled. Can not sync.');
die(); exit();
} }
// Authentication is required to update Kitsu // Authentication is required to update Kitsu
@ -296,17 +296,7 @@ final class SyncLists extends BaseCommand {
{ {
$uType = ucfirst($type); $uType = ucfirst($type);
$kitsuCount = 0; return $this->kitsuModel->{"get{$uType}ListCount"}() ?? 0;
try
{
$kitsuCount = $this->kitsuModel->{"get{$uType}ListCount"}();
}
catch (FailedResponseException $e)
{
dump($e);
}
return $kitsuCount;
} }
private function fetchKitsu(string $type): array private function fetchKitsu(string $type): array
@ -327,7 +317,10 @@ final class SyncLists extends BaseCommand {
if ( ! array_key_exists('included', $data)) if ( ! array_key_exists('included', $data))
{ {
dump($data); dump([
'problem' => 'Missing included data in method ' . __METHOD__,
'data' => $data
]);
return []; return [];
} }
@ -742,7 +735,11 @@ final class SyncLists extends BaseCommand {
} }
} }
dump($responseData); dump([
'problem' => 'Failed to update kitsu list item',
'syncDate' => $itemsToUpdate[$key],
'responseData' => $responseData,
]);
$verb = ($action === SyncAction::UPDATE) ? SyncAction::UPDATE : SyncAction::CREATE; $verb = ($action === SyncAction::UPDATE) ? SyncAction::UPDATE : SyncAction::CREATE;
$this->echoError("Failed to {$verb} Kitsu {$type} list item with id: {$id}"); $this->echoError("Failed to {$verb} Kitsu {$type} list item with id: {$id}");
@ -799,7 +796,11 @@ final class SyncLists extends BaseCommand {
} }
else else
{ {
dump($responseData); dump([
'problem' => 'Failed to update anilist list item',
'syncDate' => $itemsToUpdate[$key],
'responseData' => $responseData,
]);
$verb = ($action === SyncAction::UPDATE) ? SyncAction::UPDATE : SyncAction::CREATE; $verb = ($action === SyncAction::UPDATE) ? SyncAction::UPDATE : SyncAction::CREATE;
$this->echoError("Failed to {$verb} Anilist {$type} list item with id: {$id}"); $this->echoError("Failed to {$verb} Anilist {$type} list item with id: {$id}");
} }

View File

@ -77,9 +77,6 @@ final class UpdateThumbnails extends ClearThumbnails {
$includes = JsonAPI::organizeIncludes($animeList['included']); $includes = JsonAPI::organizeIncludes($animeList['included']);
$animeIds = array_keys($includes['anime']); $animeIds = array_keys($includes['anime']);
// print_r($mangaIds);
// die();
return [ return [
'anime' => $animeIds, 'anime' => $animeIds,
'manga' => $mangaIds, 'manga' => $mangaIds,

View File

@ -267,7 +267,7 @@ final class Anime extends BaseController {
if (empty($data)) if (empty($data))
{ {
$this->errorPage(400, 'Bad Request', ''); $this->errorPage(400, 'Bad Request', '');
die(); exit();
} }
$response = $this->model->incrementLibraryItem(FormItem::from($data)); $response = $this->model->incrementLibraryItem(FormItem::from($data));