Use new ParallelAPIRequest class

This commit is contained in:
Timothy Warren 2017-02-20 13:37:08 -05:00
parent fdd7f91835
commit 96fce9a6d5
4 changed files with 55 additions and 24 deletions

View File

@ -17,9 +17,11 @@
namespace Aviat\AnimeClient\API\Kitsu; namespace Aviat\AnimeClient\API\Kitsu;
use Amp\Artax\Request; use Amp\Artax\Request;
use Aviat\AnimeClient\API\CacheTrait; use Aviat\AnimeClient\API\{
use Aviat\AnimeClient\API\JsonAPI; CacheTrait,
use Aviat\AnimeClient\API\Kitsu as K; JsonAPI,
Kitsu as K
};
use Aviat\AnimeClient\API\Kitsu\Transformer\{ use Aviat\AnimeClient\API\Kitsu\Transformer\{
AnimeTransformer, AnimeTransformer,
AnimeListTransformer, AnimeListTransformer,

View File

@ -16,8 +16,7 @@
namespace Aviat\AnimeClient\API; namespace Aviat\AnimeClient\API;
use function Amp\{all, some, wait}; use Amp;
use Amp\Artax\Client; use Amp\Artax\Client;
/** /**
@ -74,9 +73,9 @@ class ParallelAPIRequest {
$client = new Client(); $client = new Client();
$promises = $client->requestMulti($this->requests); $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; return $results;
} }

View File

@ -20,6 +20,7 @@ use function Amp\some;
use function Amp\wait; use function Amp\wait;
use Amp\Artax\Client; use Amp\Artax\Client;
use Aviat\AnimeClient\API\ParallelAPIRequest;
use Aviat\AnimeClient\API\Kitsu\Enum\AnimeWatchingStatus; use Aviat\AnimeClient\API\Kitsu\Enum\AnimeWatchingStatus;
use Aviat\Ion\Di\ContainerInterface; use Aviat\Ion\Di\ContainerInterface;
use Aviat\Ion\Json; use Aviat\Ion\Json;
@ -153,7 +154,7 @@ class Anime extends API {
*/ */
public function createLibraryItem(array $data): bool public function createLibraryItem(array $data): bool
{ {
$requests = []; $requester = new ParallelAPIRequest();
if ($this->useMALAPI) if ($this->useMALAPI)
{ {
@ -163,15 +164,13 @@ class Anime extends API {
if ( ! is_null($malId)) if ( ! is_null($malId))
{ {
$malData['id'] = $malId; $malData['id'] = $malId;
$requests['mal'] = $this->malModel->createListItem($malData); $requester->addRequest($this->malModel->createListItem($malData), 'mal');
} }
} }
$requests['kitsu'] = $this->kitsuModel->createListItem($data); $requester->addRequest($this->kitsuModel->createListItem($data), 'kitsu');
$promises = (new Client)->requestMulti($requests); $results = $requester->makeRequests(TRUE);
$results = wait(some($promises));
return count($results[1]) > 0; return count($results[1]) > 0;
} }
@ -184,18 +183,16 @@ class Anime extends API {
*/ */
public function updateLibraryItem(array $data): array public function updateLibraryItem(array $data): array
{ {
$requests = []; $requester = new ParallelAPIRequest();
if ($this->useMALAPI) if ($this->useMALAPI)
{ {
$requests['mal'] = $this->malModel->updateListItem($data); $requester->addRequest($this->malModel->updateListItem($data), 'mal');
} }
$requests['kitsu'] = $this->kitsuModel->updateListItem($data); $requester->addRequest($this->kitsuModel->updateListItem($data), 'kitsu');
$promises = (new Client)->requestMulti($requests); $results = $requester->makeRequests(TRUE);
$results = wait(some($promises));
return [ return [
'body' => Json::decode($results[1]['kitsu']->getBody()), 'body' => Json::decode($results[1]['kitsu']->getBody()),
@ -212,16 +209,16 @@ class Anime extends API {
*/ */
public function deleteLibraryItem(string $id, string $malId = NULL): bool public function deleteLibraryItem(string $id, string $malId = NULL): bool
{ {
$requests = []; $requester = new ParallelAPIRequest();
if ($this->useMALAPI && ! is_null($malId)) 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; return count($results[1]) > 0;
} }

View File

@ -0,0 +1,33 @@
<?php
namespace Aviat\AnimeClient\Tests\API;
use Aviat\AnimeClient\API\{APIRequestBuilder, ParallelAPIRequest};
use Aviat\Ion\Friend;
use PHPUnit\Framework\TestCase;
class ParallelAPIRequestsTest extends TestCase {
public function testAddStringUrlRequest()
{
$requester = new ParallelAPIRequest();
$friend = new Friend($requester);
$friend->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);
}
}