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