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