Blame view

vendor/alibabacloud/client/src/Request/RoaRequest.php 8.84 KB
f0d6cde5   xu   vendor
1
2
3
4
<?php

namespace AlibabaCloud\Client\Request;

0c34aba8   xu   更新阿里云短信接口
5
6
7
8
9
10
11
12
13
14
15
use Exception;
use Stringy\Stringy;
use RuntimeException;
use AlibabaCloud\Client\SDK;
use AlibabaCloud\Client\Encode;
use AlibabaCloud\Client\Accept;
use AlibabaCloud\Client\Support\Path;
use AlibabaCloud\Client\Support\Sign;
use AlibabaCloud\Client\Filter\Filter;
use AlibabaCloud\Client\Support\Arrays;
use AlibabaCloud\Client\Filter\ApiFilter;
f0d6cde5   xu   vendor
16
17
use AlibabaCloud\Client\Credentials\StsCredential;
use AlibabaCloud\Client\Exception\ClientException;
0c34aba8   xu   更新阿里云短信接口
18
19
20
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Client\Credentials\AccessKeyCredential;
use AlibabaCloud\Client\Credentials\BearerTokenCredential;
f0d6cde5   xu   vendor
21
use AlibabaCloud\Client\Request\Traits\DeprecatedRoaTrait;
f0d6cde5   xu   vendor
22
23
24
25
26

/**
 * RESTful ROA Request.
 *
 * @package   AlibabaCloud\Client\Request
0c34aba8   xu   更新阿里云短信接口
27
 * @method setParameter()
f0d6cde5   xu   vendor
28
29
30
31
32
33
34
35
 */
class RoaRequest extends Request
{
    use DeprecatedRoaTrait;

    /**
     * @var string
     */
f0d6cde5   xu   vendor
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    public $pathPattern = '/';

    /**
     * @var array
     */
    public $pathParameters = [];

    /**
     * @var string
     */
    private $dateTimeFormat = "D, d M Y H:i:s \G\M\T";

    /**
     * Resolve request parameter.
     *
f0d6cde5   xu   vendor
51
     * @throws ClientException
0c34aba8   xu   更新阿里云短信接口
52
     * @throws Exception
f0d6cde5   xu   vendor
53
     */
0c34aba8   xu   更新阿里云短信接口
54
    public function resolveParameter()
f0d6cde5   xu   vendor
55
    {
0c34aba8   xu   更新阿里云短信接口
56
57
58
59
60
61
62
63
64
65
66
        $this->resolveQuery();
        $this->resolveHeaders();
        $this->resolveBody();
        $this->resolveUri();
        $this->resolveSignature();
    }

    private function resolveQuery()
    {
        if (!isset($this->options['query']['Version'])) {
            $this->options['query']['Version'] = $this->version;
f0d6cde5   xu   vendor
67
        }
0c34aba8   xu   更新阿里云短信接口
68
69
70
71
72
73
74
75
76
77
78
    }

    private function resolveBody()
    {
        // If the body has already been specified, it will not be resolved.
        if (isset($this->options['body'])) {
            return;
        }

        if (!isset($this->options['form_params'])) {
            return;
f0d6cde5   xu   vendor
79
        }
f0d6cde5   xu   vendor
80

0c34aba8   xu   更新阿里云短信接口
81
82
83
84
85
86
87
88
89
        // Merge data, compatible with parameters set from constructor.
        $params = Arrays::merge(
            [
                $this->data,
                $this->options['form_params']
            ]
        );

        $this->encodeBody($params);
f0d6cde5   xu   vendor
90

0c34aba8   xu   更新阿里云短信接口
91
        unset($this->options['form_params']);
f0d6cde5   xu   vendor
92
93
94
    }

