HummingBirdAnimeClient/src/AnimeClient/API/ParallelAPIRequest.php

165 lines
3.1 KiB
PHP
Raw Normal View History

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
*
2021-02-04 11:57:01 -05:00
* PHP version 8
2017-02-17 08:25:19 -05:00
*
2022-03-04 15:50:35 -05:00
* @copyright 2015 - 2022 Timothy J. Warren <tim@timshome.page>
2017-02-17 08:25:19 -05:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2020-12-10 17:06:50 -05:00
* @version 5.2
2022-03-04 15:50:35 -05:00
* @link https://git.timshome.page/timw4mail/HummingBirdAnimeClient
2017-02-17 08:25:19 -05:00
*/
namespace Aviat\AnimeClient\API;
2023-07-13 11:06:52 -04:00
use Amp\Http\Client\{HttpException, Request};
use Generator;
use Throwable;
use function Amp\call;
2023-07-13 11:06:52 -04:00
// use function Amp\Future\{async, await};
use function Amp\Promise\{all, wait};
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
*/
final class ParallelAPIRequest
{
2017-02-17 08:25:19 -05:00
/**
* Set of requests to make in parallel
*/
2020-04-10 20:01:46 -04:00
private array $requests = [];
2017-02-17 08:25:19 -05:00
/**
* Add a request
*/
2023-05-09 12:49:36 -04:00
public function addRequest(string|Request $request, string|int|null $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;
2017-02-17 08:25:19 -05:00
return $this;
}
2017-02-17 08:25:19 -05:00
$this->requests[] = $request;
2017-02-17 08:25:19 -05:00
return $this;
}
2017-02-17 08:25:19 -05:00
/**
* Add multiple requests
*
* @param Request[]|string[] $requests
2017-02-17 08:25:19 -05:00
*/
public function addRequests(array $requests): self
{
2023-05-19 10:54:08 -04:00
array_walk($requests, $this->addRequest(...));
2017-02-17 08:25:19 -05:00
return $this;
}
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
*
2019-12-09 14:34:23 -05:00
* @throws Throwable
2017-02-17 08:25:19 -05:00
*/
public function makeRequests(): array
2023-07-13 11:06:52 -04:00
{
return $this->makeRequestOld();
}
/**
* Make the requests and return the response objects
*
* @throws Throwable
*/
public function getResponses(): array
{
return $this->getResponsesOld();
}
private function makeRequestOld(): array
2017-02-17 08:25:19 -05:00
{
$client = getApiClient();
$promises = [];
foreach ($this->requests as $key => $url)
{
$promises[$key] = call(static function () use ($client, $url): Generator {
$response = yield $client->request($url);
2020-03-11 16:26:17 -04:00
return yield $response->getBody()->buffer();
});
}
return wait(all($promises));
2017-02-17 08:25:19 -05:00
}
2018-10-01 10:50:22 -04:00
2023-07-13 11:06:52 -04:00
private function makeRequestsNew(): array
{
$futures = [];
foreach ($this->requests as $key => $url)
{
$futures[$key] = async(static fn () => self::bodyHandler($url));
}
return await($futures);
}
private function getResponsesOld(): array
2018-10-01 10:50:22 -04:00
{
$client = getApiClient();
2018-10-01 10:50:22 -04:00
$promises = [];
foreach ($this->requests as $key => $url)
{
$promises[$key] = call(static fn () => yield $client->request($url));
2018-10-01 10:50:22 -04:00
}
return wait(all($promises));
}
2023-07-13 11:06:52 -04:00
private function getResponsesNew(): array
{
$futures = [];
foreach ($this->requests as $key => $url)
{
$futures[$key] = async(static fn () => self::responseHandler($url));
}
return await($futures);
}
private static function bodyHandler(string|Request $uri): string
{
$client = getApiClient();
if (is_string($uri))
{
$uri = new Request($uri);
}
$response = $client->request($uri);
return $response->getBody()->buffer();
}
private static function responseHandler(string|Request $uri)
{
$client = getApiClient();
if (is_string($uri))
{
$uri = new Request($uri);
}
return $client->request($uri);
}
}