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