1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlibabaCloud\Client\Request; |
4
|
|
|
|
5
|
|
|
use AlibabaCloud\Client\Exception\ClientException; |
6
|
|
|
use AlibabaCloud\Client\Exception\ServerException; |
7
|
|
|
use AlibabaCloud\Client\Http\GuzzleTrait; |
8
|
|
|
use AlibabaCloud\Client\Request\Traits\AcsTrait; |
9
|
|
|
use AlibabaCloud\Client\Request\Traits\ClientTrait; |
10
|
|
|
use AlibabaCloud\Client\Request\Traits\DeprecatedTrait; |
11
|
|
|
use AlibabaCloud\Client\Request\Traits\MagicTrait; |
12
|
|
|
use AlibabaCloud\Client\Result\Result; |
13
|
|
|
use AlibabaCloud\Client\Traits\ArrayAccessTrait; |
14
|
|
|
use AlibabaCloud\Client\Traits\ObjectAccessTrait; |
15
|
|
|
use GuzzleHttp\Client; |
16
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
17
|
|
|
use GuzzleHttp\Psr7\Uri; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Request |
21
|
|
|
* |
22
|
|
|
* @package AlibabaCloud\Client\Request |
23
|
|
|
* |
24
|
|
|
* @method string resolveParameters($credential) |
25
|
|
|
*/ |
26
|
|
|
abstract class Request implements \ArrayAccess |
27
|
|
|
{ |
28
|
|
|
use DeprecatedTrait; |
29
|
|
|
use GuzzleTrait; |
30
|
|
|
use MagicTrait; |
31
|
|
|
use ClientTrait; |
32
|
|
|
use AcsTrait; |
33
|
|
|
use ArrayAccessTrait; |
34
|
|
|
use ObjectAccessTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
public $scheme = 'http'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
public $method = 'GET'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
public $format = 'JSON'; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
public $client = \ALIBABA_CLOUD_GLOBAL_CLIENT; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var Uri |
58
|
|
|
*/ |
59
|
|
|
public $uri; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var Client |
63
|
|
|
*/ |
64
|
|
|
public $guzzle; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var array The original parameters of the request. |
68
|
|
|
*/ |
69
|
|
|
public $data = []; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var string |
73
|
|
|
*/ |
74
|
|
|
protected $stringToBeSigned = ''; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Request constructor. |
78
|
|
|
* |
79
|
|
|
* @param array $options |
80
|
|
|
*/ |
81
|
155 |
|
public function __construct(array $options = []) |
82
|
|
|
{ |
83
|
155 |
|
$this->uri = new Uri(); |
84
|
155 |
|
$this->uri = $this->uri->withScheme($this->scheme); |
85
|
155 |
|
$this->guzzle = new Client(); |
86
|
155 |
|
$this->options['http_errors'] = false; |
87
|
155 |
|
$this->options['timeout'] = ALIBABA_CLOUD_TIMEOUT; |
88
|
155 |
|
$this->options['connect_timeout'] = ALIBABA_CLOUD_CONNECT_TIMEOUT; |
89
|
155 |
|
if ($options !== []) { |
90
|
1 |
|
$this->options($options); |
91
|
1 |
|
} |
92
|
155 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $name |
96
|
|
|
* @param string $value |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
1 |
|
public function userAgentAppend($name, $value) |
101
|
|
|
{ |
102
|
1 |
|
UserAgent::append($name, $value); |
103
|
1 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Set the response data format. |
108
|
|
|
* |
109
|
|
|
* @param string $format |
110
|
|
|
* |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
12 |
|
public function format($format) |
114
|
|
|
{ |
115
|
12 |
|
$this->format = \strtoupper($format); |
116
|
12 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set the request body. |
121
|
|
|
* |
122
|
|
|
* @param string $content |
123
|
|
|
* |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
5 |
|
public function body($content) |
127
|
|
|
{ |
128
|
5 |
|
$this->options['body'] = $content; |
129
|
5 |
|
return $this; |
130
|
1 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Set the json as body. |
134
|
|
|
* |
135
|
|
|
* @param array|object $content |
136
|
|
|
* |
137
|
|
|
* @return $this |
138
|
|
|
*/ |
139
|
2 |
|
public function jsonBody($content) |
140
|
|
|
{ |
141
|
2 |
|
if (\is_array($content) || \is_object($content)) { |
142
|
2 |
|
$content = \json_encode($content); |
143
|
2 |
|
} |
144
|
2 |
|
return $this->body($content); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set the request scheme. |
149
|
|
|
* |
150
|
|
|
* @param string $scheme |
151
|
|
|
* |
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
26 |
|
public function scheme($scheme) |
155
|
|
|
{ |
156
|
26 |
|
$this->scheme = \strtolower($scheme); |
157
|
26 |
|
$this->uri = $this->uri->withScheme($this->scheme); |
158
|
26 |
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Set the request host. |
163
|
|
|
* |
164
|
|
|
* @param string $host |
165
|
|
|
* |
166
|
|
|
* @return $this |
167
|
|
|
*/ |
168
|
28 |
|
public function host($host) |
169
|
|
|
{ |
170
|
28 |
|
$this->uri = $this->uri->withHost($host); |
171
|
28 |
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param string $method |
176
|
|
|
* |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
65 |
|
public function method($method) |
180
|
|
|
{ |
181
|
65 |
|
$this->method = \strtoupper($method); |
182
|
65 |
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param string $clientName |
187
|
|
|
* |
188
|
|
|
* @return $this |
189
|
|
|
*/ |
190
|
80 |
|
public function client($clientName) |
191
|
|
|
{ |
192
|
80 |
|
$this->client = $clientName; |
193
|
80 |
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return bool |
198
|
|
|
* @throws ClientException |
199
|
|
|
*/ |
200
|
1 |
|
public function isDebug() |
201
|
|
|
{ |
202
|
1 |
|
if (isset($this->options['debug'])) { |
203
|
1 |
|
return $this->options['debug'] === true; |
204
|
|
|
} |
205
|
|
|
|
206
|
1 |
|
if (isset($this->httpClient()->options['debug'])) { |
207
|
1 |
|
return $this->httpClient()->options['debug'] === true; |
208
|
|
|
} |
209
|
|
|
|
210
|
1 |
|
return false; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return Result |
215
|
|
|
* @throws ClientException |
216
|
|
|
* @throws ServerException |
217
|
|
|
*/ |
218
|
66 |
|
public function request() |
219
|
|
|
{ |
220
|
66 |
|
$this->options['headers']['User-Agent'] = UserAgent::toString(); |
221
|
|
|
|
222
|
66 |
|
$this->resolveUri(); |
223
|
|
|
|
224
|
66 |
|
$this->resolveParameters($this->credential()); |
225
|
|
|
|
226
|
50 |
|
if (isset($this->options['form_params'])) { |
227
|
28 |
|
$this->options['form_params'] = \GuzzleHttp\Psr7\parse_query( |
228
|
28 |
|
self::getPostHttpBody($this->options['form_params']) |
229
|
28 |
|
); |
230
|
28 |
|
} |
231
|
|
|
|
232
|
50 |
|
$this->mergeOptionsIntoClient(); |
233
|
|
|
|
234
|
50 |
|
$result = new Result($this->response(), $this); |
235
|
|
|
|
236
|
45 |
|
if (!$result->isSuccess()) { |
237
|
27 |
|
throw new ServerException($result); |
238
|
|
|
} |
239
|
|
|
|
240
|
18 |
|
return $result; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param array $post |
245
|
|
|
* |
246
|
|
|
* @return bool|string |
247
|
|
|
*/ |
248
|
31 |
|
public static function getPostHttpBody(array $post) |
249
|
|
|
{ |
250
|
31 |
|
$content = ''; |
251
|
31 |
|
foreach ($post as $apiKey => $apiValue) { |
252
|
31 |
|
$content .= "$apiKey=" . urlencode($apiValue) . '&'; |
253
|
31 |
|
} |
254
|
31 |
|
return substr($content, 0, -1); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @throws ClientException |
259
|
|
|
*/ |
260
|
50 |
|
private function response() |
261
|
|
|
{ |
262
|
|
|
try { |
263
|
50 |
|
return $this->guzzle->request( |
264
|
50 |
|
$this->method, |
265
|
50 |
|
(string)$this->uri, |
266
|
50 |
|
$this->options |
267
|
50 |
|
); |
268
|
5 |
|
} catch (GuzzleException $e) { |
269
|
5 |
|
throw new ClientException( |
270
|
5 |
|
$e->getMessage(), |
271
|
5 |
|
\ALIBABA_CLOUD_SERVER_UNREACHABLE, |
272
|
|
|
$e |
273
|
5 |
|
); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @return string |
279
|
|
|
*/ |
280
|
27 |
|
public function stringToBeSigned() |
281
|
|
|
{ |
282
|
27 |
|
return $this->stringToBeSigned; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|