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