Start of migration to amphp/http-client 5.0

This commit is contained in:
Timothy Warren 2023-10-26 11:10:21 -04:00
parent b0a16e7730
commit 1ad4427584
4 changed files with 31 additions and 26 deletions

View File

@ -30,7 +30,7 @@
"lock": false "lock": false
}, },
"require": { "require": {
"amphp/http-client": "^4.5.0", "amphp/http-client": "^v5.0.0",
"aura/html": "^2.5.0", "aura/html": "^2.5.0",
"aura/router": "3.2.0", "aura/router": "3.2.0",
"aura/session": "^2.1.0", "aura/session": "^2.1.0",
@ -47,7 +47,7 @@
"maximebf/consolekit": "^1.0.3", "maximebf/consolekit": "^1.0.3",
"monolog/monolog": "^3.0.0", "monolog/monolog": "^3.0.0",
"php": ">= 8.2.0", "php": ">= 8.2.0",
"psr/http-message": "^1.0.1", "psr/http-message": "^1.0.1 || ^2.0.0",
"symfony/polyfill-mbstring": "^1.0.0", "symfony/polyfill-mbstring": "^1.0.0",
"symfony/polyfill-util": "^1.0.0", "symfony/polyfill-util": "^1.0.0",
"tracy/tracy": "^2.8.0", "tracy/tracy": "^2.8.0",

View File

@ -14,6 +14,7 @@
namespace Aviat\AnimeClient; namespace Aviat\AnimeClient;
use Amp\Future;
use Amp\Http\Client\{HttpClient, HttpClientBuilder, Request, Response}; use Amp\Http\Client\{HttpClient, HttpClientBuilder, Request, Response};
use Aviat\Ion\{ConfigInterface, ImageBuilder}; use Aviat\Ion\{ConfigInterface, ImageBuilder};
@ -23,7 +24,7 @@ use Throwable;
use Yosymfony\Toml\{Toml, TomlBuilder}; use Yosymfony\Toml\{Toml, TomlBuilder};
use function Amp\Promise\wait; use function Amp\async;
use function Aviat\Ion\_dir; use function Aviat\Ion\_dir;
const SECONDS_IN_MINUTE = 60; const SECONDS_IN_MINUTE = 60;
@ -210,7 +211,11 @@ function getResponse(Request|string $request): Response
$request = new Request($request); $request = new Request($request);
} }
return wait($client->request($request)); $future = async(fn () => $client->request($request));
[$response] = Future\await([$future]);
return $response;
} }
/** /**

View File

@ -14,17 +14,16 @@
namespace Aviat\AnimeClient\API; namespace Aviat\AnimeClient\API;
// use Amp\Http\Client\Form; use Amp\Future;
use Amp\Http\Client\Body\FormBody; use Amp\Http\Client\Form;
use Amp\Http\Client\{HttpClientBuilder, HttpException, Request}; use Amp\Http\Client\{HttpClientBuilder, HttpException, Request};
use Aviat\Ion\Json; use Aviat\Ion\Json;
use InvalidArgumentException; use InvalidArgumentException;
use Psr\Log\LoggerAwareTrait; use Psr\Log\LoggerAwareTrait;
use Throwable; use Throwable;
// use function Amp\async;
// use function Amp\Future\await; use function Amp\async;
use function Amp\Promise\wait;
use function Aviat\AnimeClient\getResponse; use function Aviat\AnimeClient\getResponse;
use const Aviat\AnimeClient\USER_AGENT; use const Aviat\AnimeClient\USER_AGENT;
@ -110,7 +109,7 @@ abstract class APIRequestBuilder
/** /**
* Set the request body * Set the request body
*/ */
public function setBody(FormBody|string $body): self public function setBody(Form|string $body): self
{ {
$this->request->setBody($body); $this->request->setBody($body);
@ -124,8 +123,9 @@ abstract class APIRequestBuilder
*/ */
public function setFormFields(array $fields): self public function setFormFields(array $fields): self
{ {
$body = new FormBody(); $body = new Form;
$body->addFields($fields);
array_walk($fields, fn ($content, $name) => $body->addField($name, $content));
return $this->setBody($body); return $this->setBody($body);
} }
@ -145,7 +145,12 @@ abstract class APIRequestBuilder
*/ */
public function setHeader(string $name, ?string $value = NULL): self public function setHeader(string $name, ?string $value = NULL): self
{ {
if (NULL === $value) if ($name === '')
{
return $this;
}
if ($value === NULL)
{ {
$this->unsetHeader($name); $this->unsetHeader($name);
} }
@ -164,10 +169,7 @@ abstract class APIRequestBuilder
*/ */
public function setHeaders(array $headers): self public function setHeaders(array $headers): self
{ {
foreach ($headers as $name => $value) array_walk($headers, fn ($value, $name) => $this->setHeader($name, $value));
{
$this->setHeader($name, $value);
}
return $this; return $this;
} }
@ -206,11 +208,9 @@ abstract class APIRequestBuilder
$this->logger?->debug('API Request', [ $this->logger?->debug('API Request', [
'request_url' => $this->request->getUri(), 'request_url' => $this->request->getUri(),
'request_headers' => $this->request->getHeaders(), 'request_headers' => $this->request->getHeaders(),
'request_body' => wait( 'request_body' => $this->request->getBody()
$this->request->getBody() ->getContent()
->createBodyStream() ->read(),
->read()
),
]); ]);
return $this->request; return $this->request;
@ -225,7 +225,7 @@ abstract class APIRequestBuilder
{ {
$response = getResponse($request); $response = getResponse($request);
return wait($response->getBody()->buffer()); return $response->getBody()->buffer();
} }
/** /**

View File

@ -15,6 +15,7 @@
namespace Aviat\AnimeClient\API\Kitsu; namespace Aviat\AnimeClient\API\Kitsu;
use Amp; use Amp;
use Amp\Future;
use Aviat\AnimeClient\API\Kitsu\Transformer\{ use Aviat\AnimeClient\API\Kitsu\Transformer\{
AnimeHistoryTransformer, AnimeHistoryTransformer,
AnimeListTransformer, AnimeListTransformer,
@ -40,7 +41,6 @@ use Aviat\Ion\{
Json Json
}; };
use Generator; use Generator;
use function Amp\Promise\wait;
use function Aviat\AnimeClient\getApiClient; use function Aviat\AnimeClient\getApiClient;
use const Aviat\AnimeClient\SESSION_SEGMENT; use const Aviat\AnimeClient\SESSION_SEGMENT;
@ -87,7 +87,7 @@ final class Model
'password' => $password, 'password' => $password,
], ],
]); ]);
$data = Json::decode(wait($response->getBody()->buffer())); $data = Json::decode($response->getBody()->buffer());
if (array_key_exists('error', $data)) if (array_key_exists('error', $data))
{ {
@ -124,7 +124,7 @@ final class Model
'refresh_token' => $token, 'refresh_token' => $token,
], ],
]); ]);
$data = Json::decode(wait($response->getBody()->buffer())); $data = Json::decode($response->getBody()->buffer());
if (array_key_exists('error', $data)) if (array_key_exists('error', $data))
{ {