Use new ParallelAPIRequest class

This commit is contained in:
Timothy Warren 2017-02-20 13:37:08 -05:00
parent 93038e61e5
commit 9f0484a93b
4 changed files with 55 additions and 24 deletions

View File

@ -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,

View File

@ -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;
}

View File

@ -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;
}

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);
}
}