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