|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AlibabaCloud\Client\Request; |
|
4
|
|
|
|
|
5
|
|
|
use AlibabaCloud\Client\Credentials\AccessKeyCredential; |
|
6
|
|
|
use AlibabaCloud\Client\Credentials\BearerTokenCredential; |
|
7
|
|
|
use AlibabaCloud\Client\Credentials\CredentialsInterface; |
|
8
|
|
|
use AlibabaCloud\Client\Credentials\StsCredential; |
|
9
|
|
|
use AlibabaCloud\Client\Exception\ClientException; |
|
10
|
|
|
use AlibabaCloud\Client\Filter\ApiFilter; |
|
11
|
|
|
use AlibabaCloud\Client\Filter\Filter; |
|
12
|
|
|
use AlibabaCloud\Client\Request\Traits\DeprecatedRoaTrait; |
|
13
|
|
|
use AlibabaCloud\Client\SDK; |
|
14
|
|
|
use RuntimeException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* RESTful ROA Request. |
|
18
|
|
|
* |
|
19
|
|
|
* @package AlibabaCloud\Client\Request |
|
20
|
|
|
*/ |
|
21
|
|
|
class RoaRequest extends Request |
|
22
|
|
|
{ |
|
23
|
|
|
use DeprecatedRoaTrait; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private static $headerSeparator = "\n"; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private static $querySeparator = '&'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
public $pathPattern = '/'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
public $pathParameters = []; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
private $dateTimeFormat = "D, d M Y H:i:s \G\M\T"; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Resolve request parameter. |
|
52
|
|
|
* |
|
53
|
|
|
* @param AccessKeyCredential|BearerTokenCredential|StsCredential|CredentialsInterface $credential |
|
54
|
|
|
* |
|
55
|
|
|
* @throws ClientException |
|
56
|
|
|
*/ |
|
57
|
17 |
|
public function resolveParameters($credential) |
|
58
|
|
|
{ |
|
59
|
17 |
|
$signature = $this->httpClient()->getSignature(); |
|
60
|
17 |
|
$this->options['query']['Version'] = $this->version; |
|
61
|
17 |
|
$this->options['headers']['x-acs-version'] = $this->version; |
|
62
|
17 |
|
$this->options['headers']['Date'] = gmdate($this->dateTimeFormat); |
|
63
|
17 |
|
$this->options['headers']['Accept'] = self::formatToAccept($this->format); |
|
64
|
17 |
|
$this->options['headers']['x-acs-signature-method'] = $signature->getMethod(); |
|
65
|
17 |
|
$this->options['headers']['x-acs-signature-version'] = $signature->getVersion(); |
|
66
|
17 |
|
if ($signature->getType()) { |
|
67
|
4 |
|
$this->options['headers']['x-acs-signature-type'] = $signature->getType(); |
|
68
|
4 |
|
} |
|
69
|
17 |
|
$this->options['headers']['x-acs-region-id'] = $this->realRegionId(); |
|
70
|
17 |
|
if (isset($this->options['form_params'])) { |
|
71
|
2 |
|
$this->options['headers']['Content-MD5'] = $this->contentMD5(); |
|
72
|
2 |
|
} |
|
73
|
17 |
|
$this->options['headers']['Content-Type'] = "{$this->options['headers']['Accept']};chrset=utf-8"; |
|
74
|
|
|
|
|
75
|
17 |
|
$this->resolveSecurityToken($credential); |
|
76
|
17 |
|
$this->resolveBearerToken($credential); |
|
77
|
|
|
|
|
78
|
17 |
|
$this->options['headers']['Authorization'] = $this->signature($credential); |
|
79
|
17 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Returns the accept header according to format. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $format |
|
85
|
|
|
* |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
21 |
|
private static function formatToAccept($format) |
|
89
|
|
|
{ |
|
90
|
21 |
|
switch (\strtoupper($format)) { |
|
91
|
21 |
|
case 'JSON': |
|
92
|
18 |
|
return 'application/json'; |
|
93
|
3 |
|
case 'XML': |
|
94
|
1 |
|
return 'application/xml'; |
|
95
|
2 |
|
default: |
|
96
|
2 |
|
return 'application/octet-stream'; |
|
97
|
2 |
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Calculate the md5 value of the content. |
|
102
|
|
|
* |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
3 |
|
private function contentMD5() |
|
106
|
|
|
{ |
|
107
|
3 |
|
return base64_encode( |
|
108
|
3 |
|
md5(json_encode($this->options['form_params']), true) |
|
109
|
3 |
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param CredentialsInterface $credential |
|
114
|
|
|
*/ |
|
115
|
17 |
|
private function resolveSecurityToken(CredentialsInterface $credential) |
|
116
|
|
|
{ |
|
117
|
17 |
|
if ($credential instanceof StsCredential && $credential->getSecurityToken()) { |
|
118
|
1 |
|
$this->options['headers']['x-acs-security-token'] = $credential->getSecurityToken(); |
|
119
|
1 |
|
} |
|
120
|
17 |
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param CredentialsInterface $credential |
|
124
|
|
|
*/ |
|
125
|
17 |
|
private function resolveBearerToken(CredentialsInterface $credential) |
|
126
|
|
|
{ |
|
127
|
17 |
|
if ($credential instanceof BearerTokenCredential) { |
|
128
|
3 |
|
$this->options['headers']['x-acs-bearer-token'] = $credential->getBearerToken(); |
|
129
|
3 |
|
} |
|
130
|
17 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return string |
|
134
|
|
|
*/ |
|
135
|
17 |
|
private function headerStringToSign() |
|
136
|
|
|
{ |
|
137
|
17 |
|
$string = $this->method . self::$headerSeparator; |
|
138
|
17 |
|
if (isset($this->options['headers']['Accept'])) { |
|
139
|
17 |
|
$string .= $this->options['headers']['Accept']; |
|
140
|
17 |
|
} |
|
141
|
17 |
|
$string .= self::$headerSeparator; |
|
142
|
|
|
|
|
143
|
17 |
|
if (isset($this->options['headers']['Content-MD5'])) { |
|
144
|
2 |
|
$string .= $this->options['headers']['Content-MD5']; |
|
145
|
2 |
|
} |
|
146
|
17 |
|
$string .= self::$headerSeparator; |
|
147
|
|
|
|
|
148
|
17 |
|
if (isset($this->options['headers']['Content-Type'])) { |
|
149
|
17 |
|
$string .= $this->options['headers']['Content-Type']; |
|
150
|
17 |
|
} |
|
151
|
17 |
|
$string .= self::$headerSeparator; |
|
152
|
|
|
|
|
153
|
17 |
|
if (isset($this->options['headers']['Date'])) { |
|
154
|
17 |
|
$string .= $this->options['headers']['Date']; |
|
155
|
17 |
|
} |
|
156
|
17 |
|
$string .= self::$headerSeparator; |
|
157
|
|
|
|
|
158
|
17 |
|
$string .= $this->constructAcsHeader(); |
|
159
|
|
|
|
|
160
|
17 |
|
return $string; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return string |
|
165
|
|
|
*/ |
|
166
|
17 |
|
private function resourceStringToSign() |
|
167
|
|
|
{ |
|
168
|
17 |
|
$this->uri = $this->uri->withPath($this->assignPathParameters()) |
|
169
|
17 |
|
->withQuery($this->queryString()); |
|
170
|
|
|
|
|
171
|
17 |
|
return $this->uri->getPath() . '?' . $this->uri->getQuery(); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @return string |
|
176
|
|
|
*/ |
|
177
|
17 |
|
public function stringToBeSigned() |
|
178
|
|
|
{ |
|
179
|
17 |
|
return $this->headerStringToSign() . $this->resourceStringToSign(); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Sign the request message. |
|
184
|
|
|
* |
|
185
|
|
|
* @param AccessKeyCredential|BearerTokenCredential|StsCredential $credential |
|
186
|
|
|
* |
|
187
|
|
|
* @return string |
|
188
|
|
|
* @throws ClientException |
|
189
|
|
|
*/ |
|
190
|
17 |
|
private function signature($credential) |
|
191
|
|
|
{ |
|
192
|
17 |
|
$accessKeyId = $credential->getAccessKeyId(); |
|
193
|
17 |
|
$signature = $this->httpClient() |
|
194
|
17 |
|
->getSignature() |
|
195
|
17 |
|
->sign( |
|
196
|
17 |
|
$this->stringToBeSigned(), |
|
197
|
17 |
|
$credential->getAccessKeySecret() |
|
198
|
17 |
|
); |
|
199
|
|
|
|
|
200
|
17 |
|
return "acs $accessKeyId:$signature"; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Construct standard Header for Alibaba Cloud. |
|
205
|
|
|
* |
|
206
|
|
|
* @return string |
|
207
|
|
|
*/ |
|
208
|
17 |
|
private function constructAcsHeader() |
|
209
|
|
|
{ |
|
210
|
17 |
|
$sortMap = []; |
|
211
|
17 |
|
foreach ($this->options['headers'] as $headerKey => $headerValue) { |
|
212
|
17 |
|
$key = strtolower($headerKey); |
|
213
|
17 |
|
if (strpos($key, 'x-acs-') === 0) { |
|
214
|
17 |
|
$sortMap[$key] = $headerValue; |
|
215
|
17 |
|
} |
|
216
|
17 |
|
} |
|
217
|
17 |
|
ksort($sortMap); |
|
218
|
17 |
|
$headerString = ''; |
|
219
|
17 |
|
foreach ($sortMap as $sortMapKey => $sortMapValue) { |
|
220
|
17 |
|
$headerString .= $sortMapKey . ':' . $sortMapValue . self::$headerSeparator; |
|
221
|
17 |
|
} |
|
222
|
|
|
|
|
223
|
17 |
|
return $headerString; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Assign path parameters to the url. |
|
228
|
|
|
* |
|
229
|
|
|
* @return string |
|
230
|
|
|
*/ |
|
231
|
19 |
|
private function assignPathParameters() |
|
232
|
|
|
{ |
|
233
|
19 |
|
$result = $this->pathPattern; |
|
234
|
19 |
|
foreach ($this->pathParameters as $pathParameterKey => $apiParameterValue) { |
|
235
|
12 |
|
$target = '[' . $pathParameterKey . ']'; |
|
236
|
12 |
|
$result = str_replace($target, $apiParameterValue, $result); |
|
237
|
19 |
|
} |
|
238
|
|
|
|
|
239
|
19 |
|
return $result; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Get the query string. |
|
244
|
|
|
* |
|
245
|
|
|
* @return bool|mixed|string |
|
246
|
|
|
*/ |
|
247
|
17 |
|
public function queryString() |
|
248
|
|
|
{ |
|
249
|
17 |
|
$query = isset($this->options['query']) |
|
250
|
17 |
|
? $this->options['query'] |
|
251
|
17 |
|
: []; |
|
252
|
|
|
|
|
253
|
17 |
|
$queryString = $this->ksort($queryString, $query); |
|
254
|
|
|
|
|
255
|
17 |
|
if (0 < count($query)) { |
|
256
|
17 |
|
$queryString = substr($queryString, 0, -1); |
|
257
|
17 |
|
} |
|
258
|
|
|
|
|
259
|
17 |
|
return $queryString; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Sort the entries by key. |
|
264
|
|
|
* |
|
265
|
|
|
* @param string $queryString |
|
266
|
|
|
* @param array $map |
|
267
|
|
|
* |
|
268
|
|
|
* @return string |
|
269
|
|
|
*/ |
|
270
|
17 |
|
private function ksort(&$queryString, array $map) |
|
271
|
|
|
{ |
|
272
|
17 |
|
ksort($map); |
|
273
|
17 |
|
foreach ($map as $sortMapKey => $sortMapValue) { |
|
274
|
17 |
|
$queryString .= $sortMapKey; |
|
275
|
17 |
|
if ($sortMapValue !== null) { |
|
276
|
17 |
|
$queryString .= '=' . $sortMapValue; |
|
277
|
17 |
|
} |
|
278
|
17 |
|
$queryString .= self::$querySeparator; |
|
279
|
17 |
|
} |
|
280
|
|
|
|
|
281
|
17 |
|
return $queryString; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Set path parameter by name. |
|
286
|
|
|
* |
|
287
|
|
|
* @param string $name |
|
288
|
|
|
* @param string $value |
|
289
|
|
|
* |
|
290
|
|
|
* @return RoaRequest |
|
291
|
|
|
* @throws ClientException |
|
292
|
|
|
*/ |
|
293
|
16 |
|
public function pathParameter($name, $value) |
|
294
|
|
|
{ |
|
295
|
16 |
|
Filter::name($name); |
|
296
|
|
|
|
|
297
|
14 |
|
if ($value === '') { |
|
298
|
1 |
|
throw new ClientException( |
|
299
|
1 |
|
'Value cannot be empty', |
|
300
|
|
|
SDK::INVALID_ARGUMENT |
|
301
|
1 |
|
); |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
13 |
|
$this->pathParameters[$name] = $value; |
|
305
|
|
|
|
|
306
|
13 |
|
return $this; |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* Set path pattern. |
|
311
|
|
|
* |
|
312
|
|
|
* @param string $pattern |
|
313
|
|
|
* |
|
314
|
|
|
* @return self |
|
315
|
|
|
* @throws ClientException |
|
316
|
|
|
*/ |
|
317
|
10 |
|
public function pathPattern($pattern) |
|
318
|
|
|
{ |
|
319
|
10 |
|
ApiFilter::pattern($pattern); |
|
320
|
|
|
|
|
321
|
8 |
|
$this->pathPattern = $pattern; |
|
322
|
|
|
|
|
323
|
8 |
|
return $this; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* Magic method for set or get request parameters. |
|
328
|
|
|
* |
|
329
|
|
|
* @param string $name |
|
330
|
|
|
* @param mixed $arguments |
|
331
|
|
|
* |
|
332
|
|
|
* @return $this |
|
333
|
|
|
*/ |
|
334
|
12 |
|
public function __call($name, $arguments) |
|
335
|
|
|
{ |
|
336
|
12 |
|
if (\strpos($name, 'get') === 0) { |
|
337
|
1 |
|
$parameterName = $this->propertyNameByMethodName($name); |
|
338
|
|
|
|
|
339
|
1 |
|
return $this->__get($parameterName); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
12 |
|
if (\strpos($name, 'with') === 0) { |
|
343
|
11 |
|
$parameterName = $this->propertyNameByMethodName($name, 4); |
|
344
|
11 |
|
$this->__set($parameterName, $arguments[0]); |
|
345
|
11 |
|
$this->pathParameters[$parameterName] = $arguments[0]; |
|
346
|
|
|
|
|
347
|
11 |
|
return $this; |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
2 |
|
if (\strpos($name, 'set') === 0) { |
|
351
|
1 |
|
$parameterName = $this->propertyNameByMethodName($name); |
|
352
|
1 |
|
$withMethod = "with$parameterName"; |
|
353
|
|
|
|
|
354
|
1 |
|
return $this->$withMethod($arguments[0]); |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
1 |
|
throw new RuntimeException('Call to undefined method ' . __CLASS__ . '::' . $name . '()'); |
|
358
|
|
|
} |
|
359
|
|
|
} |
|
360
|
|
|
|