From bd61bd734be3b04524fcbbbe03ebe9d09f0d09d1 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Wed, 10 Jan 2018 16:34:25 -0500 Subject: [PATCH] Simplify syncing script --- src/API/APIRequestBuilder.php | 17 ++++++++++++++++- src/Command/SyncKitsuWithMal.php | 15 ++++++--------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/API/APIRequestBuilder.php b/src/API/APIRequestBuilder.php index 17b91060..fa975b0c 100644 --- a/src/API/APIRequestBuilder.php +++ b/src/API/APIRequestBuilder.php @@ -121,7 +121,7 @@ class APIRequestBuilder { return $this->setBody($body); } - + /** * Unset a request header * @@ -227,6 +227,21 @@ class APIRequestBuilder { return $this->request; } + /** + * Get the data from the response of the passed request + * + * @param Request $request + * @return mixed + * @throws \Error + * @throws \Throwable + * @throws \TypeError + */ + public function getResponseData(Request $request) + { + $response = wait((new HummingbirdClient)->request($request)); + return wait($response->getBody()); + } + /** * Create a new http request * diff --git a/src/Command/SyncKitsuWithMal.php b/src/Command/SyncKitsuWithMal.php index 1f7d9887..be3ee4c2 100644 --- a/src/Command/SyncKitsuWithMal.php +++ b/src/Command/SyncKitsuWithMal.php @@ -16,9 +16,6 @@ namespace Aviat\AnimeClient\Command; -use function Amp\Promise\{all, wait}; - -use Amp\Artax\Client; use Aviat\AnimeClient\API\{ FailedResponseException, JsonAPI, @@ -27,8 +24,7 @@ use Aviat\AnimeClient\API\{ Mapping\MangaReadingStatus }; use Aviat\AnimeClient\API\MAL\Transformer\{ - AnimeListTransformer as ALT, - MangaListTransformer as MLT + AnimeListTransformer as ALT }; use Aviat\Ion\Json; @@ -78,9 +74,10 @@ class SyncKitsuWithMal extends BaseCommand { $malCount = 0; if ( ! empty($malList)) { - $malCount = (array_key_exists(0, $malList)) - ? count($malList) - : count([$malList]); + $malCount = count(array_key_exists(0, $malList) + ? $malList + : [$malList] + ); } try @@ -485,7 +482,7 @@ class SyncKitsuWithMal extends BaseCommand { $responseData = Json::decode($response); $id = $itemsToUpdate[$key]['mal_id']; - if ( ! array_key_exists('errors', $responseData)); + if ( ! array_key_exists('errors', $responseData)) { $verb = ($action === 'update') ? 'updated' : 'created'; $this->echoBox("Successfully {$verb} Kitsu {$type} list item with id: {$id}");