Some API client cleanup
This commit is contained in:
parent
921febaeb4
commit
f09716b040
@ -17,6 +17,7 @@
|
||||
namespace Aviat\AnimeClient\API;
|
||||
|
||||
use function Amp\Promise\wait;
|
||||
use function Aviat\AnimeClient\getResponse;
|
||||
|
||||
use Amp;
|
||||
use Amp\Artax\{FormBody, Request};
|
||||
@ -250,7 +251,7 @@ class APIRequestBuilder {
|
||||
*/
|
||||
public function getResponseData(Request $request)
|
||||
{
|
||||
$response = wait((new HummingbirdClient)->request($request));
|
||||
$response = getResponse($request);
|
||||
return wait($response->getBody());
|
||||
}
|
||||
|
||||
|
@ -19,14 +19,12 @@ namespace Aviat\AnimeClient\API\Anilist;
|
||||
use const Aviat\AnimeClient\USER_AGENT;
|
||||
|
||||
use function Amp\Promise\wait;
|
||||
use function Aviat\AnimeClient\getResponse;
|
||||
|
||||
use Amp\Artax\Request;
|
||||
use Amp\Artax\Response;
|
||||
|
||||
use Aviat\AnimeClient\API\{
|
||||
Anilist,
|
||||
HummingbirdClient
|
||||
};
|
||||
use Aviat\AnimeClient\API\Anilist;
|
||||
use Aviat\Ion\Json;
|
||||
use Aviat\Ion\Di\ContainerAware;
|
||||
|
||||
@ -200,7 +198,7 @@ trait AnilistTrait {
|
||||
}
|
||||
|
||||
$request = $this->setUpRequest($url, $options);
|
||||
$response = wait((new HummingbirdClient)->request($request));
|
||||
$response = getResponse($request);
|
||||
|
||||
$logger->debug('Anilist response', [
|
||||
'status' => $response->getStatus(),
|
||||
@ -221,7 +219,7 @@ trait AnilistTrait {
|
||||
$logger = $this->container->getLogger('anilist-request');
|
||||
}
|
||||
|
||||
$response = wait((new HummingbirdClient)->request($request));
|
||||
$response = getResponse($request);
|
||||
|
||||
$logger->debug('Anilist response', [
|
||||
'status' => $response->getStatus(),
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -16,16 +16,15 @@
|
||||
|
||||
namespace Aviat\AnimeClient\API\Kitsu;
|
||||
|
||||
use Amp\Artax\Response;
|
||||
use const Aviat\AnimeClient\SESSION_SEGMENT;
|
||||
|
||||
use function Amp\Promise\wait;
|
||||
use function Aviat\AnimeClient\getResponse;
|
||||
|
||||
use Amp\Artax\Request;
|
||||
use Aviat\AnimeClient\AnimeClient;
|
||||
use Amp\Artax\Response;
|
||||
use Aviat\AnimeClient\API\{
|
||||
FailedResponseException,
|
||||
HummingbirdClient,
|
||||
Kitsu as K
|
||||
};
|
||||
use Aviat\Ion\Json;
|
||||
@ -132,7 +131,7 @@ trait KitsuTrait {
|
||||
|
||||
$request = $this->setUpRequest($type, $url, $options);
|
||||
|
||||
$response = wait((new HummingbirdClient)->request($request));
|
||||
$response = getResponse($request);
|
||||
|
||||
if ($logger)
|
||||
{
|
||||
|
@ -19,12 +19,10 @@ namespace Aviat\AnimeClient\API\Kitsu;
|
||||
use const Aviat\AnimeClient\SESSION_SEGMENT;
|
||||
|
||||
use function Amp\Promise\wait;
|
||||
use function Aviat\AnimeClient\getResponse;
|
||||
|
||||
use Amp\Artax\Request;
|
||||
use Aviat\AnimeClient\API\{
|
||||
HummingbirdClient,
|
||||
ListItemInterface
|
||||
};
|
||||
use Aviat\AnimeClient\API\ListItemInterface;
|
||||
use Aviat\AnimeClient\Types\FormItemData;
|
||||
use Aviat\Ion\Di\ContainerAware;
|
||||
use Aviat\Ion\Json;
|
||||
@ -107,9 +105,8 @@ final class ListItem implements ListItemInterface {
|
||||
$request = $request->setHeader('Authorization', $authHeader);
|
||||
}
|
||||
|
||||
$request = $request->getFullRequest();
|
||||
|
||||
$response = wait((new HummingbirdClient)->request($request));
|
||||
$request = $request->getFullReqest();
|
||||
$response = getResponse($request);
|
||||
return Json::decode(wait($response->getBody()));
|
||||
}
|
||||
|
||||
|
@ -19,6 +19,8 @@ namespace Aviat\AnimeClient\API;
|
||||
use function Amp\call;
|
||||
use function Amp\Promise\{all, wait};
|
||||
|
||||
use Amp\Artax\DefaultClient;
|
||||
|
||||
/**
|
||||
* Class to simplify making and validating simultaneous requests
|
||||
*/
|
||||
@ -70,7 +72,7 @@ final class ParallelAPIRequest {
|
||||
*/
|
||||
public function makeRequests(): array
|
||||
{
|
||||
$client = new HummingbirdClient();
|
||||
$client = new DefaultClient();
|
||||
$promises = [];
|
||||
|
||||
foreach ($this->requests as $key => $url)
|
||||
@ -92,7 +94,7 @@ final class ParallelAPIRequest {
|
||||
*/
|
||||
public function getResponses(): array
|
||||
{
|
||||
$client = new HummingbirdClient();
|
||||
$client = new DefaultClient();
|
||||
$promises = [];
|
||||
|
||||
foreach ($this->requests as $key => $url)
|
||||
|
@ -16,6 +16,11 @@
|
||||
|
||||
namespace Aviat\AnimeClient;
|
||||
|
||||
use function Amp\Promise\wait;
|
||||
|
||||
use Amp\Artax\DefaultClient;
|
||||
use Amp\Artax\Response;
|
||||
|
||||
use Aviat\Ion\ConfigInterface;
|
||||
use Yosymfony\Toml\{Toml, TomlBuilder};
|
||||
|
||||
@ -203,6 +208,18 @@ function checkFolderPermissions(ConfigInterface $config): array
|
||||
return $errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Simplify making a request with Artax
|
||||
*
|
||||
* @param $request
|
||||
* @return Response
|
||||
* @throws \Throwable
|
||||
*/
|
||||
function getResponse ($request): Response
|
||||
{
|
||||
return wait((new DefaultClient)->request($request));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the path for the cached image from the original image
|
||||
*
|
||||
|
@ -16,13 +16,11 @@
|
||||
|
||||
namespace Aviat\AnimeClient\Controller;
|
||||
|
||||
use function Aviat\AnimeClient\createPlaceholderImage;
|
||||
use function Amp\Promise\wait;
|
||||
use function Aviat\AnimeClient\getResponse;
|
||||
use function Aviat\AnimeClient\createPlaceholderImage;
|
||||
|
||||
use Aviat\AnimeClient\Controller as BaseController;
|
||||
use Aviat\AnimeClient\API\{HummingbirdClient, JsonAPI};
|
||||
use Aviat\Ion\Di\ContainerInterface;
|
||||
use Aviat\Ion\View\HtmlView;
|
||||
|
||||
/**
|
||||
* Controller for handling routes that don't fit elsewhere
|
||||
@ -55,7 +53,7 @@ final class Images extends BaseController {
|
||||
// Kitsu doesn't serve webp, but for most use cases,
|
||||
// jpg is a safe assumption
|
||||
$tryJpg = ['anime','characters','manga','people'];
|
||||
if ($ext === 'webp' && in_array($type, $tryJpg, TRUE))
|
||||
if ($ext === 'webp' && \in_array($type, $tryJpg, TRUE))
|
||||
{
|
||||
$ext = 'jpg';
|
||||
$currentUrl = str_replace('webp', 'jpg', $currentUrl);
|
||||
@ -102,8 +100,7 @@ final class Images extends BaseController {
|
||||
$height = $imageType['height'];
|
||||
$filePrefix = "{$baseSavePath}/{$type}/{$id}";
|
||||
|
||||
$promise = (new HummingbirdClient)->request($kitsuUrl);
|
||||
$response = wait($promise);
|
||||
$response = getResponse($kitsuUrl);
|
||||
|
||||
if ($response->getStatus() !== 200)
|
||||
{
|
||||
@ -162,7 +159,7 @@ final class Images extends BaseController {
|
||||
if ($display)
|
||||
{
|
||||
$contentType = ($ext === 'webp')
|
||||
? "image/webp"
|
||||
? 'image/webp'
|
||||
: $response->getHeader('content-type')[0];
|
||||
|
||||
$outputFile = (strpos($file, '-original') !== FALSE)
|
||||
|
@ -17,7 +17,9 @@
|
||||
namespace Aviat\AnimeClient\Tests\API;
|
||||
|
||||
use function Amp\Promise\wait;
|
||||
use Aviat\AnimeClient\API\{APIRequestBuilder, HummingbirdClient};
|
||||
use function Aviat\AnimeClient\getResponse;
|
||||
|
||||
use Aviat\AnimeClient\API\APIRequestBuilder;
|
||||
use Aviat\Ion\Json;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\NullLogger;
|
||||
@ -41,7 +43,7 @@ class APIRequestBuilderTest extends TestCase {
|
||||
{
|
||||
$request = $this->builder->newRequest('GET', 'gzip')
|
||||
->getFullRequest();
|
||||
$response = wait((new HummingbirdClient)->request($request));
|
||||
$response = getResponse($request);
|
||||
$body = Json::decode(wait($response->getBody()));
|
||||
$this->assertEquals(1, $body['gzipped']);
|
||||
}
|
||||
@ -59,7 +61,7 @@ class APIRequestBuilderTest extends TestCase {
|
||||
->setBasicAuth('username', 'password')
|
||||
->getFullRequest();
|
||||
|
||||
$response = wait((new HummingbirdClient)->request($request));
|
||||
$response = getResponse($request);
|
||||
$body = Json::decode(wait($response->getBody()));
|
||||
|
||||
$this->assertEquals('Basic dXNlcm5hbWU6cGFzc3dvcmQ=', $body['headers']['Authorization']);
|
||||
@ -87,7 +89,7 @@ class APIRequestBuilderTest extends TestCase {
|
||||
->setQuery($query)
|
||||
->getFullRequest();
|
||||
|
||||
$response = wait((new HummingbirdClient)->request($request));
|
||||
$response = getResponse($request);
|
||||
$body = Json::decode(wait($response->getBody()));
|
||||
|
||||
$this->assertEquals($expected, $body['args']);
|
||||
@ -104,7 +106,7 @@ class APIRequestBuilderTest extends TestCase {
|
||||
->setFormFields($formValues)
|
||||
->getFullRequest();
|
||||
|
||||
$response = wait((new HummingbirdClient)->request($request));
|
||||
$response = getResponse($request);
|
||||
$body = Json::decode(wait($response->getBody()));
|
||||
|
||||
$this->assertEquals($formValues, $body['form']);
|
||||
@ -128,7 +130,7 @@ class APIRequestBuilderTest extends TestCase {
|
||||
->setJsonBody($data)
|
||||
->getFullRequest();
|
||||
|
||||
$response = wait((new HummingbirdClient)->request($request));
|
||||
$response = getResponse($request);
|
||||
$body = Json::decode(wait($response->getBody()));
|
||||
|
||||
$this->assertEquals($data, $body['json']);
|
||||
|
Loading…
Reference in New Issue
Block a user