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