1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlibabaCloud\Client\Request; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Stringy\Stringy; |
7
|
|
|
use RuntimeException; |
8
|
|
|
use AlibabaCloud\Client\SDK; |
9
|
|
|
use AlibabaCloud\Client\Encode; |
10
|
|
|
use AlibabaCloud\Client\Accept; |
11
|
|
|
use AlibabaCloud\Client\Support\Path; |
12
|
|
|
use AlibabaCloud\Client\Support\Sign; |
13
|
|
|
use AlibabaCloud\Client\Filter\Filter; |
14
|
|
|
use AlibabaCloud\Client\Support\Arrays; |
15
|
|
|
use AlibabaCloud\Client\Filter\ApiFilter; |
16
|
|
|
use AlibabaCloud\Client\Credentials\StsCredential; |
17
|
|
|
use AlibabaCloud\Client\Exception\ClientException; |
18
|
|
|
use AlibabaCloud\Client\Exception\ServerException; |
19
|
|
|
use AlibabaCloud\Client\Credentials\AccessKeyCredential; |
20
|
|
|
use AlibabaCloud\Client\Credentials\BearerTokenCredential; |
21
|
|
|
use AlibabaCloud\Client\Request\Traits\DeprecatedRoaTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* RESTful ROA Request. |
25
|
|
|
* |
26
|
|
|
* @package AlibabaCloud\Client\Request |
27
|
|
|
* @method setParameter() |
28
|
|
|
*/ |
29
|
|
|
class RoaRequest extends Request |
30
|
|
|
{ |
31
|
|
|
use DeprecatedRoaTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
public $pathPattern = '/'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
public $pathParameters = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $dateTimeFormat = "D, d M Y H:i:s \G\M\T"; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Resolve request parameter. |
50
|
|
|
* |
51
|
|
|
* @throws ClientException |
52
|
|
|
* @throws Exception |
53
|
|
|
*/ |
54
|
18 |
|
public function resolveParameter() |
55
|
|
|
{ |
56
|
18 |
|
$this->resolveQuery(); |
57
|
18 |
|
$this->resolveHeaders(); |
58
|
18 |
|
$this->resolveBody(); |
59
|
18 |
|
$this->resolveUri(); |
60
|
18 |
|
$this->resolveSignature(); |
61
|
18 |
|
} |
62
|
|
|
|
63
|
18 |
|
private function resolveQuery() |
64
|
|
|
{ |
65
|
18 |
|
if (!isset($this->options['query']['Version'])) { |
66
|
18 |
|
$this->options['query']['Version'] = $this->version; |
67
|
18 |
|
} |
68
|
18 |
|
} |
69
|
|
|
|
70
|
18 |
|
private function resolveBody() |
71
|
|
|
{ |
72
|
|
|
// If the body has already been specified, it will not be resolved. |
73
|
18 |
|
if (isset($this->options['body'])) { |
74
|
1 |
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
17 |
|
if (!isset($this->options['form_params'])) { |
78
|
14 |
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Merge data, compatible with parameters set from constructor. |
82
|
3 |
|
$params = Arrays::merge( |
83
|
|
|
[ |
84
|
3 |
|
$this->data, |
85
|
3 |
|
$this->options['form_params'] |
86
|
3 |
|
] |
87
|
3 |
|
); |
88
|
|
|
|
89
|
3 |
|
$this->encodeBody($params); |
90
|
|
|
|
91
|
3 |
|
unset($this->options['form_params']); |
92
|
3 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Determine the body format based on the Content-Type and calculate the MD5 value. |
96
|
|
|
* |
97
|
|
|
* @param array $params |
98
|
|
|
*/ |
99
|
3 |
|
private function encodeBody(array $params) |
100
|
|
|
{ |
101
|
3 |
|
$stringy = Stringy::create($this->options['headers']['Content-Type']); |
102
|
|
|
|
103
|
3 |
|
if ($stringy->contains('application/json', false)) { |
104
|
2 |
|
$this->options['body'] = json_encode($params); |
105
|
2 |
|
$this->options['headers']['Content-MD5'] = base64_encode(md5($this->options['body'], true)); |
106
|
|
|
|
107
|
2 |
|
return; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
$this->options['body'] = Encode::create($params)->ksort()->toString(); |
111
|
1 |
|
$this->options['headers']['Content-MD5'] = base64_encode(md5($this->options['body'], true)); |
112
|
1 |
|
$this->options['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'; |
113
|
1 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @throws ClientException |
117
|
|
|
* @throws ServerException |
118
|
|
|
* @throws Exception |
119
|
|
|
*/ |
120
|
18 |
|
private function resolveHeaders() |
121
|
|
|
{ |
122
|
18 |
|
$this->options['headers']['x-acs-version'] = $this->version; |
123
|
18 |
|
$this->options['headers']['x-acs-region-id'] = $this->realRegionId(); |
124
|
18 |
|
$this->options['headers']['Date'] = gmdate($this->dateTimeFormat); |
125
|
|
|
|
126
|
18 |
|
$signature = $this->httpClient()->getSignature(); |
127
|
18 |
|
$this->options['headers']['x-acs-signature-method'] = $signature->getMethod(); |
128
|
18 |
|
$this->options['headers']['x-acs-signature-nonce'] = Sign::uuid($this->product . $this->action); |
129
|
18 |
|
$this->options['headers']['x-acs-signature-version'] = $signature->getVersion(); |
130
|
18 |
|
if ($signature->getType()) { |
131
|
4 |
|
$this->options['headers']['x-acs-signature-type'] = $signature->getType(); |
132
|
4 |
|
} |
133
|
|
|
|
134
|
18 |
|
$this->resolveAccept(); |
135
|
18 |
|
$this->resolveContentType(); |
136
|
18 |
|
$this->resolveSecurityToken(); |
137
|
18 |
|
$this->resolveBearerToken(); |
138
|
18 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @throws ClientException |
142
|
|
|
* @throws Exception |
143
|
|
|
*/ |
144
|
18 |
|
private function resolveSignature() |
145
|
|
|
{ |
146
|
18 |
|
$this->options['headers']['Authorization'] = $this->signature(); |
147
|
18 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* If accept is not specified, it is determined by format. |
151
|
|
|
*/ |
152
|
18 |
|
private function resolveAccept() |
153
|
|
|
{ |
154
|
18 |
|
if (!isset($this->options['headers']['Accept'])) { |
155
|
17 |
|
$this->options['headers']['Accept'] = Accept::create($this->format)->toString(); |
156
|
17 |
|
} |
157
|
18 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* If the Content-Type is not specified, it is determined according to accept. |
161
|
|
|
*/ |
162
|
18 |
|
private function resolveContentType() |
163
|
|
|
{ |
164
|
18 |
|
if (!isset($this->options['headers']['Content-Type'])) { |
165
|
17 |
|
$this->options['headers']['Content-Type'] = "{$this->options['headers']['Accept']}; charset=utf-8"; |
166
|
17 |
|
} |
167
|
18 |
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @throws ClientException |
171
|
|
|
* @throws ServerException |
172
|
|
|
*/ |
173
|
20 |
|
private function resolveSecurityToken() |
174
|
|
|
{ |
175
|
20 |
|
if (!$this->credential() instanceof StsCredential) { |
176
|
18 |
|
return; |
177
|
|
|
} |
178
|
|
|
|
179
|
2 |
|
if (!$this->credential()->getSecurityToken()) { |
180
|
1 |
|
return; |
181
|
|
|
} |
182
|
|
|
|
183
|
1 |
|
$this->options['headers']['x-acs-security-token'] = $this->credential()->getSecurityToken(); |
184
|
1 |
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @throws ClientException |
188
|
|
|
* @throws ServerException |
189
|
|
|
*/ |
190
|
18 |
|
private function resolveBearerToken() |
191
|
|
|
{ |
192
|
18 |
|
if ($this->credential() instanceof BearerTokenCredential) { |
193
|
4 |
|
$this->options['headers']['x-acs-bearer-token'] = $this->credential()->getBearerToken(); |
194
|
4 |
|
} |
195
|
18 |
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Sign the request message. |
199
|
|
|
* |
200
|
|
|
* @return string |
201
|
|
|
* @throws ClientException |
202
|
|
|
* @throws ServerException |
203
|
|
|
*/ |
204
|
18 |
|
private function signature() |
205
|
|
|
{ |
206
|
|
|
/** |
207
|
|
|
* @var AccessKeyCredential $credential |
208
|
|
|
*/ |
209
|
18 |
|
$credential = $this->credential(); |
210
|
18 |
|
$access_key_id = $credential->getAccessKeyId(); |
211
|
18 |
|
$signature = $this->httpClient() |
212
|
18 |
|
->getSignature() |
213
|
18 |
|
->sign( |
214
|
18 |
|
$this->stringToSign(), |
215
|
18 |
|
$credential->getAccessKeySecret() |
216
|
18 |
|
); |
217
|
|
|
|
218
|
18 |
|
return "acs $access_key_id:$signature"; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return void |
223
|
|
|
*/ |
224
|
18 |
|
private function resolveUri() |
225
|
|
|
{ |
226
|
18 |
|
$path = Path::assign($this->pathPattern, $this->pathParameters); |
227
|
|
|
|
228
|
18 |
|
$this->uri = $this->uri->withPath($path) |
229
|
18 |
|
->withQuery( |
230
|
18 |
|
$this->queryString() |
231
|
18 |
|
); |
232
|
18 |
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @return string |
236
|
|
|
*/ |
237
|
18 |
|
public function stringToSign() |
238
|
|
|
{ |
239
|
18 |
|
$request = new \GuzzleHttp\Psr7\Request( |
240
|
18 |
|
$this->method, |
241
|
18 |
|
$this->uri, |
242
|
18 |
|
$this->options['headers'] |
243
|
18 |
|
); |
244
|
|
|
|
245
|
18 |
|
return Sign::roaString($request); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @return bool|string |
250
|
|
|
*/ |
251
|
18 |
|
private function queryString() |
252
|
|
|
{ |
253
|
18 |
|
$query = isset($this->options['query']) |
254
|
18 |
|
? $this->options['query'] |
255
|
18 |
|
: []; |
256
|
|
|
|
257
|
18 |
|
return Encode::create($query)->ksort()->toString(); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Set path parameter by name. |
262
|
|
|
* |
263
|
|
|
* @param string $name |
264
|
|
|
* @param string $value |
265
|
|
|
* |
266
|
|
|
* @return RoaRequest |
267
|
|
|
* @throws ClientException |
268
|
|
|
*/ |
269
|
11 |
|
public function pathParameter($name, $value) |
270
|
|
|
{ |
271
|
11 |
|
Filter::name($name); |
272
|
|
|
|
273
|
9 |
|
if ($value === '') { |
274
|
1 |
|
throw new ClientException( |
275
|
1 |
|
'Value cannot be empty', |
276
|
|
|
SDK::INVALID_ARGUMENT |
277
|
1 |
|
); |
278
|
|
|
} |
279
|
|
|
|
280
|
8 |
|
$this->pathParameters[$name] = $value; |
281
|
|
|
|
282
|
8 |
|
return $this; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Set path pattern. |
287
|
|
|
* |
288
|
|
|
* @param string $pattern |
289
|
|
|
* |
290
|
|
|
* @return self |
291
|
|
|
* @throws ClientException |
292
|
|
|
*/ |
293
|
10 |
|
public function pathPattern($pattern) |
294
|
|
|
{ |
295
|
10 |
|
ApiFilter::pattern($pattern); |
296
|
|
|
|
297
|
8 |
|
$this->pathPattern = $pattern; |
298
|
|
|
|
299
|
8 |
|
return $this; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Magic method for set or get request parameters. |
304
|
|
|
* |
305
|
|
|
* @param string $name |
306
|
|
|
* @param mixed $arguments |
307
|
|
|
* |
308
|
|
|
* @return $this |
309
|
|
|
*/ |
310
|
12 |
|
public function __call($name, $arguments) |
311
|
|
|
{ |
312
|
12 |
|
if (strncmp($name, 'get', 3) === 0) { |
313
|
1 |
|
$parameter_name = \mb_strcut($name, 3); |
314
|
|
|
|
315
|
1 |
|
return $this->__get($parameter_name); |
316
|
|
|
} |
317
|
|
|
|
318
|
12 |
|
if (strncmp($name, 'with', 4) === 0) { |
319
|
10 |
|
$parameter_name = \mb_strcut($name, 4); |
320
|
10 |
|
$this->__set($parameter_name, $arguments[0]); |
321
|
10 |
|
$this->pathParameters[$parameter_name] = $arguments[0]; |
322
|
|
|
|
323
|
10 |
|
return $this; |
324
|
|
|
} |
325
|
|
|
|
326
|
2 |
|
if (strncmp($name, 'set', 3) === 0) { |
327
|
1 |
|
$parameter_name = \mb_strcut($name, 3); |
328
|
1 |
|
$with_method = "with$parameter_name"; |
329
|
|
|
|
330
|
1 |
|
throw new RuntimeException("Please use $with_method instead of $name"); |
331
|
|
|
} |
332
|
|
|
|
333
|
1 |
|
throw new RuntimeException('Call to undefined method ' . __CLASS__ . '::' . $name . '()'); |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|