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
|
157 |
|
public function __construct(array $options = []) |
87
|
|
|
{ |
88
|
157 |
|
$this->uri = new Uri(); |
89
|
157 |
|
$this->uri = $this->uri->withScheme($this->scheme); |
90
|
157 |
|
$this->guzzle = new Client(); |
91
|
157 |
|
$this->options['http_errors'] = false; |
92
|
157 |
|
$this->options['timeout'] = ALIBABA_CLOUD_TIMEOUT; |
93
|
157 |
|
$this->options['connect_timeout'] = ALIBABA_CLOUD_CONNECT_TIMEOUT; |
94
|
|
|
|
95
|
157 |
|
if ($options !== []) { |
96
|
1 |
|
$this->options($options); |
97
|
1 |
|
} |
98
|
157 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $name |
102
|
|
|
* @param string $value |
103
|
|
|
* |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
1 |
|
public function appendUserAgent($name, $value) |
107
|
|
|
{ |
108
|
1 |
|
if (!UserAgent::isGuarded($name)) { |
109
|
1 |
|
$this->userAgent[$name] = $value; |
110
|
1 |
|
} |
111
|
|
|
|
112
|
1 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param array $userAgent |
117
|
|
|
* |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
2 |
|
public function withUserAgent(array $userAgent) |
121
|
|
|
{ |
122
|
2 |
|
$this->userAgent = UserAgent::clean($userAgent); |
123
|
|
|
|
124
|
2 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Set the response data format. |
129
|
|
|
* |
130
|
|
|
* @param string $format |
131
|
|
|
* |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
12 |
|
public function format($format) |
135
|
1 |
|
{ |
136
|
12 |
|
$this->format = \strtoupper($format); |
137
|
|
|
|
138
|
12 |
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set the request body. |
143
|
|
|
* |
144
|
|
|
* @param string $content |
145
|
|
|
* |
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
5 |
|
public function body($content) |
149
|
|
|
{ |
150
|
5 |
|
$this->options['body'] = $content; |
151
|
|
|
|
152
|
5 |
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Set the json as body. |
157
|
|
|
* |
158
|
|
|
* @param array|object $content |
159
|
|
|
* |
160
|
|
|
* @return $this |
161
|
|
|
*/ |
162
|
2 |
|
public function jsonBody($content) |
163
|
|
|
{ |
164
|
2 |
|
if (\is_array($content) || \is_object($content)) { |
165
|
2 |
|
$content = \json_encode($content); |
166
|
2 |
|
} |
167
|
|
|
|
168
|
2 |
|
return $this->body($content); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Set the request scheme. |
173
|
|
|
* |
174
|
|
|
* @param string $scheme |
175
|
|
|
* |
176
|
|
|
* @return $this |
177
|
|
|
*/ |
178
|
26 |
|
public function scheme($scheme) |
179
|
|
|
{ |
180
|
26 |
|
$this->scheme = \strtolower($scheme); |
181
|
26 |
|
$this->uri = $this->uri->withScheme($this->scheme); |
182
|
|
|
|
183
|
26 |
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Set the request host. |
188
|
|
|
* |
189
|
|
|
* @param string $host |
190
|
|
|
* |
191
|
|
|
* @return $this |
192
|
|
|
*/ |
193
|
28 |
|
public function host($host) |
194
|
|
|
{ |
195
|
28 |
|
$this->uri = $this->uri->withHost($host); |
196
|
|
|
|
197
|
28 |
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param string $method |
202
|
|
|
* |
203
|
|
|
* @return $this |
204
|
|
|
*/ |
205
|
65 |
|
public function method($method) |
206
|
|
|
{ |
207
|
65 |
|
$this->method = \strtoupper($method); |
208
|
|
|
|
209
|
65 |
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param string $clientName |
214
|
|
|
* |
215
|
|
|
* @return $this |
216
|
|
|
*/ |
217
|
80 |
|
public function client($clientName) |
218
|
|
|
{ |
219
|
80 |
|
$this->client = $clientName; |
220
|
|
|
|
221
|
80 |
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return bool |
226
|
|
|
* @throws ClientException |
227
|
|
|
*/ |
228
|
1 |
|
public function isDebug() |
229
|
|
|
{ |
230
|
1 |
|
if (isset($this->options['debug'])) { |
231
|
1 |
|
return $this->options['debug'] === true; |
232
|
|
|
} |
233
|
|
|
|
234
|
1 |
|
if (isset($this->httpClient()->options['debug'])) { |
235
|
1 |
|
return $this->httpClient()->options['debug'] === true; |
236
|
|
|
} |
237
|
|
|
|
238
|
1 |
|
return false; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return Result |
243
|
|
|
* @throws ClientException |
244
|
|
|
* @throws ServerException |
245
|
|
|
*/ |
246
|
69 |
|
public function request() |
247
|
|
|
{ |
248
|
69 |
|
$this->options['headers']['User-Agent'] = UserAgent::toString($this->userAgent); |
249
|
|
|
|
250
|
69 |
|
$this->resolveUri(); |
251
|
|
|
|
252
|
66 |
|
$this->resolveParameters($this->credential()); |
253
|
|
|
|
254
|
50 |
|
if (isset($this->options['form_params'])) { |
255
|
28 |
|
$this->options['form_params'] = \GuzzleHttp\Psr7\parse_query( |
256
|
28 |
|
self::getPostHttpBody($this->options['form_params']) |
257
|
28 |
|
); |
258
|
28 |
|
} |
259
|
|
|
|
260
|
50 |
|
$this->mergeOptionsIntoClient(); |
261
|
|
|
|
262
|
50 |
|
$result = new Result($this->response(), $this); |
263
|
|
|
|
264
|
44 |
|
if (!$result->isSuccess()) { |
265
|
28 |
|
throw new ServerException($result); |
266
|
|
|
} |
267
|
|
|
|
268
|
16 |
|
return $result; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @param array $post |
273
|
|
|
* |
274
|
|
|
* @return bool|string |
275
|
|
|
*/ |
276
|
31 |
|
public static function getPostHttpBody(array $post) |
277
|
|
|
{ |
278
|
31 |
|
$content = ''; |
279
|
31 |
|
foreach ($post as $apiKey => $apiValue) { |
280
|
31 |
|
$content .= "$apiKey=" . urlencode($apiValue) . '&'; |
281
|
31 |
|
} |
282
|
|
|
|
283
|
31 |
|
return substr($content, 0, -1); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @throws ClientException |
288
|
|
|
*/ |
289
|
50 |
|
private function response() |
290
|
|
|
{ |
291
|
|
|
try { |
292
|
50 |
|
return $this->guzzle->request( |
293
|
50 |
|
$this->method, |
294
|
50 |
|
(string)$this->uri, |
295
|
50 |
|
$this->options |
296
|
50 |
|
); |
297
|
6 |
|
} catch (GuzzleException $e) { |
298
|
6 |
|
throw new ClientException( |
299
|
6 |
|
$e->getMessage(), |
300
|
6 |
|
\ALIBABA_CLOUD_SERVER_UNREACHABLE, |
301
|
|
|
$e |
302
|
6 |
|
); |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @return string |
308
|
|
|
*/ |
309
|
28 |
|
public function stringToBeSigned() |
310
|
|
|
{ |
311
|
28 |
|
return $this->stringToBeSigned; |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|