    /**
0c34aba8   xu   更新阿里云短信接口
95
     * Determine the body format based on the Content-Type and calculate the MD5 value.
f0d6cde5   xu   vendor
96
     *
0c34aba8   xu   更新阿里云短信接口
97
     * @param array $params
f0d6cde5   xu   vendor
98
     */
0c34aba8   xu   更新阿里云短信接口
99
    private function encodeBody(array $params)
f0d6cde5   xu   vendor
100
    {
0c34aba8   xu   更新阿里云短信接口
101
102
103
104
105
106
107
        $stringy = Stringy::create($this->options['headers']['Content-Type']);

        if ($stringy->contains('application/json', false)) {
            $this->options['body']                   = json_encode($params);
            $this->options['headers']['Content-MD5'] = base64_encode(md5($this->options['body'], true));

            return;
f0d6cde5   xu   vendor
108
        }
0c34aba8   xu   更新阿里云短信接口
109
110
111
112

        $this->options['body']                    = Encode::create($params)->ksort()->toString();
        $this->options['headers']['Content-MD5']  = base64_encode(md5($this->options['body'], true));
        $this->options['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
f0d6cde5   xu   vendor
113
114
115
    }

    /**
0c34aba8   xu   更新阿里云短信接口
116
117
118
     * @throws ClientException
     * @throws ServerException
     * @throws Exception
f0d6cde5   xu   vendor
119
     */
0c34aba8   xu   更新阿里云短信接口
120
    private function resolveHeaders()
f0d6cde5   xu   vendor
121
    {
0c34aba8   xu   更新阿里云短信接口
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
        $this->options['headers']['x-acs-version']   = $this->version;
        $this->options['headers']['x-acs-region-id'] = $this->realRegionId();
        $this->options['headers']['Date']            = gmdate($this->dateTimeFormat);

        $signature                                           = $this->httpClient()->getSignature();
        $this->options['headers']['x-acs-signature-method']  = $signature->getMethod();
        $this->options['headers']['x-acs-signature-nonce']   = Sign::uuid($this->product . $this->realRegionId());
        $this->options['headers']['x-acs-signature-version'] = $signature->getVersion();
        if ($signature->getType()) {
            $this->options['headers']['x-acs-signature-type'] = $signature->getType();
        }

        $this->resolveAccept();
        $this->resolveContentType();
        $this->resolveSecurityToken();
        $this->resolveBearerToken();
f0d6cde5   xu   vendor
138
139
140
    }

    /**
0c34aba8   xu   更新阿里云短信接口
141
142
     * @throws ClientException
     * @throws Exception
f0d6cde5   xu   vendor
143
     */
0c34aba8   xu   更新阿里云短信接口
144
    private function resolveSignature()
f0d6cde5   xu   vendor
145
    {
0c34aba8   xu   更新阿里云短信接口
146
        $this->options['headers']['Authorization'] = $this->signature();
f0d6cde5   xu   vendor
147
148
149
    }

    /**
0c34aba8   xu   更新阿里云短信接口
150
     * If accept is not specified, it is determined by format.
f0d6cde5   xu   vendor
151
     */
0c34aba8   xu   更新阿里云短信接口
152
    private function resolveAccept()
f0d6cde5   xu   vendor
153
    {
0c34aba8   xu   更新阿里云短信接口
154
155
        if (!isset($this->options['headers']['Accept'])) {
            $this->options['headers']['Accept'] = Accept::create($this->format)->toString();
f0d6cde5   xu   vendor
156
157
158
159
        }
    }

    /**
0c34aba8   xu   更新阿里云短信接口
160
     * If the Content-Type is not specified, it is determined according to accept.
f0d6cde5   xu   vendor
161
     */
0c34aba8   xu   更新阿里云短信接口
162
    private function resolveContentType()
f0d6cde5   xu   vendor
163
    {
0c34aba8   xu   更新阿里云短信接口
164
165
        if (!isset($this->options['headers']['Content-Type'])) {
            $this->options['headers']['Content-Type'] = "{$this->options['headers']['Accept']}; charset=utf-8";
f0d6cde5   xu   vendor
166
        }
0c34aba8   xu   更新阿里云短信接口
167
    }
f0d6cde5   xu   vendor
168

0c34aba8   xu   更新阿里云短信接口
169
170
171
172
173
174
175
176
    /**
     * @throws ClientException
     * @throws ServerException
     */
    private function resolveSecurityToken()
    {
        if (!$this->credential() instanceof StsCredential) {
            return;
f0d6cde5   xu   vendor
177
        }
f0d6cde5   xu   vendor
178

0c34aba8   xu   更新阿里云短信接口
179
180
        if (!$this->credential()->getSecurityToken()) {
            return;
f0d6cde5   xu   vendor
181
        }
f0d6cde5   xu   vendor
182

0c34aba8   xu   更新阿里云短信接口
183
        $this->options['headers']['x-acs-security-token'] = $this->credential()->getSecurityToken();
f0d6cde5   xu   vendor
184
185
186
    }

    /**
0c34aba8   xu   更新阿里云短信接口
187
188
     * @throws ClientException
     * @throws ServerException
f0d6cde5   xu   vendor
189
     */
0c34aba8   xu   更新阿里云短信接口
190
    private function resolveBearerToken()
f0d6cde5   xu   vendor
191
    {
0c34aba8   xu   更新阿里云短信接口
192
193
        if ($this->credential() instanceof BearerTokenCredential) {
            $this->options['headers']['x-acs-bearer-token'] = $this->credential()->getBearerToken();
f0d6cde5   xu   vendor
194
        }
f0d6cde5   xu   vendor
195
196
197
    }

    /**
0c34aba8   xu   更新阿里云短信接口
198
     * Sign the request message.
f0d6cde5   xu   vendor
199
200
     *
     * @return string
0c34aba8   xu   更新阿里云短信接口
201
202
     * @throws ClientException
     * @throws ServerException
f0d6cde5   xu   vendor
203
     */
0c34aba8   xu   更新阿里云短信接口
204
    private function signature()
f0d6cde5   xu   vendor
205
    {
0c34aba8   xu   更新阿里云短信接口
206
207
208
209
210
211
212
213
214
215
216
217
218
        /**
         * @var AccessKeyCredential $credential
         */
        $credential    = $this->credential();
        $access_key_id = $credential->getAccessKeyId();
        $signature     = $this->httpClient()
                              ->getSignature()
                              ->sign(
                                  $this->stringToSign(),
                                  $credential->getAccessKeySecret()
                              );

        return "acs $access_key_id:$signature";
f0d6cde5   xu   vendor
219
220
221
    }

    /**
0c34aba8   xu   更新阿里云短信接口
222
     * @return void
f0d6cde5   xu   vendor
223
     */
0c34aba8   xu   更新阿里云短信接口
224
    private function resolveUri()
f0d6cde5   xu   vendor
225
    {
0c34aba8   xu   更新阿里云短信接口
226
        $path = Path::assign($this->pathPattern, $this->pathParameters);
f0d6cde5   xu   vendor
227

0c34aba8   xu   更新阿里云短信接口
228
229
230
231
232
        $this->uri = $this->uri->withPath($path)
                               ->withQuery(
                                   $this->queryString()
                               );
    }
f0d6cde5   xu   vendor
233

0c34aba8   xu   更新阿里云短信接口
234
235
236
237
238
239
240
241
242
243
    /**
     * @return string
     */
    public function stringToSign()
    {
        $request = new \GuzzleHttp\Psr7\Request(
            $this->method,
            $this->uri,
            $this->options['headers']
        );
f0d6cde5   xu   vendor
244

0c34aba8   xu   更新阿里云短信接口
245
        return Sign::roaString($request);
f0d6cde5   xu   vendor
246
247
248
    }

    /**
0c34aba8   xu   更新阿里云短信接口
249
     * @return bool|string
f0d6cde5   xu   vendor
250
     */
0c34aba8   xu   更新阿里云短信接口
251
    private function queryString()
f0d6cde5   xu   vendor
252
    {
0c34aba8   xu   更新阿里云短信接口
253
254
255
        $query = isset($this->options['query'])
            ? $this->options['query']
            : [];
f0d6cde5   xu   vendor
256

0c34aba8   xu   更新阿里云短信接口
257
        return Encode::create($query)->ksort()->toString();
f0d6cde5   xu   vendor
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
    }

    /**
     * Set path parameter by name.
     *
     * @param string $name
     * @param string $value
     *
     * @return RoaRequest
     * @throws ClientException
     */
    public function pathParameter($name, $value)
    {
        Filter::name($name);

        if ($value === '') {
            throw new ClientException(
                'Value cannot be empty',
                SDK::INVALID_ARGUMENT
            );
        }

        $this->pathParameters[$name] = $value;

        return $this;
    }

    /**
     * Set path pattern.
     *
     * @param string $pattern
     *
     * @return self
     * @throws ClientException
     */
    public function pathPattern($pattern)
    {
        ApiFilter::pattern($pattern);

        $this->pathPattern = $pattern;

        return $this;
    }

    /**
     * Magic method for set or get request parameters.
     *
     * @param string $name
     * @param mixed  $arguments
     *
     * @return $this
     */
    public function __call($name, $arguments)
    {
0c34aba8   xu   更新阿里云短信接口
312
313
        if (strncmp($name, 'get', 3) === 0) {
            $parameter_name = \mb_strcut($name, 3);
f0d6cde5   xu   vendor
314

0c34aba8   xu   更新阿里云短信接口
315
            return $this->__get($parameter_name);
f0d6cde5   xu   vendor
316
317
        }

0c34aba8   xu   更新阿里云短信接口
318
319
320
321
        if (strncmp($name, 'with', 4) === 0) {
            $parameter_name = \mb_strcut($name, 4);
            $this->__set($parameter_name, $arguments[0]);
            $this->pathParameters[$parameter_name] = $arguments[0];
f0d6cde5   xu   vendor
322
323
324
325

            return $this;
        }

0c34aba8   xu   更新阿里云短信接口
326
327
328
        if (strncmp($name, 'set', 3) === 0) {
            $parameter_name = \mb_strcut($name, 3);
            $with_method    = "with$parameter_name";
f0d6cde5   xu   vendor
329

0c34aba8   xu   更新阿里云短信接口
330
            throw new RuntimeException("Please use $with_method instead of $name");
f0d6cde5   xu   vendor
331
332
333
334
335
        }

        throw new RuntimeException('Call to undefined method ' . __CLASS__ . '::' . $name . '()');
    }
}