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