18
18
use GuzzleHttp \Exception \RequestException ;
19
19
use GuzzleHttp \Message \RequestInterface ;
20
20
use GuzzleHttp \Pool ;
21
- use Http \Adapter \Message \InternalRequestInterface ;
21
+ use Http \Adapter \Common \Exception \CannotFetchUri ;
22
+ use Http \Adapter \Internal \Message \InternalRequest ;
23
+ use Http \Adapter \Internal \Message \MessageFactory ;
22
24
use Http \Adapter \Normalizer \BodyNormalizer ;
23
25
24
26
/**
25
27
* @author GeLo <[email protected] >
26
28
*/
27
- class Guzzle5HttpAdapter extends CurlHttpAdapter
29
+ class Guzzle5HttpAdapter extends Core \ CurlHttpAdapter
28
30
{
29
31
/**
30
32
* @var ClientInterface
@@ -33,12 +35,13 @@ class Guzzle5HttpAdapter extends CurlHttpAdapter
33
35
34
36
/**
35
37
*
36
- * @param ClientInterface|null $client
37
- * @param ConfigurationInterface|null $configuration
38
+ * @param ClientInterface|null $client
39
+ * @param array $options
40
+ * @param MessageFactory|null $messageFactory
38
41
*/
39
- public function __construct (ClientInterface $ client = null , ConfigurationInterface $ configuration = null )
42
+ public function __construct (ClientInterface $ client = null , array $ options = [], MessageFactory $ messageFactory = null )
40
43
{
41
- parent ::__construct ($ configuration );
44
+ parent ::__construct ($ options , $ messageFactory );
42
45
43
46
$ this ->client = $ client ?: new Client ();
44
47
}
@@ -54,20 +57,17 @@ public function getName()
54
57
/**
55
58
* {@inheritdoc}
56
59
*/
57
- protected function sendInternalRequest (InternalRequestInterface $ internalRequest )
60
+ protected function sendInternalRequest (InternalRequest $ internalRequest )
58
61
{
59
62
try {
60
63
$ response = $ this ->client ->send ($ this ->createRequest ($ internalRequest ));
61
64
} catch (RequestException $ e ) {
62
- throw HttpAdapterException::cannotFetchUri (
63
- $ e ->getRequest ()->getUrl (),
64
- $ this ->getName (),
65
- $ e ->getMessage ()
66
- );
65
+ throw new CannotFetchUri ($ e ->getRequest ()->getUrl (), $ this ->getName (), $ e );
67
66
}
68
67
69
- return $ this ->getConfiguration ()-> getMessageFactory ()->createResponse (
68
+ return $ this ->getMessageFactory ()->createResponse (
70
69
(integer ) $ response ->getStatusCode (),
70
+ null ,
71
71
$ response ->getProtocolVersion (),
72
72
$ response ->getHeaders (),
73
73
BodyNormalizer::normalize (
@@ -82,7 +82,7 @@ function () use ($response) {
82
82
/**
83
83
* {@inheritdoc}
84
84
*/
85
- protected function sendInternalRequests (array $ internalRequests , $ success , $ error )
85
+ protected function sendInternalRequests (array $ internalRequests , callable $ success , callable $ error )
86
86
{
87
87
$ requests = [];
88
88
foreach ($ internalRequests as $ internalRequest ) {
@@ -101,38 +101,39 @@ protected function createFile($file)
101
101
}
102
102
103
103
/**
104
- * Creates a request.
104
+ * Creates a request
105
105
*
106
- * @param InternalRequestInterface $internalRequest
107
- * @param callable|null $success
108
- * @param callable|null $error
106
+ * @param InternalRequest $internalRequest
107
+ * @param callable|null $success
108
+ * @param callable|null $error
109
109
*
110
110
* @return RequestInterface
111
111
*/
112
- private function createRequest (InternalRequestInterface $ internalRequest , callable $ success = null , callable $ error = null )
112
+ private function createRequest (InternalRequest $ internalRequest , callable $ success = null , callable $ error = null )
113
113
{
114
114
$ request = $ this ->client ->createRequest (
115
115
$ internalRequest ->getMethod (),
116
116
(string ) $ internalRequest ->getUri (),
117
117
[
118
118
'exceptions ' => false ,
119
119
'allow_redirects ' => false ,
120
- 'timeout ' => $ this ->getConfiguration ()-> getTimeout ( ),
121
- 'connect_timeout ' => $ this ->getConfiguration ()-> getTimeout ( ),
120
+ 'timeout ' => $ this ->getContextOption ( ' timeout ' , $ internalRequest ),
121
+ 'connect_timeout ' => $ this ->getContextOption ( ' timeout ' , $ internalRequest ),
122
122
'version ' => $ internalRequest ->getProtocolVersion (),
123
123
'headers ' => $ this ->prepareHeaders ($ internalRequest ),
124
124
'body ' => $ this ->prepareContent ($ internalRequest ),
125
125
]
126
126
);
127
127
128
128
if (isset ($ success )) {
129
- $ messageFactory = $ this ->getConfiguration ()-> getMessageFactory ();
129
+ $ messageFactory = $ this ->getMessageFactory ();
130
130
131
131
$ request ->getEmitter ()->on (
132
132
'complete ' ,
133
133
function (CompleteEvent $ event ) use ($ success , $ internalRequest , $ messageFactory ) {
134
134
$ response = $ messageFactory ->createResponse (
135
135
(integer ) $ event ->getResponse ()->getStatusCode (),
136
+ null ,
136
137
$ event ->getResponse ()->getProtocolVersion (),
137
138
$ event ->getResponse ()->getHeaders (),
138
139
BodyNormalizer::normalize (
@@ -143,6 +144,7 @@ function () use ($event) {
143
144
)
144
145
);
145
146
147
+ $ response = new Core \Message \ParameterableResponse ($ response );
146
148
$ response = $ response ->withParameter ('request ' , $ internalRequest );
147
149
call_user_func ($ success , $ response );
148
150
}
@@ -155,10 +157,10 @@ function () use ($event) {
155
157
$ request ->getEmitter ()->on (
156
158
'error ' ,
157
159
function (ErrorEvent $ event ) use ($ error , $ internalRequest , $ httpAdapterName ) {
158
- $ exception = HttpAdapterException:: cannotFetchUri (
160
+ $ exception = new CannotFetchUri (
159
161
$ event ->getException ()->getRequest ()->getUrl (),
160
162
$ httpAdapterName ,
161
- $ event ->getException ()-> getMessage ()
163
+ $ event ->getException ()
162
164
);
163
165
$ exception ->setRequest ($ internalRequest );
164
166
call_user_func ($ error , $ exception );
0 commit comments