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