1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlibabaCloud\Client\Request; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use ArrayAccess; |
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use GuzzleHttp\Psr7\Uri; |
9
|
|
|
use GuzzleHttp\Middleware; |
10
|
|
|
use AlibabaCloud\Client\SDK; |
11
|
|
|
use GuzzleHttp\HandlerStack; |
12
|
|
|
use AlibabaCloud\Client\Encode; |
13
|
|
|
use AlibabaCloud\Client\AlibabaCloud; |
14
|
|
|
use AlibabaCloud\Client\Filter\Filter; |
15
|
|
|
use AlibabaCloud\Client\Result\Result; |
16
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
17
|
|
|
use AlibabaCloud\Client\Filter\ApiFilter; |
18
|
|
|
use AlibabaCloud\Client\Log\LogFormatter; |
19
|
|
|
use AlibabaCloud\Client\Traits\HttpTrait; |
20
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
21
|
|
|
use AlibabaCloud\Client\Filter\HttpFilter; |
22
|
|
|
use AlibabaCloud\Client\Traits\RegionTrait; |
23
|
|
|
use AlibabaCloud\Client\Filter\ClientFilter; |
24
|
|
|
use AlibabaCloud\Client\Request\Traits\AcsTrait; |
25
|
|
|
use AlibabaCloud\Client\Traits\ArrayAccessTrait; |
26
|
|
|
use AlibabaCloud\Client\Traits\ObjectAccessTrait; |
27
|
|
|
use AlibabaCloud\Client\Request\Traits\RetryTrait; |
28
|
|
|
use AlibabaCloud\Client\Exception\ClientException; |
29
|
|
|
use AlibabaCloud\Client\Exception\ServerException; |
30
|
|
|
use AlibabaCloud\Client\Request\Traits\MagicTrait; |
31
|
|
|
use AlibabaCloud\Client\Request\Traits\ClientTrait; |
32
|
|
|
use AlibabaCloud\Client\Request\Traits\DeprecatedTrait; |
33
|
|
|
use AlibabaCloud\Client\Credentials\Providers\CredentialsProvider; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Class Request |
37
|
|
|
* |
38
|
|
|
* @package AlibabaCloud\Client\Request |
39
|
|
|
* |
40
|
|
|
* @method string stringToSign() |
41
|
|
|
* @method string resolveParameter() |
42
|
|
|
*/ |
43
|
|
|
abstract class Request implements ArrayAccess |
44
|
|
|
{ |
45
|
|
|
use DeprecatedTrait; |
46
|
|
|
use HttpTrait; |
47
|
|
|
use RegionTrait; |
48
|
|
|
use MagicTrait; |
49
|
|
|
use ClientTrait; |
50
|
|
|
use AcsTrait; |
51
|
|
|
use ArrayAccessTrait; |
52
|
|
|
use ObjectAccessTrait; |
53
|
|
|
use RetryTrait; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Request Connect Timeout |
57
|
|
|
*/ |
58
|
|
|
const CONNECT_TIMEOUT = 5; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Request Timeout |
62
|
|
|
*/ |
63
|
|
|
const TIMEOUT = 10; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var string HTTP Method |
67
|
|
|
*/ |
68
|
|
|
public $method = 'GET'; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
public $format = 'JSON'; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var string HTTP Scheme |
77
|
|
|
*/ |
78
|
|
|
protected $scheme = 'http'; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var string |
82
|
|
|
*/ |
83
|
|
|
public $client; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var Uri |
87
|
|
|
*/ |
88
|
|
|
public $uri; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var array The original parameters of the request. |
92
|
|
|
*/ |
93
|
|
|
public $data = []; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @var array |
97
|
|
|
*/ |
98
|
|
|
private $userAgent = []; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Request constructor. |
102
|
|
|
* |
103
|
|
|
* @param array $options |
104
|
|
|
* |
105
|
|
|
* @throws ClientException |
106
|
|
|
*/ |
107
|
189 |
|
public function __construct(array $options = []) |
108
|
|
|
{ |
109
|
189 |
|
$this->client = CredentialsProvider::getDefaultName(); |
110
|
189 |
|
$this->uri = new Uri(); |
111
|
189 |
|
$this->uri = $this->uri->withScheme($this->scheme); |
112
|
189 |
|
$this->options['http_errors'] = false; |
113
|
189 |
|
$this->options['connect_timeout'] = self::CONNECT_TIMEOUT; |
114
|
189 |
|
$this->options['timeout'] = self::TIMEOUT; |
115
|
189 |
|
if ($options !== []) { |
116
|
1 |
|
$this->options($options); |
117
|
1 |
|
} |
118
|
|
|
|
119
|
189 |
|
if (strtolower(\AlibabaCloud\Client\env('DEBUG')) === 'sdk') { |
120
|
101 |
|
$this->options['debug'] = true; |
121
|
101 |
|
} |
122
|
189 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $name |
126
|
|
|
* @param string $value |
127
|
|
|
* |
128
|
|
|
* @return $this |
129
|
|
|
* @throws ClientException |
130
|
|
|
*/ |
131
|
5 |
|
public function appendUserAgent($name, $value) |
132
|
|
|
{ |
133
|
5 |
|
$filter_name = Filter::name($name); |
134
|
|
|
|
135
|
3 |
|
if (!UserAgent::isGuarded($filter_name)) { |
136
|
3 |
|
$this->userAgent[$filter_name] = Filter::value($value); |
137
|
1 |
|
} |
138
|
|
|
|
139
|
1 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param array $userAgent |
144
|
|
|
* |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
2 |
|
public function withUserAgent(array $userAgent) |
148
|
|
|
{ |
149
|
2 |
|
$this->userAgent = UserAgent::clean($userAgent); |
150
|
|
|
|
151
|
2 |
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set Accept format. |
156
|
|
|
* |
157
|
|
|
* @param string $format |
158
|
|
|
* |
159
|
|
|
* @return $this |
160
|
|
|
* @throws ClientException |
161
|
|
|
*/ |
162
|
14 |
|
public function format($format) |
163
|
|
|
{ |
164
|
14 |
|
$this->format = ApiFilter::format($format); |
165
|
|
|
|
166
|
12 |
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param $contentType |
171
|
|
|
* |
172
|
|
|
* @return $this |
173
|
|
|
* @throws ClientException |
174
|
|
|
*/ |
175
|
1 |
|
public function contentType($contentType) |
176
|
|
|
{ |
177
|
1 |
|
$this->options['headers']['Content-Type'] = HttpFilter::contentType($contentType); |
178
|
|
|
|
179
|
1 |
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param string $accept |
184
|
|
|
* |
185
|
|
|
* @return $this |
186
|
|
|
* @throws ClientException |
187
|
|
|
*/ |
188
|
2 |
|
public function accept($accept) |
189
|
|
|
{ |
190
|
2 |
|
$this->options['headers']['Accept'] = HttpFilter::accept($accept); |
191
|
|
|
|
192
|
2 |
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Set the request body. |
197
|
|
|
* |
198
|
|
|
* @param string $body |
199
|
|
|
* |
200
|
|
|
* @return $this |
201
|
|
|
* @throws ClientException |
202
|
|
|
*/ |
203
|
5 |
|
public function body($body) |
204
|
|
|
{ |
205
|
5 |
|
$this->options['body'] = HttpFilter::body($body); |
206
|
|
|
|
207
|
3 |
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Set the json as body. |
212
|
|
|
* |
213
|
|
|
* @param array|object $content |
214
|
|
|
* |
215
|
|
|
* @return $this |
216
|
|
|
* @throws ClientException |
217
|
|
|
*/ |
218
|
3 |
|
public function jsonBody($content) |
219
|
|
|
{ |
220
|
3 |
|
if (!\is_array($content) && !\is_object($content)) { |
221
|
1 |
|
throw new ClientException( |
222
|
1 |
|
'jsonBody only accepts an array or object', |
223
|
|
|
SDK::INVALID_ARGUMENT |
224
|
1 |
|
); |
225
|
|
|
} |
226
|
|
|
|
227
|
2 |
|
return $this->body(\json_encode($content)); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Set the request scheme. |
232
|
|
|
* |
233
|
|
|
* @param string $scheme |
234
|
|
|
* |
235
|
|
|
* @return $this |
236
|
|
|
* @throws ClientException |
237
|
|
|
*/ |
238
|
17 |
|
public function scheme($scheme) |
239
|
|
|
{ |
240
|
17 |
|
$this->scheme = HttpFilter::scheme($scheme); |
241
|
15 |
|
$this->uri = $this->uri->withScheme($scheme); |
242
|
|
|
|
243
|
15 |
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Set the request host. |
248
|
|
|
* |
249
|
|
|
* @param string $host |
250
|
|
|
* |
251
|
|
|
* @return $this |
252
|
|
|
* @throws ClientException |
253
|
|
|
*/ |
254
|
28 |
|
public function host($host) |
255
|
|
|
{ |
256
|
28 |
|
$this->uri = $this->uri->withHost(HttpFilter::host($host)); |
257
|
|
|
|
258
|
26 |
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param string $method |
263
|
|
|
* |
264
|
|
|
* @return $this |
265
|
|
|
* @throws ClientException |
266
|
|
|
*/ |
267
|
49 |
|
public function method($method) |
268
|
|
|
{ |
269
|
49 |
|
$this->method = HttpFilter::method($method); |
270
|
|
|
|
271
|
47 |
|
return $this; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @param string $clientName |
276
|
|
|
* |
277
|
|
|
* @return $this |
278
|
|
|
* @throws ClientException |
279
|
|
|
*/ |
280
|
67 |
|
public function client($clientName) |
281
|
|
|
{ |
282
|
67 |
|
$this->client = ClientFilter::clientName($clientName); |
283
|
|
|
|
284
|
65 |
|
return $this; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @return bool |
289
|
|
|
* @throws ClientException |
290
|
|
|
*/ |
291
|
1 |
|
public function isDebug() |
292
|
|
|
{ |
293
|
1 |
|
if (isset($this->options['debug'])) { |
294
|
1 |
|
return $this->options['debug'] === true; |
295
|
|
|
} |
296
|
|
|
|
297
|
1 |
|
if (isset($this->httpClient()->options['debug'])) { |
298
|
1 |
|
return $this->httpClient()->options['debug'] === true; |
299
|
|
|
} |
300
|
|
|
|
301
|
1 |
|
return false; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @throws ClientException |
306
|
|
|
* @throws ServerException |
307
|
|
|
*/ |
308
|
66 |
|
public function resolveOption() |
309
|
|
|
{ |
310
|
66 |
|
$this->options['headers']['User-Agent'] = UserAgent::toString($this->userAgent); |
311
|
|
|
|
312
|
66 |
|
$this->cleanQuery(); |
313
|
66 |
|
$this->cleanFormParams(); |
314
|
66 |
|
$this->resolveHost(); |
315
|
63 |
|
$this->resolveParameter(); |
316
|
|
|
|
317
|
60 |
|
if (isset($this->options['form_params'])) { |
318
|
27 |
|
$this->options['form_params'] = \GuzzleHttp\Psr7\parse_query( |
319
|
27 |
|
Encode::create($this->options['form_params'])->toString() |
320
|
27 |
|
); |
321
|
27 |
|
} |
322
|
|
|
|
323
|
60 |
|
$this->mergeOptionsIntoClient(); |
324
|
60 |
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @return Result |
328
|
|
|
* @throws ClientException |
329
|
|
|
* @throws ServerException |
330
|
|
|
*/ |
331
|
64 |
|
public function request() |
332
|
|
|
{ |
333
|
64 |
|
$this->resolveOption(); |
334
|
58 |
|
$result = new Result($this->response(), $this); |
335
|
|
|
|
336
|
55 |
|
if (!$result->isSuccess()) { |
337
|
21 |
|
if ($this->shouldRetry($result)) { |
338
|
2 |
|
$this->retry--; |
339
|
|
|
|
340
|
2 |
|
return $this->request(); |
341
|
|
|
} |
342
|
21 |
|
throw new ServerException($result); |
343
|
|
|
} |
344
|
|
|
|
345
|
35 |
|
return $result; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/*** |
349
|
|
|
* @return PromiseInterface |
350
|
|
|
* @throws Exception |
351
|
|
|
*/ |
352
|
2 |
|
public function requestAsync() |
353
|
|
|
{ |
354
|
2 |
|
$this->resolveOption(); |
355
|
|
|
|
356
|
2 |
|
return self::createClient()->requestAsync( |
357
|
2 |
|
$this->method, |
358
|
2 |
|
(string)$this->uri, |
359
|
2 |
|
$this->options |
360
|
2 |
|
); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Remove redundant Query |
365
|
|
|
* |
366
|
|
|
* @codeCoverageIgnore |
367
|
|
|
*/ |
368
|
|
|
private function cleanQuery() |
369
|
|
|
{ |
370
|
|
|
if (isset($this->options['query']) && $this->options['query'] === []) { |
371
|
|
|
unset($this->options['query']); |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Remove redundant Headers |
377
|
|
|
* |
378
|
|
|
* @codeCoverageIgnore |
379
|
|
|
*/ |
380
|
|
|
private function cleanFormParams() |
381
|
|
|
{ |
382
|
|
|
if (isset($this->options['form_params']) && $this->options['form_params'] === []) { |
383
|
|
|
unset($this->options['form_params']); |
384
|
|
|
} |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* @return Client |
389
|
|
|
* @throws Exception |
390
|
|
|
*/ |
391
|
70 |
|
public static function createClient() |
392
|
|
|
{ |
393
|
70 |
|
if (AlibabaCloud::hasMock()) { |
394
|
22 |
|
$stack = HandlerStack::create(AlibabaCloud::getMock()); |
395
|
22 |
|
} else { |
396
|
48 |
|
$stack = HandlerStack::create(); |
397
|
|
|
} |
398
|
|
|
|
399
|
70 |
|
if (AlibabaCloud::isRememberHistory()) { |
400
|
39 |
|
$stack->push(Middleware::history(AlibabaCloud::referenceHistory())); |
401
|
39 |
|
} |
402
|
|
|
|
403
|
70 |
|
if (AlibabaCloud::getLogger()) { |
404
|
2 |
|
$stack->push(Middleware::log( |
405
|
2 |
|
AlibabaCloud::getLogger(), |
406
|
2 |
|
new LogFormatter(AlibabaCloud::getLogFormat()) |
407
|
2 |
|
)); |
408
|
2 |
|
} |
409
|
|
|
|
410
|
70 |
|
self::$config['handler'] = $stack; |
411
|
|
|
|
412
|
70 |
|
return new Client(self::$config); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* @throws ClientException |
417
|
|
|
* @throws Exception |
418
|
|
|
*/ |
419
|
58 |
|
private function response() |
420
|
|
|
{ |
421
|
|
|
try { |
422
|
58 |
|
return self::createClient()->request( |
423
|
58 |
|
$this->method, |
424
|
58 |
|
(string)$this->uri, |
425
|
58 |
|
$this->options |
426
|
58 |
|
); |
427
|
4 |
|
} catch (GuzzleException $exception) { |
428
|
3 |
|
throw new ClientException( |
429
|
3 |
|
$exception->getMessage(), |
430
|
3 |
|
SDK::SERVER_UNREACHABLE, |
431
|
|
|
$exception |
432
|
3 |
|
); |
433
|
|
|
} |
434
|
|
|
} |
435
|
|
|
} |
436
|
|
|
|