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