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
|
154 |
|
public function __construct(array $options = []) |
82
|
|
|
{ |
83
|
154 |
|
$this->uri = new Uri(); |
84
|
154 |
|
$this->uri = $this->uri->withScheme($this->scheme); |
85
|
154 |
|
$this->guzzle = new Client(); |
86
|
154 |
|
$this->options['http_errors'] = false; |
87
|
154 |
|
$this->options['timeout'] = ALIBABA_CLOUD_TIMEOUT; |
88
|
154 |
|
$this->options['connect_timeout'] = ALIBABA_CLOUD_CONNECT_TIMEOUT; |
89
|
154 |
|
if ($options !== []) { |
90
|
1 |
|
$this->options($options); |
91
|
|
|
} |
92
|
154 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set the response data format. |
96
|
|
|
* |
97
|
|
|
* @param string $format |
98
|
|
|
* |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
14 |
|
public function format($format) |
102
|
|
|
{ |
103
|
14 |
|
$this->format = \strtoupper($format); |
104
|
14 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set the request body. |
109
|
|
|
* |
110
|
|
|
* @param string $content |
111
|
|
|
* |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
5 |
|
public function body($content) |
115
|
|
|
{ |
116
|
5 |
|
$this->options['body'] = $content; |
117
|
5 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Set the json as body. |
122
|
|
|
* |
123
|
|
|
* @param array|object $content |
124
|
|
|
* |
125
|
|
|
* @return $this |
126
|
|
|
*/ |
127
|
2 |
|
public function jsonBody($content) |
128
|
|
|
{ |
129
|
2 |
|
if (\is_array($content) || \is_object($content)) { |
130
|
2 |
|
$content = \json_encode($content); |
131
|
|
|
} |
132
|
2 |
|
return $this->body($content); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set the request scheme. |
137
|
|
|
* |
138
|
|
|
* @param string $scheme |
139
|
|
|
* |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
25 |
|
public function scheme($scheme) |
143
|
|
|
{ |
144
|
25 |
|
$this->scheme = \strtolower($scheme); |
145
|
25 |
|
$this->uri = $this->uri->withScheme($this->scheme); |
146
|
25 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Set the request host. |
151
|
|
|
* |
152
|
|
|
* @param string $host |
153
|
|
|
* |
154
|
|
|
* @return $this |
155
|
|
|
*/ |
156
|
29 |
|
public function host($host) |
157
|
|
|
{ |
158
|
29 |
|
$this->uri = $this->uri->withHost($host); |
159
|
29 |
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param string $method |
164
|
|
|
* |
165
|
|
|
* @return $this |
166
|
|
|
*/ |
167
|
65 |
|
public function method($method) |
168
|
|
|
{ |
169
|
65 |
|
$this->method = \strtoupper($method); |
170
|
65 |
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param string $clientName |
175
|
|
|
* |
176
|
|
|
* @return $this |
177
|
|
|
*/ |
178
|
80 |
|
public function client($clientName) |
179
|
|
|
{ |
180
|
80 |
|
$this->client = $clientName; |
181
|
80 |
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return bool |
186
|
|
|
* @throws ClientException |
187
|
|
|
*/ |
188
|
1 |
|
public function isDebug() |
189
|
|
|
{ |
190
|
1 |
|
if (isset($this->options['debug'])) { |
191
|
1 |
|
return $this->options['debug'] === true; |
192
|
|
|
} |
193
|
|
|
|
194
|
1 |
|
if (isset($this->httpClient()->options['debug'])) { |
195
|
1 |
|
return $this->httpClient()->options['debug'] === true; |
196
|
|
|
} |
197
|
|
|
|
198
|
1 |
|
return false; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return Result |
203
|
|
|
* @throws ClientException |
204
|
|
|
* @throws ServerException |
205
|
|
|
*/ |
206
|
66 |
|
public function request() |
207
|
|
|
{ |
208
|
66 |
|
$this->options['headers']['User-Agent'] = $this->userAgentString(); |
209
|
|
|
|
210
|
66 |
|
$this->resolveUri(); |
211
|
|
|
|
212
|
64 |
|
$this->resolveParameters($this->credential()); |
213
|
|
|
|
214
|
49 |
|
if (isset($this->options['form_params'])) { |
215
|
25 |
|
$this->options['form_params'] = \GuzzleHttp\Psr7\parse_query( |
216
|
25 |
|
self::getPostHttpBody($this->options['form_params']) |
217
|
|
|
); |
218
|
|
|
} |
219
|
|
|
|
220
|
49 |
|
$this->mergeOptionsIntoClient(); |
221
|
|
|
|
222
|
49 |
|
$result = new Result($this->response(), $this); |
223
|
|
|
|
224
|
41 |
|
if (!$result->isSuccess()) { |
225
|
40 |
|
throw new ServerException($result); |
226
|
|
|
} |
227
|
|
|
|
228
|
1 |
|
return $result; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param array $post |
233
|
|
|
* |
234
|
|
|
* @return bool|string |
235
|
|
|
*/ |
236
|
28 |
|
public static function getPostHttpBody(array $post) |
237
|
|
|
{ |
238
|
28 |
|
$content = ''; |
239
|
28 |
|
foreach ($post as $apiKey => $apiValue) { |
240
|
28 |
|
$content .= "$apiKey=" . urlencode($apiValue) . '&'; |
241
|
|
|
} |
242
|
28 |
|
return substr($content, 0, -1); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @throws ClientException |
247
|
|
|
*/ |
248
|
49 |
|
private function response() |
249
|
|
|
{ |
250
|
|
|
try { |
251
|
49 |
|
return $this->guzzle->request( |
252
|
49 |
|
$this->method, |
253
|
49 |
|
(string)$this->uri, |
254
|
49 |
|
$this->options |
255
|
|
|
); |
256
|
8 |
|
} catch (GuzzleException $e) { |
257
|
8 |
|
throw new ClientException( |
258
|
8 |
|
$e->getMessage(), |
259
|
8 |
|
\ALIBABA_CLOUD_SERVER_UNREACHABLE, |
260
|
8 |
|
$e |
261
|
|
|
); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @return string |
267
|
|
|
*/ |
268
|
40 |
|
public function stringToBeSigned() |
269
|
|
|
{ |
270
|
40 |
|
return $this->stringToBeSigned; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|