All in GraphQL #34

Merged
timw4mail merged 87 commits from develop into master 2020-12-01 10:07:49 -05:00
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

@ -154,16 +154,12 @@ final class RequestBuilder extends APIRequestBuilder {
*/ */
public function postRequest(...$args): array public function postRequest(...$args): array
{ {
$logger = NULL; $logger = $this->container->getLogger('kitsu-request');
if ($this->getContainer())
{
$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());
} }
@ -275,26 +271,17 @@ final class RequestBuilder extends APIRequestBuilder {
*/ */
public function getResponse(string $type, string $url, array $options = []): Response public function getResponse(string $type, string $url, array $options = []): Response
{ {
$logger = NULL; $logger = $this->container->getLogger('kitsu-request');
if ($this->getContainer())
{
$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', [
{ 'status' => $response->getStatus(),
$logger->debug('Kitsu API Response', [ 'reason' => $response->getReason(),
'status' => $response->getStatus(), 'body' => $response->getBody(),
'reason' => $response->getReason(), 'headers' => $response->getHeaders(),
'body' => $response->getBody(), 'requestHeaders' => $request->getHeaders(),
'headers' => $response->getHeaders(), ]);
'requestHeaders' => $request->getHeaders(),
]);
}
return $response; return $response;
} }
@ -306,12 +293,7 @@ final class RequestBuilder extends APIRequestBuilder {
*/ */
private function getResponseFromRequest(Request $request): Response private function getResponseFromRequest(Request $request): Response
{ {
$logger = NULL; $logger = $this->container->getLogger('kitsu-request');
if ($this->getContainer())
{
$logger = $this->container->getLogger('kitsu-request');
}
$response = getResponse($request); $response = getResponse($request);
$logger->debug('Kitsu GraphQL response', [ $logger->debug('Kitsu GraphQL response', [
@ -337,24 +319,17 @@ 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; $logger = $this->container->getLogger('kitsu-request');
if ($this->getContainer()) $logger->debug('Kitsu GraphQL response', [
{ 'status' => $response->getStatus(),
$logger = $this->container->getLogger('kitsu-request'); 'reason' => $response->getReason(),
$logger->debug('Kitsu GraphQL response', [ 'body' => $response->getBody(),
'status' => $response->getStatus(), 'headers' => $response->getHeaders(),
'reason' => $response->getReason(), ]);
'body' => $response->getBody(),
'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()));
@ -373,12 +348,7 @@ final class RequestBuilder extends APIRequestBuilder {
*/ */
private function request(string $type, string $url, array $options = []): array private function request(string $type, string $url, array $options = []): array
{ {
$logger = NULL; $logger = $this->container->getLogger('kitsu-request');
if ($this->getContainer())
{
$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));