2017-02-17 08:25:19 -05:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Hummingbird Anime List Client
|
|
|
|
*
|
2018-08-22 13:48:27 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2017-02-17 08:25:19 -05:00
|
|
|
*
|
2019-12-03 15:17:25 -05:00
|
|
|
* PHP version 7.2
|
2017-02-17 08:25:19 -05:00
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2019-12-06 09:16:35 -05:00
|
|
|
* @copyright 2015 - 2019 Timothy J. Warren
|
2017-02-17 08:25:19 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2019-12-06 09:16:35 -05:00
|
|
|
* @version 4.2
|
2017-03-07 20:53:58 -05:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
2017-02-17 08:25:19 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\API;
|
|
|
|
|
2019-12-06 09:15:49 -05:00
|
|
|
use Amp\Artax\Request;
|
2017-12-08 22:32:00 -05:00
|
|
|
use function Amp\call;
|
|
|
|
use function Amp\Promise\{all, wait};
|
2018-12-07 10:24:42 -05:00
|
|
|
use function Aviat\AnimeClient\getApiClient;
|
2018-11-29 11:00:50 -05:00
|
|
|
|
2017-02-17 08:25:19 -05:00
|
|
|
/**
|
|
|
|
* Class to simplify making and validating simultaneous requests
|
|
|
|
*/
|
2018-08-08 10:12:45 -04:00
|
|
|
final class ParallelAPIRequest {
|
2017-12-08 22:32:00 -05:00
|
|
|
|
2017-02-17 08:25:19 -05:00
|
|
|
/**
|
|
|
|
* Set of requests to make in parallel
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-08-08 10:12:45 -04:00
|
|
|
private $requests = [];
|
2017-12-08 22:32:00 -05:00
|
|
|
|
2017-02-17 08:25:19 -05:00
|
|
|
/**
|
|
|
|
* Add a request
|
|
|
|
*
|
2019-12-06 09:15:49 -05:00
|
|
|
* @param string|Request $request
|
2017-02-17 08:25:19 -05:00
|
|
|
* @param string|number $key
|
|
|
|
* @return self
|
|
|
|
*/
|
2017-02-17 10:55:17 -05:00
|
|
|
public function addRequest($request, $key = NULL): self
|
2017-02-17 08:25:19 -05:00
|
|
|
{
|
2018-02-02 09:50:58 -05:00
|
|
|
if ($key !== NULL)
|
2017-02-17 08:25:19 -05:00
|
|
|
{
|
|
|
|
$this->requests[$key] = $request;
|
|
|
|
return $this;
|
|
|
|
}
|
2017-12-08 22:32:00 -05:00
|
|
|
|
2017-02-17 08:25:19 -05:00
|
|
|
$this->requests[] = $request;
|
|
|
|
return $this;
|
|
|
|
}
|
2017-12-08 22:32:00 -05:00
|
|
|
|
2017-02-17 08:25:19 -05:00
|
|
|
/**
|
|
|
|
* Add multiple requests
|
|
|
|
*
|
2019-12-06 09:15:49 -05:00
|
|
|
* @param string[]|Request[] $requests
|
2017-02-17 08:25:19 -05:00
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function addRequests(array $requests): self
|
|
|
|
{
|
|
|
|
array_walk($requests, [$this, 'addRequest']);
|
|
|
|
return $this;
|
|
|
|
}
|
2017-12-08 22:32:00 -05:00
|
|
|
|
2017-02-17 08:25:19 -05:00
|
|
|
/**
|
2018-10-01 10:50:22 -04:00
|
|
|
* Make the requests, and return the body for each
|
2017-02-17 08:25:19 -05:00
|
|
|
*
|
2017-12-08 22:32:00 -05:00
|
|
|
* @return array
|
2018-09-26 22:31:04 -04:00
|
|
|
* @throws \Throwable
|
2017-02-17 08:25:19 -05:00
|
|
|
*/
|
2017-12-08 22:32:00 -05:00
|
|
|
public function makeRequests(): array
|
2017-02-17 08:25:19 -05:00
|
|
|
{
|
2018-12-07 10:24:42 -05:00
|
|
|
$client = getApiClient();
|
2018-12-06 16:21:02 -05:00
|
|
|
|
2017-12-08 22:32:00 -05:00
|
|
|
$promises = [];
|
|
|
|
|
|
|
|
foreach ($this->requests as $key => $url)
|
|
|
|
{
|
2019-12-06 09:15:49 -05:00
|
|
|
$promises[$key] = call(static function () use ($client, $url) {
|
2017-12-08 22:32:00 -05:00
|
|
|
$response = yield $client->request($url);
|
2018-08-08 10:12:45 -04:00
|
|
|
return yield $response->getBody();
|
2017-12-08 22:32:00 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return wait(all($promises));
|
2017-02-17 08:25:19 -05:00
|
|
|
}
|
2018-10-01 10:50:22 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make the requests and return the response objects
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
|
|
|
public function getResponses(): array
|
|
|
|
{
|
2018-12-07 10:24:42 -05:00
|
|
|
$client = getApiClient();
|
2018-12-06 16:21:02 -05:00
|
|
|
|
2018-10-01 10:50:22 -04:00
|
|
|
$promises = [];
|
|
|
|
|
|
|
|
foreach ($this->requests as $key => $url)
|
|
|
|
{
|
2019-12-06 09:15:49 -05:00
|
|
|
$promises[$key] = call(static function () use ($client, $url) {
|
2018-10-01 10:50:22 -04:00
|
|
|
return yield $client->request($url);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return wait(all($promises));
|
|
|
|
}
|
2017-02-17 08:25:19 -05:00
|
|
|
}
|