From 1a4a65ceafbe9130f74f38e505fc48d354298795 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Wed, 11 Mar 2020 16:26:17 -0400 Subject: [PATCH] Move to Amp/HttpClient from Amp/Artax --- Jenkinsfile | 2 +- composer.json | 6 +++--- src/API/APIRequestBuilder.php | 24 +++++++++++++----------- src/API/AbstractListItem.php | 4 ++-- src/API/Anilist/AnilistTrait.php | 12 ++++++------ src/API/Anilist/ListItem.php | 4 ++-- src/API/Anilist/Model.php | 12 +++++------- src/API/Kitsu/KitsuTrait.php | 10 +++++----- src/API/Kitsu/ListItem.php | 6 +++--- src/API/Kitsu/Model.php | 8 ++++---- src/API/ParallelAPIRequest.php | 6 +++--- src/AnimeClient.php | 11 ++++++----- src/Controller/Images.php | 4 ++-- 13 files changed, 55 insertions(+), 54 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index fad2b0f2..b698b0af 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -12,7 +12,7 @@ pipeline { sh 'apk add --no-cache php7-phpdbg php7-xsl php7-gd php7-json php7-pdo' sh 'curl -sS https://getcomposer.org/installer | php' sh 'rm -f composer.lock' - sh 'php composer.phar install' + sh 'php composer.phar install --ignore-platform-reqs' sh 'php vendor/bin/robo lint' sh 'php ./vendor/bin/phpunit --colors=never' } diff --git a/composer.json b/composer.json index 27149a55..401e12bc 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ } }, "require": { - "amphp/artax": "^3.0", + "amphp/http-client": "^4.2", "aura/html": "^2.0", "aura/router": "^3.0", "aura/session": "^2.0", @@ -28,13 +28,13 @@ "ext-json": "*", "ext-gd":"*", "ext-pdo": "*", + "laminas/laminas-diactoros": "^2.0.0", "maximebf/consolekit": "^1.0", "monolog/monolog": "^2.0.1", "php": "^7.3", "psr/http-message": "~1.0", "psr/log": "~1.0", - "yosymfony/toml": "^1.0", - "zendframework/zend-diactoros": "^2.0.0" + "yosymfony/toml": "^1.0" }, "require-dev": { "consolidation/robo": "^2.0.0", diff --git a/src/API/APIRequestBuilder.php b/src/API/APIRequestBuilder.php index 4c6eb772..dde26286 100644 --- a/src/API/APIRequestBuilder.php +++ b/src/API/APIRequestBuilder.php @@ -4,7 +4,7 @@ * * An API client for Kitsu to manage anime and manga watch lists * - * PHP version 7.3 + * PHP version 7.2 * * @package HummingbirdAnimeClient * @author Timothy J. Warren @@ -22,13 +22,14 @@ use function Amp\Promise\wait; use function Aviat\AnimeClient\getResponse; use Amp; -use Amp\Artax\{FormBody, Request}; +use Amp\Http\Client\Request; +use Amp\Http\Client\Body\FormBody; use Aviat\Ion\Json; use InvalidArgumentException; use Psr\Log\LoggerAwareTrait; /** - * Wrapper around Artax to make it easier to build API requests + * Wrapper around Http\Client to make it easier to build API requests */ abstract class APIRequestBuilder { use LoggerAwareTrait; @@ -78,7 +79,7 @@ abstract class APIRequestBuilder { public static function simpleRequest(string $uri): Request { return (new Request($uri)) - ->withHeader('User-Agent', USER_AGENT); + ->setHeader('User-Agent', USER_AGENT); } /** @@ -118,7 +119,7 @@ abstract class APIRequestBuilder { */ public function setBody($body): self { - $this->request = $this->request->withBody($body); + $this->request->setBody($body); return $this; } @@ -145,7 +146,7 @@ abstract class APIRequestBuilder { */ public function unsetHeader(string $name): self { - $this->request = $this->request->withoutHeader($name); + $this->request->removeHeader($name); return $this; } @@ -164,7 +165,7 @@ abstract class APIRequestBuilder { } else { - $this->request = $this->request->withHeader($name, $value); + $this->request->setHeader($name, $value); } return $this; @@ -254,7 +255,7 @@ abstract class APIRequestBuilder { public function getResponseData(Request $request) { $response = getResponse($request); - return wait($response->getBody()); + return wait($response->getBody()->buffer()); } /** @@ -306,7 +307,7 @@ abstract class APIRequestBuilder { $url .= '?' . $this->query; } - $this->request = $this->request->withUri($url); + $this->request->setUri($url); return $this->request; } @@ -324,7 +325,8 @@ abstract class APIRequestBuilder { $this->path = ''; $this->query = ''; - $this->request = (new Request($requestUrl)) - ->withMethod($type); + $this->request = new Request($requestUrl, $type); + $this->request->setTcpConnectTimeout(300000); + $this->request->setTransferTimeout(300000); } } \ No newline at end of file diff --git a/src/API/AbstractListItem.php b/src/API/AbstractListItem.php index 9ea460f5..09a4739e 100644 --- a/src/API/AbstractListItem.php +++ b/src/API/AbstractListItem.php @@ -4,7 +4,7 @@ * * An API client for Kitsu to manage anime and manga watch lists * - * PHP version 7.3 + * PHP version 7.2 * * @package HummingbirdAnimeClient * @author Timothy J. Warren @@ -16,7 +16,7 @@ namespace Aviat\AnimeClient\API; -use Amp\Artax\Request; +use Amp\Http\Client\Request; use Aviat\AnimeClient\Types\FormItemData; /** diff --git a/src/API/Anilist/AnilistTrait.php b/src/API/Anilist/AnilistTrait.php index e25c6d9e..7caa045c 100644 --- a/src/API/Anilist/AnilistTrait.php +++ b/src/API/Anilist/AnilistTrait.php @@ -4,7 +4,7 @@ * * An API client for Kitsu to manage anime and manga watch lists * - * PHP version 7.3 + * PHP version 7.2 * * @package HummingbirdAnimeClient * @author Timothy J. Warren @@ -21,8 +21,8 @@ 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 Amp\Http\Client\Request; +use Amp\Http\Client\Response; use Aviat\AnimeClient\API\Anilist; use Aviat\Ion\Json; @@ -194,7 +194,7 @@ trait AnilistTrait { $request = $this->mutateRequest($name, $variables); $response = $this->getResponseFromRequest($request); - return Json::decode(wait($response->getBody())); + return Json::decode(wait($response->getBody()->buffer())); } /** @@ -286,8 +286,8 @@ trait AnilistTrait { } } - // dump(wait($response->getBody())); + // dump(wait($response->getBody()->buffer())); - return Json::decode(wait($response->getBody())); + return Json::decode(wait($response->getBody()->buffer())); } } \ No newline at end of file diff --git a/src/API/Anilist/ListItem.php b/src/API/Anilist/ListItem.php index 0dced326..e01a3880 100644 --- a/src/API/Anilist/ListItem.php +++ b/src/API/Anilist/ListItem.php @@ -4,7 +4,7 @@ * * An API client for Kitsu to manage anime and manga watch lists * - * PHP version 7.3 + * PHP version 7.2 * * @package HummingbirdAnimeClient * @author Timothy J. Warren @@ -16,7 +16,7 @@ namespace Aviat\AnimeClient\API\Anilist; -use Amp\Artax\Request; +use Amp\Http\Client\Request; use Aviat\AnimeClient\API\AbstractListItem; use Aviat\AnimeClient\API\Enum\AnimeWatchingStatus\Anilist as AnilistStatus; diff --git a/src/API/Anilist/Model.php b/src/API/Anilist/Model.php index 54de7b58..085b93fe 100644 --- a/src/API/Anilist/Model.php +++ b/src/API/Anilist/Model.php @@ -4,7 +4,7 @@ * * An API client for Kitsu to manage anime and manga watch lists * - * PHP version 7.3 + * PHP version 7.2 * * @package HummingbirdAnimeClient * @author Timothy J. Warren @@ -20,7 +20,7 @@ use function Amp\Promise\wait; use InvalidArgumentException; -use Amp\Artax\Request; +use Amp\Http\Client\Request; use Aviat\AnimeClient\API\Anilist; use Aviat\AnimeClient\API\Mapping\{AnimeWatchingStatus, MangaReadingStatus}; use Aviat\AnimeClient\Types\FormItem; @@ -79,7 +79,7 @@ final class Model $response = $this->getResponseFromRequest($request); - return Json::decode(wait($response->getBody())); + return Json::decode(wait($response->getBody()->buffer())); } /** @@ -276,7 +276,7 @@ final class Model * this way is more accurate than getting the list item id * directly from the MAL id */ - private function getListIdFromMediaId(string $mediaId): ?string + private function getListIdFromMediaId(string $mediaId): string { $config = $this->container->get('config'); $anilistUser = $config->get(['anilist', 'username']); @@ -286,9 +286,7 @@ final class Model 'userName' => $anilistUser, ]); - $data = $info['data']['MediaList']; - - return ($data !== NULL) ? (string)$data['id'] : NULL; + return (string)$info['data']['MediaList']['id']; } /** diff --git a/src/API/Kitsu/KitsuTrait.php b/src/API/Kitsu/KitsuTrait.php index ea4e1dd8..4597d450 100644 --- a/src/API/Kitsu/KitsuTrait.php +++ b/src/API/Kitsu/KitsuTrait.php @@ -4,7 +4,7 @@ * * An API client for Kitsu to manage anime and manga watch lists * - * PHP version 7.3 + * PHP version 7.2 * * @package HummingbirdAnimeClient * @author Timothy J. Warren @@ -21,8 +21,8 @@ use const Aviat\AnimeClient\SESSION_SEGMENT; use function Amp\Promise\wait; use function Aviat\AnimeClient\getResponse; -use Amp\Artax\Request; -use Amp\Artax\Response; +use Amp\Http\Client\Request; +use Amp\Http\Client\Response; use Aviat\AnimeClient\API\{ FailedResponseException, Kitsu as K @@ -181,7 +181,7 @@ trait KitsuTrait { try { - return Json::decode(wait($response->getBody())); + return Json::decode(wait($response->getBody()->buffer())); } catch (JsonException $e) { @@ -241,7 +241,7 @@ trait KitsuTrait { } } - return JSON::decode(wait($response->getBody()), TRUE); + return JSON::decode(wait($response->getBody()->buffer()), TRUE); } /** diff --git a/src/API/Kitsu/ListItem.php b/src/API/Kitsu/ListItem.php index 60e10b98..1c7a9f4e 100644 --- a/src/API/Kitsu/ListItem.php +++ b/src/API/Kitsu/ListItem.php @@ -4,7 +4,7 @@ * * An API client for Kitsu to manage anime and manga watch lists * - * PHP version 7.3 + * PHP version 7.2 * * @package HummingbirdAnimeClient * @author Timothy J. Warren @@ -23,7 +23,7 @@ use const Aviat\AnimeClient\SESSION_SEGMENT; use function Amp\Promise\wait; use function Aviat\AnimeClient\getResponse; -use Amp\Artax\Request; +use Amp\Http\Client\Request; use Aviat\AnimeClient\API\AbstractListItem; use Aviat\AnimeClient\Types\FormItemData; use Aviat\Ion\Di\ContainerAware; @@ -126,7 +126,7 @@ final class ListItem extends AbstractListItem { $request = $request->getFullRequest(); $response = getResponse($request); - return Json::decode(wait($response->getBody())); + return Json::decode(wait($response->getBody()->buffer())); } public function increment(string $id, FormItemData $data): Request diff --git a/src/API/Kitsu/Model.php b/src/API/Kitsu/Model.php index a99c9e25..61529a92 100644 --- a/src/API/Kitsu/Model.php +++ b/src/API/Kitsu/Model.php @@ -4,7 +4,7 @@ * * An API client for Kitsu to manage anime and manga watch lists * - * PHP version 7.3 + * PHP version 7.2 * * @package HummingbirdAnimeClient * @author Timothy J. Warren @@ -18,7 +18,7 @@ namespace Aviat\AnimeClient\API\Kitsu; use function Amp\Promise\wait; -use Amp\Artax\Request; +use Amp\Http\Client\Request; use Aviat\AnimeClient\API\{ CacheTrait, JsonAPI, @@ -126,7 +126,7 @@ final class Model { 'password' => $password ] ]); - $data = Json::decode(wait($response->getBody())); + $data = Json::decode(wait($response->getBody()->buffer())); if (array_key_exists('error', $data)) { @@ -163,7 +163,7 @@ final class Model { ] ]); - $data = Json::decode(wait($response->getBody())); + $data = Json::decode(wait($response->getBody()->buffer())); if (array_key_exists('access_token', $data)) { diff --git a/src/API/ParallelAPIRequest.php b/src/API/ParallelAPIRequest.php index e3b3ac50..1640fc5b 100644 --- a/src/API/ParallelAPIRequest.php +++ b/src/API/ParallelAPIRequest.php @@ -4,7 +4,7 @@ * * An API client for Kitsu to manage anime and manga watch lists * - * PHP version 7.3 + * PHP version 7.2 * * @package HummingbirdAnimeClient * @author Timothy J. Warren @@ -16,7 +16,7 @@ namespace Aviat\AnimeClient\API; -use Amp\Artax\Request; +use Amp\Http\Client\Request; use function Amp\call; use function Amp\Promise\{all, wait}; use function Aviat\AnimeClient\getApiClient; @@ -82,7 +82,7 @@ final class ParallelAPIRequest { { $promises[$key] = call(static function () use ($client, $url) { $response = yield $client->request($url); - return yield $response->getBody(); + return yield $response->getBody()->buffer(); }); } diff --git a/src/AnimeClient.php b/src/AnimeClient.php index b282560e..cd858e23 100644 --- a/src/AnimeClient.php +++ b/src/AnimeClient.php @@ -4,7 +4,7 @@ * * An API client for Kitsu to manage anime and manga watch lists * - * PHP version 7.3 + * PHP version 7.2 * * @package HummingbirdAnimeClient * @author Timothy J. Warren @@ -18,7 +18,9 @@ namespace Aviat\AnimeClient; use function Amp\Promise\wait; -use Amp\Artax\{Client, DefaultClient, Response}; +use Amp\Http\Client\Response; +use Amp\Http\Client\HttpClient; +use Amp\Http\Client\HttpClientBuilder; use Aviat\Ion\ConfigInterface; use Yosymfony\Toml\{Toml, TomlBuilder}; @@ -218,15 +220,14 @@ function getApiClient () if ($client === NULL) { - $client = new DefaultClient; - $client->setOption(Client::OP_TRANSFER_TIMEOUT, 0); + $client = HttpClientBuilder::buildDefault(); } return $client; } /** - * Simplify making a request with Artax + * Simplify making a request with Http\Client * * @param $request * @return Response diff --git a/src/Controller/Images.php b/src/Controller/Images.php index 37f9a747..dd32c032 100644 --- a/src/Controller/Images.php +++ b/src/Controller/Images.php @@ -4,7 +4,7 @@ * * An API client for Kitsu to manage anime and manga watch lists * - * PHP version 7.3 + * PHP version 7.2 * * @package HummingbirdAnimeClient * @author Timothy J. Warren @@ -130,7 +130,7 @@ final class Images extends BaseController { return; } - $data = wait($response->getBody()); + $data = wait($response->getBody()->buffer()); [$origWidth] = getimagesizefromstring($data); $gdImg = imagecreatefromstring($data);