Move to Amp/HttpClient from Amp/Artax

This commit is contained in:
Timothy Warren 2020-03-11 16:26:17 -04:00
parent eb12f57e7d
commit 37c3d6ecf0
13 changed files with 55 additions and 54 deletions

2
Jenkinsfile vendored
View File

@ -12,7 +12,7 @@ pipeline {
sh 'apk add --no-cache php7-phpdbg php7-xsl php7-gd php7-json php7-pdo' sh 'apk add --no-cache php7-phpdbg php7-xsl php7-gd php7-json php7-pdo'
sh 'curl -sS https://getcomposer.org/installer | php' sh 'curl -sS https://getcomposer.org/installer | php'
sh 'rm -f composer.lock' 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/robo lint'
sh 'php ./vendor/bin/phpunit --colors=never' sh 'php ./vendor/bin/phpunit --colors=never'
} }

View File

@ -18,7 +18,7 @@
} }
}, },
"require": { "require": {
"amphp/artax": "^3.0", "amphp/http-client": "^4.2",
"aura/html": "^2.0", "aura/html": "^2.0",
"aura/router": "^3.0", "aura/router": "^3.0",
"aura/session": "^2.0", "aura/session": "^2.0",
@ -28,13 +28,13 @@
"ext-json": "*", "ext-json": "*",
"ext-gd":"*", "ext-gd":"*",
"ext-pdo": "*", "ext-pdo": "*",
"laminas/laminas-diactoros": "^2.0.0",
"maximebf/consolekit": "^1.0", "maximebf/consolekit": "^1.0",
"monolog/monolog": "^2.0.1", "monolog/monolog": "^2.0.1",
"php": "^7.3", "php": "^7.3",
"psr/http-message": "~1.0", "psr/http-message": "~1.0",
"psr/log": "~1.0", "psr/log": "~1.0",
"yosymfony/toml": "^1.0", "yosymfony/toml": "^1.0"
"zendframework/zend-diactoros": "^2.0.0"
}, },
"require-dev": { "require-dev": {
"consolidation/robo": "^2.0.0", "consolidation/robo": "^2.0.0",

View File

@ -4,7 +4,7 @@
* *
* An API client for Kitsu to manage anime and manga watch lists * An API client for Kitsu to manage anime and manga watch lists
* *
* PHP version 7.3 * PHP version 7.2
* *
* @package HummingbirdAnimeClient * @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net> * @author Timothy J. Warren <tim@timshomepage.net>
@ -22,13 +22,14 @@ use function Amp\Promise\wait;
use function Aviat\AnimeClient\getResponse; use function Aviat\AnimeClient\getResponse;
use Amp; use Amp;
use Amp\Artax\{FormBody, Request}; use Amp\Http\Client\Request;
use Amp\Http\Client\Body\FormBody;
use Aviat\Ion\Json; use Aviat\Ion\Json;
use InvalidArgumentException; use InvalidArgumentException;
use Psr\Log\LoggerAwareTrait; 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 { abstract class APIRequestBuilder {
use LoggerAwareTrait; use LoggerAwareTrait;
@ -78,7 +79,7 @@ abstract class APIRequestBuilder {
public static function simpleRequest(string $uri): Request public static function simpleRequest(string $uri): Request
{ {
return (new Request($uri)) 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 public function setBody($body): self
{ {
$this->request = $this->request->withBody($body); $this->request->setBody($body);
return $this; return $this;
} }
@ -145,7 +146,7 @@ abstract class APIRequestBuilder {
*/ */
public function unsetHeader(string $name): self public function unsetHeader(string $name): self
{ {
$this->request = $this->request->withoutHeader($name); $this->request->removeHeader($name);
return $this; return $this;
} }
@ -164,7 +165,7 @@ abstract class APIRequestBuilder {
} }
else else
{ {
$this->request = $this->request->withHeader($name, $value); $this->request->setHeader($name, $value);
} }
return $this; return $this;
@ -254,7 +255,7 @@ abstract class APIRequestBuilder {
public function getResponseData(Request $request) public function getResponseData(Request $request)
{ {
$response = getResponse($request); $response = getResponse($request);
return wait($response->getBody()); return wait($response->getBody()->buffer());
} }
/** /**
@ -306,7 +307,7 @@ abstract class APIRequestBuilder {
$url .= '?' . $this->query; $url .= '?' . $this->query;
} }
$this->request = $this->request->withUri($url); $this->request->setUri($url);
return $this->request; return $this->request;
} }
@ -324,7 +325,8 @@ abstract class APIRequestBuilder {
$this->path = ''; $this->path = '';
$this->query = ''; $this->query = '';
$this->request = (new Request($requestUrl)) $this->request = new Request($requestUrl, $type);
->withMethod($type); $this->request->setTcpConnectTimeout(300000);
$this->request->setTransferTimeout(300000);
} }
} }

View File

@ -4,7 +4,7 @@
* *
* An API client for Kitsu to manage anime and manga watch lists * An API client for Kitsu to manage anime and manga watch lists
* *
* PHP version 7.3 * PHP version 7.2
* *
* @package HummingbirdAnimeClient * @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net> * @author Timothy J. Warren <tim@timshomepage.net>
@ -16,7 +16,7 @@
namespace Aviat\AnimeClient\API; namespace Aviat\AnimeClient\API;
use Amp\Artax\Request; use Amp\Http\Client\Request;
use Aviat\AnimeClient\Types\FormItemData; use Aviat\AnimeClient\Types\FormItemData;
/** /**

View File

@ -4,7 +4,7 @@
* *
* An API client for Kitsu to manage anime and manga watch lists * An API client for Kitsu to manage anime and manga watch lists
* *
* PHP version 7.3 * PHP version 7.2
* *
* @package HummingbirdAnimeClient * @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net> * @author Timothy J. Warren <tim@timshomepage.net>
@ -21,8 +21,8 @@ use const Aviat\AnimeClient\USER_AGENT;
use function Amp\Promise\wait; use function Amp\Promise\wait;
use function Aviat\AnimeClient\getResponse; use function Aviat\AnimeClient\getResponse;
use Amp\Artax\Request; use Amp\Http\Client\Request;
use Amp\Artax\Response; use Amp\Http\Client\Response;
use Aviat\AnimeClient\API\Anilist; use Aviat\AnimeClient\API\Anilist;
use Aviat\Ion\Json; use Aviat\Ion\Json;
@ -194,7 +194,7 @@ trait AnilistTrait {
$request = $this->mutateRequest($name, $variables); $request = $this->mutateRequest($name, $variables);
$response = $this->getResponseFromRequest($request); $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()));
} }
} }

View File

@ -4,7 +4,7 @@
* *
* An API client for Kitsu to manage anime and manga watch lists * An API client for Kitsu to manage anime and manga watch lists
* *
* PHP version 7.3 * PHP version 7.2
* *
* @package HummingbirdAnimeClient * @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net> * @author Timothy J. Warren <tim@timshomepage.net>
@ -16,7 +16,7 @@
namespace Aviat\AnimeClient\API\Anilist; namespace Aviat\AnimeClient\API\Anilist;
use Amp\Artax\Request; use Amp\Http\Client\Request;
use Aviat\AnimeClient\API\AbstractListItem; use Aviat\AnimeClient\API\AbstractListItem;
use Aviat\AnimeClient\API\Enum\AnimeWatchingStatus\Anilist as AnilistStatus; use Aviat\AnimeClient\API\Enum\AnimeWatchingStatus\Anilist as AnilistStatus;

View File

@ -4,7 +4,7 @@
* *
* An API client for Kitsu to manage anime and manga watch lists * An API client for Kitsu to manage anime and manga watch lists
* *
* PHP version 7.3 * PHP version 7.2
* *
* @package HummingbirdAnimeClient * @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net> * @author Timothy J. Warren <tim@timshomepage.net>
@ -20,7 +20,7 @@ use function Amp\Promise\wait;
use InvalidArgumentException; use InvalidArgumentException;
use Amp\Artax\Request; use Amp\Http\Client\Request;
use Aviat\AnimeClient\API\Anilist; use Aviat\AnimeClient\API\Anilist;
use Aviat\AnimeClient\API\Mapping\{AnimeWatchingStatus, MangaReadingStatus}; use Aviat\AnimeClient\API\Mapping\{AnimeWatchingStatus, MangaReadingStatus};
use Aviat\AnimeClient\Types\FormItem; use Aviat\AnimeClient\Types\FormItem;
@ -79,7 +79,7 @@ final class Model
$response = $this->getResponseFromRequest($request); $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 * this way is more accurate than getting the list item id
* directly from the MAL id * directly from the MAL id
*/ */
private function getListIdFromMediaId(string $mediaId): ?string private function getListIdFromMediaId(string $mediaId): string
{ {
$config = $this->container->get('config'); $config = $this->container->get('config');
$anilistUser = $config->get(['anilist', 'username']); $anilistUser = $config->get(['anilist', 'username']);
@ -286,9 +286,7 @@ final class Model
'userName' => $anilistUser, 'userName' => $anilistUser,
]); ]);
$data = $info['data']['MediaList']; return (string)$info['data']['MediaList']['id'];
return ($data !== NULL) ? (string)$data['id'] : NULL;
} }
/** /**

View File

@ -4,7 +4,7 @@
* *
* An API client for Kitsu to manage anime and manga watch lists * An API client for Kitsu to manage anime and manga watch lists
* *
* PHP version 7.3 * PHP version 7.2
* *
* @package HummingbirdAnimeClient * @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net> * @author Timothy J. Warren <tim@timshomepage.net>
@ -21,8 +21,8 @@ use const Aviat\AnimeClient\SESSION_SEGMENT;
use function Amp\Promise\wait; use function Amp\Promise\wait;
use function Aviat\AnimeClient\getResponse; use function Aviat\AnimeClient\getResponse;
use Amp\Artax\Request; use Amp\Http\Client\Request;
use Amp\Artax\Response; use Amp\Http\Client\Response;
use Aviat\AnimeClient\API\{ use Aviat\AnimeClient\API\{
FailedResponseException, FailedResponseException,
Kitsu as K Kitsu as K
@ -181,7 +181,7 @@ trait KitsuTrait {
try try
{ {
return Json::decode(wait($response->getBody())); return Json::decode(wait($response->getBody()->buffer()));
} }
catch (JsonException $e) catch (JsonException $e)
{ {
@ -241,7 +241,7 @@ trait KitsuTrait {
} }
} }
return JSON::decode(wait($response->getBody()), TRUE); return JSON::decode(wait($response->getBody()->buffer()), TRUE);
} }
/** /**

View File

@ -4,7 +4,7 @@
* *
* An API client for Kitsu to manage anime and manga watch lists * An API client for Kitsu to manage anime and manga watch lists
* *
* PHP version 7.3 * PHP version 7.2
* *
* @package HummingbirdAnimeClient * @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net> * @author Timothy J. Warren <tim@timshomepage.net>
@ -23,7 +23,7 @@ use const Aviat\AnimeClient\SESSION_SEGMENT;
use function Amp\Promise\wait; use function Amp\Promise\wait;
use function Aviat\AnimeClient\getResponse; use function Aviat\AnimeClient\getResponse;
use Amp\Artax\Request; use Amp\Http\Client\Request;
use Aviat\AnimeClient\API\AbstractListItem; use Aviat\AnimeClient\API\AbstractListItem;
use Aviat\AnimeClient\Types\FormItemData; use Aviat\AnimeClient\Types\FormItemData;
use Aviat\Ion\Di\ContainerAware; use Aviat\Ion\Di\ContainerAware;
@ -126,7 +126,7 @@ final class ListItem extends AbstractListItem {
$request = $request->getFullRequest(); $request = $request->getFullRequest();
$response = getResponse($request); $response = getResponse($request);
return Json::decode(wait($response->getBody())); return Json::decode(wait($response->getBody()->buffer()));
} }
public function increment(string $id, FormItemData $data): Request public function increment(string $id, FormItemData $data): Request

View File

@ -4,7 +4,7 @@
* *
* An API client for Kitsu to manage anime and manga watch lists * An API client for Kitsu to manage anime and manga watch lists
* *
* PHP version 7.3 * PHP version 7.2
* *
* @package HummingbirdAnimeClient * @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net> * @author Timothy J. Warren <tim@timshomepage.net>
@ -18,7 +18,7 @@ namespace Aviat\AnimeClient\API\Kitsu;
use function Amp\Promise\wait; use function Amp\Promise\wait;
use Amp\Artax\Request; use Amp\Http\Client\Request;
use Aviat\AnimeClient\API\{ use Aviat\AnimeClient\API\{
CacheTrait, CacheTrait,
JsonAPI, JsonAPI,
@ -126,7 +126,7 @@ final class Model {
'password' => $password 'password' => $password
] ]
]); ]);
$data = Json::decode(wait($response->getBody())); $data = Json::decode(wait($response->getBody()->buffer()));
if (array_key_exists('error', $data)) 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)) if (array_key_exists('access_token', $data))
{ {

View File

@ -4,7 +4,7 @@
* *
* An API client for Kitsu to manage anime and manga watch lists * An API client for Kitsu to manage anime and manga watch lists
* *
* PHP version 7.3 * PHP version 7.2
* *
* @package HummingbirdAnimeClient * @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net> * @author Timothy J. Warren <tim@timshomepage.net>
@ -16,7 +16,7 @@
namespace Aviat\AnimeClient\API; namespace Aviat\AnimeClient\API;
use Amp\Artax\Request; use Amp\Http\Client\Request;
use function Amp\call; use function Amp\call;
use function Amp\Promise\{all, wait}; use function Amp\Promise\{all, wait};
use function Aviat\AnimeClient\getApiClient; use function Aviat\AnimeClient\getApiClient;
@ -82,7 +82,7 @@ final class ParallelAPIRequest {
{ {
$promises[$key] = call(static function () use ($client, $url) { $promises[$key] = call(static function () use ($client, $url) {
$response = yield $client->request($url); $response = yield $client->request($url);
return yield $response->getBody(); return yield $response->getBody()->buffer();
}); });
} }

View File

@ -4,7 +4,7 @@
* *
* An API client for Kitsu to manage anime and manga watch lists * An API client for Kitsu to manage anime and manga watch lists
* *
* PHP version 7.3 * PHP version 7.2
* *
* @package HummingbirdAnimeClient * @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net> * @author Timothy J. Warren <tim@timshomepage.net>
@ -18,7 +18,9 @@ namespace Aviat\AnimeClient;
use function Amp\Promise\wait; 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 Aviat\Ion\ConfigInterface;
use Yosymfony\Toml\{Toml, TomlBuilder}; use Yosymfony\Toml\{Toml, TomlBuilder};
@ -218,15 +220,14 @@ function getApiClient ()
if ($client === NULL) if ($client === NULL)
{ {
$client = new DefaultClient; $client = HttpClientBuilder::buildDefault();
$client->setOption(Client::OP_TRANSFER_TIMEOUT, 0);
} }
return $client; return $client;
} }
/** /**
* Simplify making a request with Artax * Simplify making a request with Http\Client
* *
* @param $request * @param $request
* @return Response * @return Response

View File

@ -4,7 +4,7 @@
* *
* An API client for Kitsu to manage anime and manga watch lists * An API client for Kitsu to manage anime and manga watch lists
* *
* PHP version 7.3 * PHP version 7.2
* *
* @package HummingbirdAnimeClient * @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net> * @author Timothy J. Warren <tim@timshomepage.net>
@ -130,7 +130,7 @@ final class Images extends BaseController {
return; return;
} }
$data = wait($response->getBody()); $data = wait($response->getBody()->buffer());
[$origWidth] = getimagesizefromstring($data); [$origWidth] = getimagesizefromstring($data);
$gdImg = imagecreatefromstring($data); $gdImg = imagecreatefromstring($data);