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