99use Amp \Http \Client \Request ;
1010use Amp \Http \Client \Response ;
1111use Psr \Http \Client \ClientExceptionInterface ;
12+ use Psr \Http \Message \MessageInterface as PsrMessage ;
1213use Psr \Http \Message \RequestFactoryInterface as PsrRequestFactory ;
1314use Psr \Http \Message \RequestInterface as PsrRequest ;
1415use Psr \Http \Message \ResponseFactoryInterface as PsrResponseFactory ;
1516use Psr \Http \Message \ResponseInterface as PsrResponse ;
1617
18+ /**
19+ * @psalm-import-type ProtocolVersion from Request
20+ */
1721final 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