1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlibabaCloud\Client\Request; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Ramsey\Uuid\Uuid; |
7
|
|
|
use RuntimeException; |
8
|
|
|
use AlibabaCloud\Client\Credentials\StsCredential; |
9
|
|
|
use AlibabaCloud\Client\Exception\ClientException; |
10
|
|
|
use AlibabaCloud\Client\Exception\ServerException; |
11
|
|
|
use AlibabaCloud\Client\Credentials\BearerTokenCredential; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* RESTful RPC Request. |
15
|
|
|
* |
16
|
|
|
* @package AlibabaCloud\Client\Request |
17
|
|
|
*/ |
18
|
|
|
class RpcRequest extends Request |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $dateTimeFormat = 'Y-m-d\TH:i:s\Z'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Resolve request parameter. |
28
|
|
|
* |
29
|
|
|
* @throws ClientException |
30
|
|
|
*/ |
31
|
50 |
|
public function resolveParameters() |
32
|
|
|
{ |
33
|
50 |
|
$this->resolveBoolInParameters(); |
34
|
50 |
|
$this->resolveCommonParameters(); |
35
|
47 |
|
$this->repositionParameters(); |
36
|
47 |
|
} |
37
|
|
|
|
38
|
50 |
|
private function resolveBoolInParameters() |
39
|
|
|
{ |
40
|
50 |
|
if (isset($this->options['query'])) { |
41
|
25 |
|
$this->options['query'] = array_map( |
42
|
25 |
|
static function($value) { |
43
|
25 |
|
return self::boolToString($value); |
44
|
25 |
|
}, |
45
|
25 |
|
$this->options['query'] |
46
|
25 |
|
); |
47
|
25 |
|
} |
48
|
50 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Convert a Boolean value to a string. |
52
|
|
|
* |
53
|
|
|
* @param bool|string $value |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
32 |
|
public static function boolToString($value) |
58
|
|
|
{ |
59
|
32 |
|
if (is_bool($value)) { |
60
|
3 |
|
return $value ? 'true' : 'false'; |
61
|
|
|
} |
62
|
|
|
|
63
|
29 |
|
return $value; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Resolve Common Parameters. |
68
|
|
|
* |
69
|
|
|
* @throws ClientException |
70
|
|
|
* @throws Exception |
71
|
|
|
*/ |
72
|
52 |
|
private function resolveCommonParameters() |
73
|
|
|
{ |
74
|
52 |
|
$signature = $this->httpClient()->getSignature(); |
75
|
52 |
|
$this->options['query']['RegionId'] = $this->realRegionId(); |
76
|
52 |
|
$this->options['query']['Format'] = $this->format; |
77
|
52 |
|
$this->options['query']['SignatureMethod'] = $signature->getMethod(); |
78
|
52 |
|
$this->options['query']['SignatureVersion'] = $signature->getVersion(); |
79
|
52 |
|
$this->options['query']['SignatureNonce'] = Uuid::uuid1()->toString(); |
80
|
52 |
|
$this->options['query']['Timestamp'] = gmdate($this->dateTimeFormat); |
81
|
52 |
|
$this->options['query']['Action'] = $this->action; |
82
|
52 |
|
if ($this->credential()->getAccessKeyId()) { |
|
|
|
|
83
|
43 |
|
$this->options['query']['AccessKeyId'] = $this->credential()->getAccessKeyId(); |
84
|
43 |
|
} |
85
|
50 |
|
if ($signature->getType()) { |
86
|
12 |
|
$this->options['query']['SignatureType'] = $signature->getType(); |
87
|
12 |
|
} |
88
|
50 |
|
if (!isset($this->options['query']['Version'])) { |
89
|
50 |
|
$this->options['query']['Version'] = $this->version; |
90
|
50 |
|
} |
91
|
50 |
|
$this->resolveSecurityToken(); |
92
|
50 |
|
$this->resolveBearerToken(); |
93
|
50 |
|
$this->options['query']['Signature'] = $this->signature(); |
94
|
49 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @throws ClientException |
98
|
|
|
* @throws ServerException |
99
|
|
|
*/ |
100
|
50 |
|
private function resolveSecurityToken() |
101
|
|
|
{ |
102
|
50 |
|
if ($this->credential() instanceof StsCredential && $this->credential()->getSecurityToken()) { |
103
|
|
|
$this->options['query']['SecurityToken'] = $this->credential()->getSecurityToken(); |
104
|
|
|
} |
105
|
50 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @throws ClientException |
109
|
|
|
* @throws ServerException |
110
|
|
|
*/ |
111
|
50 |
|
private function resolveBearerToken() |
112
|
|
|
{ |
113
|
50 |
|
if ($this->credential() instanceof BearerTokenCredential) { |
114
|
7 |
|
$this->options['query']['BearerToken'] = $this->credential()->getBearerToken(); |
115
|
7 |
|
} |
116
|
50 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Sign the parameters. |
120
|
|
|
* |
121
|
|
|
* @return mixed |
122
|
|
|
* @throws ClientException |
123
|
|
|
* @throws ServerException |
124
|
|
|
*/ |
125
|
52 |
|
private function signature() |
126
|
|
|
{ |
127
|
52 |
|
return $this->httpClient() |
128
|
52 |
|
->getSignature() |
129
|
52 |
|
->sign( |
130
|
52 |
|
$this->stringToSign(), |
131
|
52 |
|
$this->credential()->getAccessKeySecret() . '&' |
|
|
|
|
132
|
52 |
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
52 |
|
public function stringToSign() |
139
|
|
|
{ |
140
|
52 |
|
$query = isset($this->options['query']) ? $this->options['query'] : []; |
141
|
52 |
|
$formParams = isset($this->options['form_params']) ? $this->options['form_params'] : []; |
142
|
52 |
|
$parameters = \AlibabaCloud\Client\arrayMerge([$query, $formParams]); |
143
|
52 |
|
ksort($parameters); |
144
|
52 |
|
$canonicalizedQuery = ''; |
145
|
52 |
|
foreach ($parameters as $key => $value) { |
146
|
52 |
|
$canonicalizedQuery .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value); |
147
|
52 |
|
} |
148
|
|
|
|
149
|
52 |
|
return $this->method . '&%2F&' . $this->percentEncode(substr($canonicalizedQuery, 1)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $string |
154
|
|
|
* |
155
|
|
|
* @return null|string|string[] |
156
|
|
|
*/ |
157
|
55 |
|
private function percentEncode($string) |
158
|
|
|
{ |
159
|
55 |
|
$result = urlencode($string); |
160
|
55 |
|
$result = str_replace(['+', '*'], ['%20', '%2A'], $result); |
161
|
55 |
|
$result = preg_replace('/%7E/', '~', $result); |
162
|
|
|
|
163
|
55 |
|
return $result; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Adjust parameter position |
168
|
|
|
*/ |
169
|
47 |
|
private function repositionParameters() |
170
|
|
|
{ |
171
|
47 |
|
if ($this->method === 'POST' || $this->method === 'PUT') { |
172
|
23 |
|
foreach ($this->options['query'] as $apiParamKey => $apiParamValue) { |
173
|
23 |
|
$this->options['form_params'][$apiParamKey] = $apiParamValue; |
174
|
23 |
|
} |
175
|
23 |
|
unset($this->options['query']); |
176
|
23 |
|
} |
177
|
47 |
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Magic method for set or get request parameters. |
181
|
|
|
* |
182
|
|
|
* @param string $name |
183
|
|
|
* @param mixed $arguments |
184
|
|
|
* |
185
|
|
|
* @return $this |
186
|
|
|
*/ |
187
|
3 |
|
public function __call($name, $arguments) |
188
|
|
|
{ |
189
|
3 |
|
if (strncmp($name, 'get', 3) === 0) { |
190
|
1 |
|
$parameterName = $this->propertyNameByMethodName($name); |
191
|
|
|
|
192
|
1 |
|
return $this->__get($parameterName); |
193
|
|
|
} |
194
|
|
|
|
195
|
3 |
|
if (strncmp($name, 'with', 4) === 0) { |
196
|
1 |
|
$parameterName = $this->propertyNameByMethodName($name, 4); |
197
|
1 |
|
$this->__set($parameterName, $arguments[0]); |
198
|
1 |
|
$this->options['query'][$parameterName] = $arguments[0]; |
199
|
|
|
|
200
|
1 |
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
2 |
|
if (strncmp($name, 'set', 3) === 0) { |
204
|
1 |
|
$parameterName = $this->propertyNameByMethodName($name); |
205
|
1 |
|
$withMethod = "with$parameterName"; |
206
|
|
|
|
207
|
1 |
|
throw new RuntimeException("Please use $withMethod instead of $name"); |
208
|
|
|
} |
209
|
|
|
|
210
|
1 |
|
throw new RuntimeException('Call to undefined method ' . __CLASS__ . '::' . $name . '()'); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|