From 96fce9a6d5bfc01cfe442b13b425419ca2b1af95 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Mon, 20 Feb 2017 13:37:08 -0500 Subject: [PATCH] Use new ParallelAPIRequest class --- src/API/Kitsu/Model.php | 8 ++++--- src/API/ParallelAPIRequest.php | 7 +++--- src/Model/Anime.php | 31 ++++++++++++-------------- tests/API/ParallelAPIRequestTest.php | 33 ++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 24 deletions(-) create mode 100644 tests/API/ParallelAPIRequestTest.php diff --git a/src/API/Kitsu/Model.php b/src/API/Kitsu/Model.php index 8c1a98f1..02bd5e60 100644 --- a/src/API/Kitsu/Model.php +++ b/src/API/Kitsu/Model.php @@ -17,9 +17,11 @@ namespace Aviat\AnimeClient\API\Kitsu; use Amp\Artax\Request; -use Aviat\AnimeClient\API\CacheTrait; -use Aviat\AnimeClient\API\JsonAPI; -use Aviat\AnimeClient\API\Kitsu as K; +use Aviat\AnimeClient\API\{ + CacheTrait, + JsonAPI, + Kitsu as K +}; use Aviat\AnimeClient\API\Kitsu\Transformer\{ AnimeTransformer, AnimeListTransformer, diff --git a/src/API/ParallelAPIRequest.php b/src/API/ParallelAPIRequest.php index 3d2f974f..f47c9d6f 100644 --- a/src/API/ParallelAPIRequest.php +++ b/src/API/ParallelAPIRequest.php @@ -16,8 +16,7 @@ namespace Aviat\AnimeClient\API; -use function Amp\{all, some, wait}; - +use Amp; use Amp\Artax\Client; /** @@ -74,9 +73,9 @@ class ParallelAPIRequest { $client = new Client(); $promises = $client->requestMulti($this->requests); - $func = ($allowFailingRequests) ? 'some' : 'all'; + $func = ($allowFailingRequests) ? '\Amp\some' : '\Amp\all'; - $results = wait($func($promises)); + $results = Amp\wait($func($promises)); return $results; } diff --git a/src/Model/Anime.php b/src/Model/Anime.php index 931acb1a..b73d32d9 100644 --- a/src/Model/Anime.php +++ b/src/Model/Anime.php @@ -20,6 +20,7 @@ use function Amp\some; use function Amp\wait; use Amp\Artax\Client; +use Aviat\AnimeClient\API\ParallelAPIRequest; use Aviat\AnimeClient\API\Kitsu\Enum\AnimeWatchingStatus; use Aviat\Ion\Di\ContainerInterface; use Aviat\Ion\Json; @@ -153,7 +154,7 @@ class Anime extends API { */ public function createLibraryItem(array $data): bool { - $requests = []; + $requester = new ParallelAPIRequest(); if ($this->useMALAPI) { @@ -163,15 +164,13 @@ class Anime extends API { if ( ! is_null($malId)) { $malData['id'] = $malId; - $requests['mal'] = $this->malModel->createListItem($malData); + $requester->addRequest($this->malModel->createListItem($malData), 'mal'); } } + + $requester->addRequest($this->kitsuModel->createListItem($data), 'kitsu'); - $requests['kitsu'] = $this->kitsuModel->createListItem($data); - - $promises = (new Client)->requestMulti($requests); - - $results = wait(some($promises)); + $results = $requester->makeRequests(TRUE); return count($results[1]) > 0; } @@ -184,18 +183,16 @@ class Anime extends API { */ public function updateLibraryItem(array $data): array { - $requests = []; + $requester = new ParallelAPIRequest(); if ($this->useMALAPI) { - $requests['mal'] = $this->malModel->updateListItem($data); + $requester->addRequest($this->malModel->updateListItem($data), 'mal'); } - $requests['kitsu'] = $this->kitsuModel->updateListItem($data); - - $promises = (new Client)->requestMulti($requests); + $requester->addRequest($this->kitsuModel->updateListItem($data), 'kitsu'); - $results = wait(some($promises)); + $results = $requester->makeRequests(TRUE); return [ 'body' => Json::decode($results[1]['kitsu']->getBody()), @@ -212,16 +209,16 @@ class Anime extends API { */ public function deleteLibraryItem(string $id, string $malId = NULL): bool { - $requests = []; + $requester = new ParallelAPIRequest(); if ($this->useMALAPI && ! is_null($malId)) { - $requests['mal'] = $this->malModel->deleteListItem($malId); + $requester->addRequest($this->malModel->deleteListItem($malId), 'MAL'); } - $requests['kitsu'] = $this->kitsuModel->deleteListItem($id); + $requester->addRequest($this->kitsuModel->deleteListItem($id), 'kitsu'); - $results = wait(some((new Client)->requestMulti($requests))); + $results = $requester->makeRequests(TRUE); return count($results[1]) > 0; } diff --git a/tests/API/ParallelAPIRequestTest.php b/tests/API/ParallelAPIRequestTest.php new file mode 100644 index 00000000..e0d33945 --- /dev/null +++ b/tests/API/ParallelAPIRequestTest.php @@ -0,0 +1,33 @@ +addRequest('https://httpbin.org'); + + $this->assertEquals($friend->requests, ['https://httpbin.org']); + } + + public function testAddStringUrlRequests() + { + $requests = [ + 'foo' => 'http://example.com', + 'bar' => 'https://example.com' + ]; + + $requester = new ParallelAPIRequest(); + $friend = new Friend($requester); + $friend->addRequests($requests); + + $this->assertEquals($friend->requests, $requests); + } +} \ No newline at end of file