1
1
<?php
2
2
3
- /*
4
- * This file is part of the Http Adapter package.
5
- *
6
- * (c) Eric GELOEN <[email protected] >
7
- *
8
- * For the full copyright and license information, please read the LICENSE
9
- * file that was distributed with this source code.
10
- */
11
-
12
3
namespace Http \Adapter ;
13
4
14
5
use GuzzleHttp \Client ;
15
6
use GuzzleHttp \ClientInterface ;
16
- use GuzzleHttp \Exception \RequestException ;
17
7
use GuzzleHttp \Message \RequestInterface as GuzzleRequest ;
18
8
use GuzzleHttp \Message \ResponseInterface as GuzzleResponse ;
19
- use GuzzleHttp \ Pool ;
9
+ use Http \ Client \ HttpClient ;
20
10
use Http \Discovery \MessageFactoryDiscovery ;
21
11
use Http \Message \MessageFactory ;
22
12
use Psr \Http \Message \RequestInterface ;
23
13
use Psr \Http \Message \ResponseInterface ;
14
+ use Http \Client \Exception as HttplugException ;
15
+ use GuzzleHttp \Exception as GuzzleExceptions ;
24
16
25
17
/**
26
18
* @author GeLo <[email protected] >
27
19
*/
28
- class Guzzle5HttpAdapter implements HttpAdapter
20
+ class Guzzle5HttpAdapter implements HttpClient
29
21
{
30
22
/**
31
23
* @var ClientInterface
@@ -38,89 +30,48 @@ class Guzzle5HttpAdapter implements HttpAdapter
38
30
private $ messageFactory ;
39
31
40
32
/**
41
- *
42
33
* @param ClientInterface|null $client
43
34
* @param MessageFactory|null $messageFactory
44
35
*/
45
36
public function __construct (ClientInterface $ client = null , MessageFactory $ messageFactory = null )
46
37
{
47
38
$ this ->client = $ client ?: new Client ();
48
39
$ this ->messageFactory = $ messageFactory ?: MessageFactoryDiscovery::find ();
49
-
50
40
}
51
41
52
42
/**
53
43
* {@inheritdoc}
54
44
*/
55
- public function sendRequest (RequestInterface $ request, array $ options = [] )
45
+ public function sendRequest (RequestInterface $ request )
56
46
{
57
- $ guzzleRequest = $ this ->createRequest ($ request, $ options );
47
+ $ guzzleRequest = $ this ->createRequest ($ request );
58
48
59
49
try {
60
50
$ response = $ this ->client ->send ($ guzzleRequest );
61
- } catch (RequestException $ e ) {
62
- throw $ this ->createException ($ e , $ request );
51
+ } catch (GuzzleExceptions \ TransferException $ e ) {
52
+ throw $ this ->handleException ($ e , $ request );
63
53
}
64
54
65
55
return $ this ->createResponse ($ response );
66
56
}
67
57
68
58
/**
69
- * {@inheritdoc}
70
- */
71
- public function sendRequests (array $ requests , array $ options = [])
72
- {
73
- $ requests = array_values ($ requests );
74
- $ guzzleRequests = [];
75
-
76
- foreach ($ requests as $ request ) {
77
- $ guzzleRequests [] = $ this ->createRequest ($ request , $ options );
78
- }
79
-
80
- $ results = Pool::batch ($ this ->client , $ guzzleRequests );
81
-
82
- $ exceptions = [];
83
- $ responses = [];
84
-
85
- foreach ($ guzzleRequests as $ key => $ guzzleRequest ) {
86
- $ result = $ results ->getResult ($ guzzleRequest );
87
-
88
- if ($ result instanceof GuzzleResponse) {
89
- $ responses [] = $ this ->createResponse ($ result );
90
- } elseif ($ result instanceof RequestException) {
91
- $ exceptions [] = $ this ->createException ($ result , $ requests [$ key ]);
92
- }
93
- }
94
-
95
- if (count ($ exceptions ) > 0 ) {
96
- throw new Exception \MultiHttpAdapterException ($ exceptions , $ responses );
97
- }
98
-
99
- return $ responses ;
100
- }
101
-
102
- /**
103
- * {@inheritdoc}
104
- */
105
- public function getName ()
106
- {
107
- return 'guzzle5 ' ;
108
- }
109
-
110
- /**
111
- * Converts a PSR request into a Guzzle request
59
+ * Converts a PSR request into a Guzzle request.
112
60
*
113
61
* @param RequestInterface $request
114
62
*
115
63
* @return GuzzleRequest
116
64
*/
117
- private function createRequest (RequestInterface $ request, array $ options = [] )
65
+ private function createRequest (RequestInterface $ request )
118
66
{
119
- $ options = $ this ->buildOptions ($ options );
67
+ $ options = [
68
+ 'exceptions ' => false ,
69
+ 'allow_redirects ' => false ,
70
+ ];
120
71
121
72
$ options ['version ' ] = $ request ->getProtocolVersion ();
122
73
$ options ['headers ' ] = $ request ->getHeaders ();
123
- $ options ['body ' ] = (string ) $ request ->getBody ();
74
+ $ options ['body ' ] = (string ) $ request ->getBody ();
124
75
125
76
return $ this ->client ->createRequest (
126
77
$ request ->getMethod (),
@@ -130,7 +81,7 @@ private function createRequest(RequestInterface $request, array $options = [])
130
81
}
131
82
132
83
/**
133
- * Converts a Guzzle response into a PSR response
84
+ * Converts a Guzzle response into a PSR response.
134
85
*
135
86
* @param GuzzleResponse $response
136
87
*
@@ -143,60 +94,41 @@ private function createResponse(GuzzleResponse $response)
143
94
return $ this ->messageFactory ->createResponse (
144
95
$ response ->getStatusCode (),
145
96
null ,
146
- $ response ->getProtocolVersion (),
147
97
$ response ->getHeaders (),
148
- isset ($ body ) ? $ body ->detach () : null
98
+ isset ($ body ) ? $ body ->detach () : null ,
99
+ $ response ->getProtocolVersion ()
149
100
);
150
101
}
151
102
152
103
/**
153
- * Converts a Guzzle exception into an HttpAdapter exception
104
+ * Converts a Guzzle exception into an Httplug exception.
154
105
*
155
- * @param RequestException $exception
106
+ * @param GuzzleExceptions\TransferException $exception
107
+ * @param RequestInterface $request
156
108
*
157
- * @return Exception\HttpAdapterException
109
+ * @return HttplugException
158
110
*/
159
- private function createException (
160
- RequestException $ exception ,
161
- RequestInterface $ originalRequest
162
- ) {
163
- $ adapterException = new Exception \HttpAdapterException (
164
- $ exception ->getMessage (),
165
- 0 ,
166
- $ exception
167
- );
168
-
169
- $ response = null ;
170
-
171
- if ($ exception ->hasResponse ()) {
172
- $ response = $ this ->createResponse ($ exception ->getResponse ());
111
+ private function handleException (GuzzleExceptions \TransferException $ exception , RequestInterface $ request )
112
+ {
113
+ if ($ exception instanceof GuzzleExceptions \ConnectException) {
114
+ return new HttplugException \NetworkException ($ exception ->getMessage (), $ request , $ exception );
173
115
}
174
116
175
- $ adapterException ->setResponse ($ response );
176
- $ adapterException ->setRequest ($ originalRequest );
177
-
178
- return $ adapterException ;
179
- }
180
-
181
- /**
182
- * Builds options for Guzzle
183
- *
184
- * @param array $options
185
- *
186
- * @return array
187
- */
188
- private function buildOptions (array $ options )
189
- {
190
- $ guzzleOptions = [
191
- 'exceptions ' => false ,
192
- 'allow_redirects ' => false ,
193
- ];
117
+ if ($ exception instanceof GuzzleExceptions \RequestException) {
118
+ // Make sure we have a response for the HttpException
119
+ if ($ exception ->hasResponse ()) {
120
+ $ psr7Response = $ this ->createResponse ($ exception ->getResponse ());
121
+ return new HttplugException \HttpException (
122
+ $ exception ->getMessage (),
123
+ $ request ,
124
+ $ psr7Response ,
125
+ $ exception
126
+ );
127
+ }
194
128
195
- if (isset ($ options ['timeout ' ])) {
196
- $ guzzleOptions ['connect_timeout ' ] = $ options ['timeout ' ];
197
- $ guzzleOptions ['timeout ' ] = $ options ['timeout ' ];
129
+ return new HttplugException \RequestException ($ exception ->getMessage (), $ request , $ exception );
198
130
}
199
131
200
- return $ guzzleOptions ;
132
+ return new HttplugException \ TransferException ( $ exception -> getMessage (), 0 , $ exception ) ;
201
133
}
202
134
}
0 commit comments