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