From 394f2c59e31eaf16f9e64b1317324e26b7f8e4e4 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sun, 10 Nov 2024 09:48:46 -0600 Subject: [PATCH] Re-write protocol version 2.0 to 2 Fixes #15. --- src/PsrAdapter.php | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/PsrAdapter.php b/src/PsrAdapter.php index afb18c6..1fc1b7b 100644 --- a/src/PsrAdapter.php +++ b/src/PsrAdapter.php @@ -9,11 +9,15 @@ use Amp\Http\Client\Request; use Amp\Http\Client\Response; use Psr\Http\Client\ClientExceptionInterface; +use Psr\Http\Message\MessageInterface as PsrMessage; use Psr\Http\Message\RequestFactoryInterface as PsrRequestFactory; use Psr\Http\Message\RequestInterface as PsrRequest; use Psr\Http\Message\ResponseFactoryInterface as PsrResponseFactory; use Psr\Http\Message\ResponseInterface as PsrResponse; +/** + * @psalm-import-type ProtocolVersion from Request + */ final class PsrAdapter { public function __construct( @@ -27,8 +31,7 @@ public function fromPsrRequest(PsrRequest $source): Request /** @psalm-suppress ArgumentTypeCoercion Wrong typehints in PSR */ $target = new Request($source->getUri(), $source->getMethod()); $target->setHeaders($source->getHeaders()); - /** @psalm-suppress ArgumentTypeCoercion Wrong typehints in PSR */ - $target->setProtocolVersions([$source->getProtocolVersion()]); + $target->setProtocolVersions([$this->getProtocolVersion($source)]); $target->setBody(new PsrStreamBody($source->getBody())); return $target; @@ -36,9 +39,8 @@ public function fromPsrRequest(PsrRequest $source): Request public function fromPsrResponse(PsrResponse $source, Request $request, ?Response $previousResponse = null): Response { - /** @psalm-suppress ArgumentTypeCoercion Wrong typehints in PSR */ return new Response( - $source->getProtocolVersion(), + $this->getProtocolVersion($source), $source->getStatusCode(), $source->getReasonPhrase(), $source->getHeaders(), @@ -113,4 +115,18 @@ private function toPsrRequestWithoutBody( return $target; } + + /** + * @return ProtocolVersion + */ + private function getProtocolVersion(PsrMessage $source): string + { + $protocolVersion = $source->getProtocolVersion(); + + return match ($protocolVersion) { + '2.0' => '2', + '2', '1.1', '1.0' => $protocolVersion, + default => throw new \Error('Invalid protocol version: ' . $protocolVersion), + }; + } }