Some API client cleanup
All checks were successful
timw4mail/HummingBirdAnimeClient/PR-14 This commit looks good

This commit is contained in:
Timothy Warren 2018-11-29 11:00:50 -05:00
parent 921febaeb4
commit f09716b040
9 changed files with 47 additions and 1344 deletions

View File

@ -17,6 +17,7 @@
namespace Aviat\AnimeClient\API; namespace Aviat\AnimeClient\API;
use function Amp\Promise\wait; use function Amp\Promise\wait;
use function Aviat\AnimeClient\getResponse;
use Amp; use Amp;
use Amp\Artax\{FormBody, Request}; use Amp\Artax\{FormBody, Request};
@ -250,7 +251,7 @@ class APIRequestBuilder {
*/ */
public function getResponseData(Request $request) public function getResponseData(Request $request)
{ {
$response = wait((new HummingbirdClient)->request($request)); $response = getResponse($request);
return wait($response->getBody()); return wait($response->getBody());
} }

View File

@ -19,14 +19,12 @@ namespace Aviat\AnimeClient\API\Anilist;
use const Aviat\AnimeClient\USER_AGENT; use const Aviat\AnimeClient\USER_AGENT;
use function Amp\Promise\wait; use function Amp\Promise\wait;
use function Aviat\AnimeClient\getResponse;
use Amp\Artax\Request; use Amp\Artax\Request;
use Amp\Artax\Response; use Amp\Artax\Response;
use Aviat\AnimeClient\API\{ use Aviat\AnimeClient\API\Anilist;
Anilist,
HummingbirdClient
};
use Aviat\Ion\Json; use Aviat\Ion\Json;
use Aviat\Ion\Di\ContainerAware; use Aviat\Ion\Di\ContainerAware;
@ -200,7 +198,7 @@ trait AnilistTrait {
} }
$request = $this->setUpRequest($url, $options); $request = $this->setUpRequest($url, $options);
$response = wait((new HummingbirdClient)->request($request)); $response = getResponse($request);
$logger->debug('Anilist response', [ $logger->debug('Anilist response', [
'status' => $response->getStatus(), 'status' => $response->getStatus(),
@ -221,7 +219,7 @@ trait AnilistTrait {
$logger = $this->container->getLogger('anilist-request'); $logger = $this->container->getLogger('anilist-request');
} }
$response = wait((new HummingbirdClient)->request($request)); $response = getResponse($request);
$logger->debug('Anilist response', [ $logger->debug('Anilist response', [
'status' => $response->getStatus(), 'status' => $response->getStatus(),

File diff suppressed because it is too large Load Diff

View File

@ -16,16 +16,15 @@
namespace Aviat\AnimeClient\API\Kitsu; namespace Aviat\AnimeClient\API\Kitsu;
use Amp\Artax\Response;
use const Aviat\AnimeClient\SESSION_SEGMENT; use const Aviat\AnimeClient\SESSION_SEGMENT;
use function Amp\Promise\wait; use function Amp\Promise\wait;
use function Aviat\AnimeClient\getResponse;
use Amp\Artax\Request; use Amp\Artax\Request;
use Aviat\AnimeClient\AnimeClient; use Amp\Artax\Response;
use Aviat\AnimeClient\API\{ use Aviat\AnimeClient\API\{
FailedResponseException, FailedResponseException,
HummingbirdClient,
Kitsu as K Kitsu as K
}; };
use Aviat\Ion\Json; use Aviat\Ion\Json;
@ -132,7 +131,7 @@ trait KitsuTrait {
$request = $this->setUpRequest($type, $url, $options); $request = $this->setUpRequest($type, $url, $options);
$response = wait((new HummingbirdClient)->request($request)); $response = getResponse($request);
if ($logger) if ($logger)
{ {

View File

@ -19,12 +19,10 @@ namespace Aviat\AnimeClient\API\Kitsu;
use const Aviat\AnimeClient\SESSION_SEGMENT; use const Aviat\AnimeClient\SESSION_SEGMENT;
use function Amp\Promise\wait; use function Amp\Promise\wait;
use function Aviat\AnimeClient\getResponse;
use Amp\Artax\Request; use Amp\Artax\Request;
use Aviat\AnimeClient\API\{ use Aviat\AnimeClient\API\ListItemInterface;
HummingbirdClient,
ListItemInterface
};
use Aviat\AnimeClient\Types\FormItemData; use Aviat\AnimeClient\Types\FormItemData;
use Aviat\Ion\Di\ContainerAware; use Aviat\Ion\Di\ContainerAware;
use Aviat\Ion\Json; use Aviat\Ion\Json;
@ -107,9 +105,8 @@ final class ListItem implements ListItemInterface {
$request = $request->setHeader('Authorization', $authHeader); $request = $request->setHeader('Authorization', $authHeader);
} }
$request = $request->getFullRequest(); $request = $request->getFullReqest();
$response = getResponse($request);
$response = wait((new HummingbirdClient)->request($request));
return Json::decode(wait($response->getBody())); return Json::decode(wait($response->getBody()));
} }

View File

@ -19,6 +19,8 @@ namespace Aviat\AnimeClient\API;
use function Amp\call; use function Amp\call;
use function Amp\Promise\{all, wait}; use function Amp\Promise\{all, wait};
use Amp\Artax\DefaultClient;
/** /**
* Class to simplify making and validating simultaneous requests * Class to simplify making and validating simultaneous requests
*/ */
@ -70,7 +72,7 @@ final class ParallelAPIRequest {
*/ */
public function makeRequests(): array public function makeRequests(): array
{ {
$client = new HummingbirdClient(); $client = new DefaultClient();
$promises = []; $promises = [];
foreach ($this->requests as $key => $url) foreach ($this->requests as $key => $url)
@ -92,7 +94,7 @@ final class ParallelAPIRequest {
*/ */
public function getResponses(): array public function getResponses(): array
{ {
$client = new HummingbirdClient(); $client = new DefaultClient();
$promises = []; $promises = [];
foreach ($this->requests as $key => $url) foreach ($this->requests as $key => $url)

View File

@ -16,6 +16,11 @@
namespace Aviat\AnimeClient; namespace Aviat\AnimeClient;
use function Amp\Promise\wait;
use Amp\Artax\DefaultClient;
use Amp\Artax\Response;
use Aviat\Ion\ConfigInterface; use Aviat\Ion\ConfigInterface;
use Yosymfony\Toml\{Toml, TomlBuilder}; use Yosymfony\Toml\{Toml, TomlBuilder};
@ -203,6 +208,18 @@ function checkFolderPermissions(ConfigInterface $config): array
return $errors; 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 * Generate the path for the cached image from the original image
* *

View File

@ -16,13 +16,11 @@
namespace Aviat\AnimeClient\Controller; namespace Aviat\AnimeClient\Controller;
use function Aviat\AnimeClient\createPlaceholderImage;
use function Amp\Promise\wait; use function Amp\Promise\wait;
use function Aviat\AnimeClient\getResponse;
use function Aviat\AnimeClient\createPlaceholderImage;
use Aviat\AnimeClient\Controller as BaseController; 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 * 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, // Kitsu doesn't serve webp, but for most use cases,
// jpg is a safe assumption // jpg is a safe assumption
$tryJpg = ['anime','characters','manga','people']; $tryJpg = ['anime','characters','manga','people'];
if ($ext === 'webp' && in_array($type, $tryJpg, TRUE)) if ($ext === 'webp' && \in_array($type, $tryJpg, TRUE))
{ {
$ext = 'jpg'; $ext = 'jpg';
$currentUrl = str_replace('webp', 'jpg', $currentUrl); $currentUrl = str_replace('webp', 'jpg', $currentUrl);
@ -102,8 +100,7 @@ final class Images extends BaseController {
$height = $imageType['height']; $height = $imageType['height'];
$filePrefix = "{$baseSavePath}/{$type}/{$id}"; $filePrefix = "{$baseSavePath}/{$type}/{$id}";
$promise = (new HummingbirdClient)->request($kitsuUrl); $response = getResponse($kitsuUrl);
$response = wait($promise);
if ($response->getStatus() !== 200) if ($response->getStatus() !== 200)
{ {
@ -162,7 +159,7 @@ final class Images extends BaseController {
if ($display) if ($display)
{ {
$contentType = ($ext === 'webp') $contentType = ($ext === 'webp')
? "image/webp" ? 'image/webp'
: $response->getHeader('content-type')[0]; : $response->getHeader('content-type')[0];
$outputFile = (strpos($file, '-original') !== FALSE) $outputFile = (strpos($file, '-original') !== FALSE)

View File

@ -17,7 +17,9 @@
namespace Aviat\AnimeClient\Tests\API; namespace Aviat\AnimeClient\Tests\API;
use function Amp\Promise\wait; 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 Aviat\Ion\Json;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger; use Psr\Log\NullLogger;
@ -41,7 +43,7 @@ class APIRequestBuilderTest extends TestCase {
{ {
$request = $this->builder->newRequest('GET', 'gzip') $request = $this->builder->newRequest('GET', 'gzip')
->getFullRequest(); ->getFullRequest();
$response = wait((new HummingbirdClient)->request($request)); $response = getResponse($request);
$body = Json::decode(wait($response->getBody())); $body = Json::decode(wait($response->getBody()));
$this->assertEquals(1, $body['gzipped']); $this->assertEquals(1, $body['gzipped']);
} }
@ -59,7 +61,7 @@ class APIRequestBuilderTest extends TestCase {
->setBasicAuth('username', 'password') ->setBasicAuth('username', 'password')
->getFullRequest(); ->getFullRequest();
$response = wait((new HummingbirdClient)->request($request)); $response = getResponse($request);
$body = Json::decode(wait($response->getBody())); $body = Json::decode(wait($response->getBody()));
$this->assertEquals('Basic dXNlcm5hbWU6cGFzc3dvcmQ=', $body['headers']['Authorization']); $this->assertEquals('Basic dXNlcm5hbWU6cGFzc3dvcmQ=', $body['headers']['Authorization']);
@ -87,7 +89,7 @@ class APIRequestBuilderTest extends TestCase {
->setQuery($query) ->setQuery($query)
->getFullRequest(); ->getFullRequest();
$response = wait((new HummingbirdClient)->request($request)); $response = getResponse($request);
$body = Json::decode(wait($response->getBody())); $body = Json::decode(wait($response->getBody()));
$this->assertEquals($expected, $body['args']); $this->assertEquals($expected, $body['args']);
@ -104,7 +106,7 @@ class APIRequestBuilderTest extends TestCase {
->setFormFields($formValues) ->setFormFields($formValues)
->getFullRequest(); ->getFullRequest();
$response = wait((new HummingbirdClient)->request($request)); $response = getResponse($request);
$body = Json::decode(wait($response->getBody())); $body = Json::decode(wait($response->getBody()));
$this->assertEquals($formValues, $body['form']); $this->assertEquals($formValues, $body['form']);
@ -128,7 +130,7 @@ class APIRequestBuilderTest extends TestCase {
->setJsonBody($data) ->setJsonBody($data)
->getFullRequest(); ->getFullRequest();
$response = wait((new HummingbirdClient)->request($request)); $response = getResponse($request);
$body = Json::decode(wait($response->getBody())); $body = Json::decode(wait($response->getBody()));
$this->assertEquals($data, $body['json']); $this->assertEquals($data, $body['json']);