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