9
9
use Amp \Http \Client \Request ;
10
10
use Amp \Http \Client \Response ;
11
11
use Psr \Http \Client \ClientExceptionInterface ;
12
+ use Psr \Http \Message \MessageInterface as PsrMessage ;
12
13
use Psr \Http \Message \RequestFactoryInterface as PsrRequestFactory ;
13
14
use Psr \Http \Message \RequestInterface as PsrRequest ;
14
15
use Psr \Http \Message \ResponseFactoryInterface as PsrResponseFactory ;
15
16
use Psr \Http \Message \ResponseInterface as PsrResponse ;
16
17
18
+ /**
19
+ * @psalm-import-type ProtocolVersion from Request
20
+ */
17
21
final class PsrAdapter
18
22
{
19
23
public function __construct (
@@ -27,18 +31,16 @@ public function fromPsrRequest(PsrRequest $source): Request
27
31
/** @psalm-suppress ArgumentTypeCoercion Wrong typehints in PSR */
28
32
$ target = new Request ($ source ->getUri (), $ source ->getMethod ());
29
33
$ target ->setHeaders ($ source ->getHeaders ());
30
- /** @psalm-suppress ArgumentTypeCoercion Wrong typehints in PSR */
31
- $ target ->setProtocolVersions ([$ source ->getProtocolVersion ()]);
34
+ $ target ->setProtocolVersions ([$ this ->getProtocolVersion ($ source )]);
32
35
$ target ->setBody (new PsrStreamBody ($ source ->getBody ()));
33
36
34
37
return $ target ;
35
38
}
36
39
37
40
public function fromPsrResponse (PsrResponse $ source , Request $ request , ?Response $ previousResponse = null ): Response
38
41
{
39
- /** @psalm-suppress ArgumentTypeCoercion Wrong typehints in PSR */
40
42
return new Response (
41
- $ source ->getProtocolVersion (),
43
+ $ this ->getProtocolVersion ($ source ),
42
44
$ source ->getStatusCode (),
43
45
$ source ->getReasonPhrase (),
44
46
$ source ->getHeaders (),
@@ -113,4 +115,18 @@ private function toPsrRequestWithoutBody(
113
115
114
116
return $ target ;
115
117
}
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
+ }
116
132
}
0 commit comments