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\ApiFilter; |
9
|
|
|
use AlibabaCloud\Client\Filter\ClientFilter; |
10
|
|
|
use AlibabaCloud\Client\Filter\Filter; |
11
|
|
|
use AlibabaCloud\Client\Filter\HttpFilter; |
12
|
|
|
use AlibabaCloud\Client\Http\GuzzleTrait; |
13
|
|
|
use AlibabaCloud\Client\Request\Traits\AcsTrait; |
14
|
|
|
use AlibabaCloud\Client\Request\Traits\ClientTrait; |
15
|
|
|
use AlibabaCloud\Client\Request\Traits\DeprecatedTrait; |
16
|
|
|
use AlibabaCloud\Client\Request\Traits\MagicTrait; |
17
|
|
|
use AlibabaCloud\Client\Result\Result; |
18
|
|
|
use AlibabaCloud\Client\Traits\ArrayAccessTrait; |
19
|
|
|
use AlibabaCloud\Client\Traits\ObjectAccessTrait; |
20
|
|
|
use GuzzleHttp\Client; |
21
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
22
|
|
|
use GuzzleHttp\Psr7\Uri; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Request |
26
|
|
|
* |
27
|
|
|
* @package AlibabaCloud\Client\Request |
28
|
|
|
* |
29
|
|
|
* @method string resolveParameters($credential) |
30
|
|
|
*/ |
31
|
|
|
abstract class Request implements \ArrayAccess |
32
|
|
|
{ |
33
|
|
|
use DeprecatedTrait; |
34
|
|
|
use GuzzleTrait; |
35
|
|
|
use MagicTrait; |
36
|
|
|
use ClientTrait; |
37
|
|
|
use AcsTrait; |
38
|
|
|
use ArrayAccessTrait; |
39
|
|
|
use ObjectAccessTrait; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
public static $config = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
public $scheme = 'http'; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
public $method = 'GET'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
public $format = 'JSON'; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
public $client; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var Uri |
68
|
|
|
*/ |
69
|
|
|
public $uri; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var Client |
73
|
|
|
*/ |
74
|
|
|
public $guzzle; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var array The original parameters of the request. |
78
|
|
|
*/ |
79
|
|
|
public $data = []; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var string |
83
|
|
|
*/ |
84
|
|
|
protected $stringToBeSigned = ''; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var array |
88
|
|
|
*/ |
89
|
|
|
private $userAgent = []; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Request constructor. |
93
|
|
|
* |
94
|
|
|
* @param array $options |
95
|
|
|
* |
96
|
|
|
* @throws ClientException |
97
|
|
|
*/ |
98
|
204 |
|
public function __construct(array $options = []) |
99
|
|
|
{ |
100
|
204 |
|
$this->client = CredentialsProvider::getDefaultName(); |
101
|
204 |
|
$this->uri = new Uri(); |
102
|
204 |
|
$this->uri = $this->uri->withScheme($this->scheme); |
103
|
204 |
|
$this->guzzle = new Client(self::$config); |
104
|
204 |
|
$this->options['http_errors'] = false; |
105
|
204 |
|
$this->options['timeout'] = ALIBABA_CLOUD_TIMEOUT; |
106
|
204 |
|
$this->options['connect_timeout'] = ALIBABA_CLOUD_CONNECT_TIMEOUT; |
107
|
204 |
|
if ($options !== []) { |
108
|
1 |
|
$this->options($options); |
109
|
1 |
|
} |
110
|
|
|
|
111
|
204 |
|
if (strtolower(\AlibabaCloud\Client\env('DEBUG')) === 'sdk') { |
112
|
1 |
|
$this->options['debug'] = true; |
113
|
1 |
|
} |
114
|
204 |
|
} |
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
|
15 |
|
public function format($format) |
157
|
|
|
{ |
158
|
15 |
|
ApiFilter::format($format); |
159
|
|
|
|
160
|
11 |
|
$this->format = \strtoupper($format); |
161
|
|
|
|
162
|
11 |
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Set the request body. |
167
|
|
|
* |
168
|
|
|
* @param string $body |
169
|
|
|
* |
170
|
|
|
* @return $this |
171
|
|
|
* @throws ClientException |
172
|
|
|
*/ |
173
|
7 |
|
public function body($body) |
174
|
|
|
{ |
175
|
7 |
|
HttpFilter::body($body); |
176
|
|
|
|
177
|
5 |
|
$this->options['body'] = $body; |
178
|
|
|
|
179
|
5 |
|
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
|
|
|
\ALIBABA_CLOUD_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
|
32 |
|
public function scheme($scheme) |
211
|
|
|
{ |
212
|
32 |
|
HttpFilter::scheme($scheme); |
213
|
|
|
|
214
|
30 |
|
$this->scheme = \strtolower($scheme); |
215
|
30 |
|
$this->uri = $this->uri->withScheme($this->scheme); |
216
|
|
|
|
217
|
30 |
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Set the request host. |
222
|
|
|
* |
223
|
|
|
* @param string $host |
224
|
|
|
* |
225
|
|
|
* @return $this |
226
|
|
|
* @throws ClientException |
227
|
|
|
*/ |
228
|
34 |
|
public function host($host) |
229
|
|
|
{ |
230
|
34 |
|
HttpFilter::host($host); |
231
|
|
|
|
232
|
32 |
|
$this->uri = $this->uri->withHost($host); |
233
|
|
|
|
234
|
32 |
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param string $method |
239
|
|
|
* |
240
|
|
|
* @return $this |
241
|
|
|
* @throws ClientException |
242
|
|
|
*/ |
243
|
73 |
|
public function method($method) |
244
|
|
|
{ |
245
|
73 |
|
$this->method = HttpFilter::method($method); |
246
|
|
|
|
247
|
71 |
|
return $this; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param string $clientName |
252
|
|
|
* |
253
|
|
|
* @return $this |
254
|
|
|
* @throws ClientException |
255
|
|
|
*/ |
256
|
88 |
|
public function client($clientName) |
257
|
|
|
{ |
258
|
88 |
|
ClientFilter::clientName($clientName); |
259
|
|
|
|
260
|
86 |
|
$this->client = $clientName; |
261
|
|
|
|
262
|
86 |
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @return bool |
267
|
|
|
* @throws ClientException |
268
|
|
|
*/ |
269
|
1 |
|
public function isDebug() |
270
|
|
|
{ |
271
|
1 |
|
if (isset($this->options['debug'])) { |
272
|
1 |
|
return $this->options['debug'] === true; |
273
|
|
|
} |
274
|
|
|
|
275
|
1 |
|
if (isset($this->httpClient()->options['debug'])) { |
276
|
1 |
|
return $this->httpClient()->options['debug'] === true; |
277
|
|
|
} |
278
|
|
|
|
279
|
1 |
|
return false; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @return Result |
284
|
|
|
* @throws ClientException |
285
|
|
|
* @throws ServerException |
286
|
|
|
*/ |
287
|
77 |
|
public function request() |
288
|
|
|
{ |
289
|
77 |
|
$this->options['headers']['User-Agent'] = UserAgent::toString($this->userAgent); |
290
|
|
|
|
291
|
77 |
|
$this->removeRedundantQuery(); |
292
|
77 |
|
$this->removeRedundantHeaders(); |
293
|
77 |
|
$this->removeRedundantHFormParams(); |
294
|
77 |
|
$this->resolveUri(); |
295
|
74 |
|
$this->resolveParameters($this->credential()); |
296
|
|
|
|
297
|
57 |
|
if (isset($this->options['form_params'])) { |
298
|
31 |
|
$this->options['form_params'] = \GuzzleHttp\Psr7\parse_query( |
299
|
31 |
|
self::getPostHttpBody($this->options['form_params']) |
300
|
31 |
|
); |
301
|
31 |
|
} |
302
|
|
|
|
303
|
57 |
|
$this->mergeOptionsIntoClient(); |
304
|
|
|
|
305
|
57 |
|
$result = new Result($this->response(), $this); |
306
|
|
|
|
307
|
47 |
|
if (!$result->isSuccess()) { |
308
|
23 |
|
throw new ServerException($result); |
309
|
|
|
} |
310
|
|
|
|
311
|
24 |
|
return $result; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Remove redundant Query |
316
|
|
|
* |
317
|
|
|
* @codeCoverageIgnore |
318
|
|
|
*/ |
319
|
|
|
private function removeRedundantQuery() |
320
|
|
|
{ |
321
|
|
|
if (isset($this->options['query']) && $this->options['query'] === []) { |
322
|
|
|
unset($this->options['query']); |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Remove redundant Headers |
328
|
|
|
* |
329
|
|
|
* @codeCoverageIgnore |
330
|
|
|
*/ |
331
|
|
|
private function removeRedundantHeaders() |
332
|
|
|
{ |
333
|
|
|
if (isset($this->options['headers']) && $this->options['headers'] === []) { |
334
|
|
|
unset($this->options['headers']); |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Remove redundant Headers |
340
|
|
|
* |
341
|
|
|
* @codeCoverageIgnore |
342
|
|
|
*/ |
343
|
|
|
private function removeRedundantHFormParams() |
344
|
|
|
{ |
345
|
|
|
if (isset($this->options['form_params']) && $this->options['form_params'] === []) { |
346
|
|
|
unset($this->options['form_params']); |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @param array $post |
352
|
|
|
* |
353
|
|
|
* @return bool|string |
354
|
|
|
*/ |
355
|
34 |
|
public static function getPostHttpBody(array $post) |
356
|
|
|
{ |
357
|
34 |
|
$content = ''; |
358
|
34 |
|
foreach ($post as $apiKey => $apiValue) { |
359
|
34 |
|
$content .= "$apiKey=" . urlencode($apiValue) . '&'; |
360
|
34 |
|
} |
361
|
|
|
|
362
|
34 |
|
return substr($content, 0, -1); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* @throws ClientException |
367
|
|
|
*/ |
368
|
57 |
|
private function response() |
369
|
|
|
{ |
370
|
|
|
try { |
371
|
57 |
|
return $this->guzzle->request( |
372
|
57 |
|
$this->method, |
373
|
57 |
|
(string)$this->uri, |
374
|
57 |
|
$this->options |
375
|
57 |
|
); |
376
|
10 |
|
} catch (GuzzleException $e) { |
377
|
10 |
|
throw new ClientException( |
378
|
10 |
|
$e->getMessage(), |
379
|
10 |
|
\ALIBABA_CLOUD_SERVER_UNREACHABLE, |
380
|
|
|
$e |
381
|
10 |
|
); |
382
|
|
|
} |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* @return string |
387
|
|
|
*/ |
388
|
25 |
|
public function stringToBeSigned() |
389
|
|
|
{ |
390
|
25 |
|
return $this->stringToBeSigned; |
391
|
|
|
} |
392
|
|
|
} |
393
|
|
|
|