Skip to content

Commit 394f2c5

Browse files
committed
Re-write protocol version 2.0 to 2
Fixes #15.
1 parent 6c7589a commit 394f2c5

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/PsrAdapter.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99
use Amp\Http\Client\Request;
1010
use Amp\Http\Client\Response;
1111
use Psr\Http\Client\ClientExceptionInterface;
12+
use Psr\Http\Message\MessageInterface as PsrMessage;
1213
use Psr\Http\Message\RequestFactoryInterface as PsrRequestFactory;
1314
use Psr\Http\Message\RequestInterface as PsrRequest;
1415
use Psr\Http\Message\ResponseFactoryInterface as PsrResponseFactory;
1516
use Psr\Http\Message\ResponseInterface as PsrResponse;
1617

18+
/**
19+
* @psalm-import-type ProtocolVersion from Request
20+
*/
1721
final class PsrAdapter
1822
{
1923
public function __construct(
@@ -27,18 +31,16 @@ public function fromPsrRequest(PsrRequest $source): Request
2731
/** @psalm-suppress ArgumentTypeCoercion Wrong typehints in PSR */
2832
$target = new Request($source->getUri(), $source->getMethod());
2933
$target->setHeaders($source->getHeaders());
30-
/** @psalm-suppress ArgumentTypeCoercion Wrong typehints in PSR */
31-
$target->setProtocolVersions([$source->getProtocolVersion()]);
34+
$target->setProtocolVersions([$this->getProtocolVersion($source)]);
3235
$target->setBody(new PsrStreamBody($source->getBody()));
3336

3437
return $target;
3538
}
3639

3740
public function fromPsrResponse(PsrResponse $source, Request $request, ?Response $previousResponse = null): Response
3841
{
39-
/** @psalm-suppress ArgumentTypeCoercion Wrong typehints in PSR */
4042
return new Response(
41-
$source->getProtocolVersion(),
43+
$this->getProtocolVersion($source),
4244
$source->getStatusCode(),
4345
$source->getReasonPhrase(),
4446
$source->getHeaders(),
@@ -113,4 +115,18 @@ private function toPsrRequestWithoutBody(
113115

114116
return $target;
115117
}
118+
119+
/**
120+
* @return ProtocolVersion
121+
*/
122+
private function getProtocolVersion(PsrMessage $source): string
123+
{
124+
$protocolVersion = $source->getProtocolVersion();
125+
126+
return match ($protocolVersion) {
127+
'2.0' => '2',
128+
'2', '1.1', '1.0' => $protocolVersion,
129+
default => throw new \Error('Invalid protocol version: ' . $protocolVersion),
130+
};
131+
}
116132
}

0 commit comments

Comments
 (0)