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