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