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