1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author @jayS-de <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Commercetools\Core\Client\Adapter; |
7
|
|
|
|
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
use GuzzleHttp\Exception\RequestException; |
10
|
|
|
use GuzzleHttp\Pool; |
11
|
|
|
use GuzzleHttp\Psr7\Request; |
12
|
|
|
use GuzzleHttp\Psr7\Response; |
13
|
|
|
use GuzzleHttp\Subscriber\Log\LogSubscriber; |
14
|
|
|
use Psr\Http\Message\RequestInterface; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
use Psr\Log\LoggerInterface; |
17
|
|
|
use Commercetools\Core\Error\ApiException; |
18
|
|
|
|
19
|
|
|
class Guzzle5Adapter implements AdapterInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Client |
23
|
|
|
*/ |
24
|
|
|
protected $client; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var LoggerInterface |
28
|
|
|
*/ |
29
|
|
|
protected $logger; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $options |
33
|
|
|
*/ |
34
|
|
|
public function __construct(array $options = []) |
35
|
|
|
{ |
36
|
|
|
if (isset($options['base_uri'])) { |
37
|
|
|
$options['base_url'] = $options['base_uri']; |
38
|
|
|
unset($options['base_uri']); |
39
|
|
|
} |
40
|
|
|
if (isset($options['headers'])) { |
41
|
|
|
$options['defaults']['headers'] = $options['headers']; |
42
|
|
|
unset($options['headers']); |
43
|
|
|
} |
44
|
|
|
$options = array_merge( |
45
|
|
|
[ |
46
|
|
|
'allow_redirects' => false, |
47
|
|
|
'verify' => true, |
48
|
|
|
'timeout' => 60, |
49
|
|
|
'connect_timeout' => 10, |
50
|
|
|
'pool_size' => 25 |
51
|
|
|
], |
52
|
|
|
$options |
53
|
|
|
); |
54
|
|
|
$this->client = new Client($options); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function setLogger(LoggerInterface $logger) |
58
|
|
|
{ |
59
|
|
|
$this->logger = $logger; |
60
|
|
|
if ($logger instanceof LoggerInterface) { |
61
|
|
|
$this->getEmitter()->attach(new LogSubscriber($logger)); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function addHandler($handler) |
66
|
|
|
{ |
67
|
|
|
$this->getEmitter()->attach($handler); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @internal |
72
|
|
|
* @return \GuzzleHttp\Event\Emitter|\GuzzleHttp\Event\EmitterInterface |
73
|
|
|
*/ |
74
|
|
|
public function getEmitter() |
75
|
|
|
{ |
76
|
|
|
return $this->client->getEmitter(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param RequestInterface $request |
81
|
|
|
* @return ResponseInterface |
82
|
|
|
* @throws \Commercetools\Core\Error\ApiException |
83
|
|
|
* @throws \Commercetools\Core\Error\BadGatewayException |
84
|
|
|
* @throws \Commercetools\Core\Error\ConcurrentModificationException |
85
|
|
|
* @throws \Commercetools\Core\Error\ErrorResponseException |
86
|
|
|
* @throws \Commercetools\Core\Error\GatewayTimeoutException |
87
|
|
|
* @throws \Commercetools\Core\Error\InternalServerErrorException |
88
|
|
|
* @throws \Commercetools\Core\Error\InvalidTokenException |
89
|
|
|
* @throws \Commercetools\Core\Error\NotFoundException |
90
|
|
|
* @throws \Commercetools\Core\Error\ServiceUnavailableException |
91
|
|
|
*/ |
92
|
|
|
public function execute(RequestInterface $request) |
93
|
|
|
{ |
94
|
|
|
$options = [ |
95
|
|
|
'headers' => $request->getHeaders(), |
96
|
|
|
'body' => (string)$request->getBody() |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
try { |
100
|
|
|
$guzzleRequest = $this->client->createRequest($request->getMethod(), (string)$request->getUri(), $options); |
|
|
|
|
101
|
|
|
$guzzleResponse = $this->client->send($guzzleRequest); |
102
|
|
|
$response = $this->packResponse($guzzleResponse); |
|
|
|
|
103
|
|
|
} catch (RequestException $exception) { |
104
|
|
|
$response = $this->packResponse($exception->getResponse()); |
|
|
|
|
105
|
|
|
throw ApiException::create($request, $response, $exception); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $response; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function packResponse(\GuzzleHttp\Message\ResponseInterface $response = null) |
112
|
|
|
{ |
113
|
|
|
if (!$response instanceof \GuzzleHttp\Message\ResponseInterface) { |
|
|
|
|
114
|
|
|
return null; |
115
|
|
|
} |
116
|
|
|
return new Response( |
117
|
|
|
$response->getStatusCode(), |
118
|
|
|
$response->getHeaders(), |
119
|
|
|
(string)$response->getBody() |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param RequestInterface[] $requests |
125
|
|
|
* @return \Psr\Http\Message\ResponseInterface[] |
126
|
|
|
* @throws \Commercetools\Core\Error\ApiException |
127
|
|
|
* @throws \Commercetools\Core\Error\BadGatewayException |
128
|
|
|
* @throws \Commercetools\Core\Error\ConcurrentModificationException |
129
|
|
|
* @throws \Commercetools\Core\Error\ErrorResponseException |
130
|
|
|
* @throws \Commercetools\Core\Error\GatewayTimeoutException |
131
|
|
|
* @throws \Commercetools\Core\Error\InternalServerErrorException |
132
|
|
|
* @throws \Commercetools\Core\Error\InvalidTokenException |
133
|
|
|
* @throws \Commercetools\Core\Error\NotFoundException |
134
|
|
|
* @throws \Commercetools\Core\Error\ServiceUnavailableException |
135
|
|
|
*/ |
136
|
|
|
public function executeBatch(array $requests) |
137
|
|
|
{ |
138
|
|
|
$results = Pool::batch( |
139
|
|
|
$this->client, |
140
|
|
|
$this->getBatchHttpRequests($requests) |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
$responses = []; |
144
|
|
|
foreach ($results as $key => $result) { |
145
|
|
|
if (!$result instanceof RequestException) { |
146
|
|
|
$response = $this->packResponse($result); |
147
|
|
View Code Duplication |
} else { |
|
|
|
|
148
|
|
|
$httpResponse = $this->packResponse($result->getResponse()); |
|
|
|
|
149
|
|
|
$request = $requests[$key]; |
150
|
|
|
$response = ApiException::create($request, $httpResponse, $result); |
151
|
|
|
} |
152
|
|
|
$responses[$key] = $response; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $responses; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
|
|
protected function getBatchHttpRequests(array $requests) |
162
|
|
|
{ |
163
|
|
|
$requests = array_map( |
164
|
|
|
function ($request) { |
165
|
|
|
/** |
166
|
|
|
* @var RequestInterface $request |
167
|
|
|
*/ |
168
|
|
|
return $this->client->createRequest( |
|
|
|
|
169
|
|
|
$request->getMethod(), |
170
|
|
|
(string)$request->getUri(), |
171
|
|
|
['headers' => $request->getHeaders()] |
172
|
|
|
); |
173
|
|
|
}, |
174
|
|
|
$requests |
175
|
|
|
); |
176
|
|
|
|
177
|
|
|
return $requests; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param $oauthUri |
182
|
|
|
* @param $clientId |
183
|
|
|
* @param $clientSecret |
184
|
|
|
* @param $formParams |
185
|
|
|
* @return ResponseInterface |
186
|
|
|
*/ |
187
|
|
|
public function authenticate($oauthUri, $clientId, $clientSecret, $formParams) |
188
|
|
|
{ |
189
|
|
|
$options = [ |
190
|
|
|
'body' => $formParams, |
191
|
|
|
'auth' => [$clientId, $clientSecret] |
192
|
|
|
]; |
193
|
|
|
|
194
|
|
|
try { |
195
|
|
|
$response = $this->client->post($oauthUri, $options); |
196
|
|
|
} catch (RequestException $exception) { |
197
|
|
|
$authRequest = $exception->getRequest(); |
198
|
|
|
$request = new Request( |
199
|
|
|
$authRequest->getMethod(), |
200
|
|
|
$authRequest->getUrl(), |
|
|
|
|
201
|
|
|
$authRequest->getHeaders(), |
202
|
|
|
(string)$authRequest->getBody() |
203
|
|
|
); |
204
|
|
|
$response = $this->packResponse($exception->getResponse()); |
|
|
|
|
205
|
|
|
throw ApiException::create($request, $response, $exception); |
206
|
|
|
} |
207
|
|
|
return $response; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param RequestInterface $request |
212
|
|
|
* @return AdapterPromiseInterface |
213
|
|
|
*/ |
214
|
|
|
public function executeAsync(RequestInterface $request) |
215
|
|
|
{ |
216
|
|
|
$options = [ |
217
|
|
|
'future' => true, |
218
|
|
|
'exceptions' => false, |
219
|
|
|
'headers' => $request->getHeaders() |
220
|
|
|
]; |
221
|
|
|
$request = $this->client->createRequest($request->getMethod(), (string)$request->getUri(), $options); |
|
|
|
|
222
|
|
|
$guzzlePromise = $this->client->send($request, $options); |
223
|
|
|
|
224
|
|
|
$promise = new Guzzle5Promise($guzzlePromise); |
|
|
|
|
225
|
|
|
$promise->then( |
226
|
|
|
function (\GuzzleHttp\Message\ResponseInterface $response) { |
227
|
|
|
return new Response( |
228
|
|
|
$response->getStatusCode(), |
229
|
|
|
$response->getHeaders(), |
230
|
|
|
(string)$response->getBody(), |
231
|
|
|
$response->getProtocolVersion(), |
232
|
|
|
$response->getReasonPhrase() |
233
|
|
|
); |
234
|
|
|
} |
235
|
|
|
); |
236
|
|
|
|
237
|
|
|
return $promise; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public static function getAdapterInfo() |
241
|
|
|
{ |
242
|
|
|
return 'GuzzleHttp/' . Client::VERSION; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.