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