1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlibabaCloud\Client\Request\Traits; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Uri; |
6
|
|
|
use AlibabaCloud\Client\SDK; |
7
|
|
|
use AlibabaCloud\Client\AlibabaCloud; |
8
|
|
|
use AlibabaCloud\Client\Request\Request; |
9
|
|
|
use AlibabaCloud\Client\Filter\ApiFilter; |
10
|
|
|
use AlibabaCloud\Client\Regions\LocationService; |
11
|
|
|
use AlibabaCloud\Client\Exception\ClientException; |
12
|
|
|
use AlibabaCloud\Client\Exception\ServerException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Trait AcsTrait |
16
|
|
|
* |
17
|
|
|
* @package AlibabaCloud\Client\Request\Traits |
18
|
|
|
* @property Uri $uri |
19
|
|
|
* @mixin Request |
20
|
|
|
*/ |
21
|
|
|
trait AcsTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
public $version; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
public $product; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
public $action; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
public $serviceCode = ''; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
public $endpointType = 'openAPI'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string|null |
50
|
|
|
*/ |
51
|
|
|
public $network = 'public'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var array|null |
55
|
|
|
*/ |
56
|
|
|
public $endpointMap; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string|null |
60
|
|
|
*/ |
61
|
|
|
public $endpointRegional; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
public $endpointSuffix = ''; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $action |
70
|
|
|
* |
71
|
|
|
* @return $this |
72
|
|
|
* @throws ClientException |
73
|
|
|
*/ |
74
|
70 |
|
public function action($action) |
75
|
|
|
{ |
76
|
70 |
|
$this->action = ApiFilter::action($action); |
77
|
|
|
|
78
|
68 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @codeCoverageIgnore |
83
|
|
|
* |
84
|
|
|
* @param string $endpointSuffix |
85
|
|
|
* |
86
|
|
|
* @return AcsTrait |
87
|
|
|
* @throws ClientException |
88
|
|
|
*/ |
89
|
|
|
public function endpointSuffix($endpointSuffix) |
90
|
|
|
{ |
91
|
|
|
$this->endpointSuffix = ApiFilter::endpointSuffix($endpointSuffix); |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $network |
98
|
|
|
*/ |
99
|
|
|
public function network($network) |
100
|
|
|
{ |
101
|
|
|
$this->network = ApiFilter::network($network); |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $version |
108
|
|
|
* |
109
|
|
|
* @return $this |
110
|
|
|
* @throws ClientException |
111
|
|
|
*/ |
112
|
74 |
|
public function version($version) |
113
|
|
|
{ |
114
|
74 |
|
$this->version = ApiFilter::version($version); |
115
|
|
|
|
116
|
72 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $product |
121
|
|
|
* |
122
|
|
|
* @return $this |
123
|
|
|
* @throws ClientException |
124
|
|
|
*/ |
125
|
79 |
|
public function product($product) |
126
|
|
|
{ |
127
|
79 |
|
$this->product = ApiFilter::product($product); |
128
|
|
|
|
129
|
77 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $endpointType |
134
|
|
|
* |
135
|
|
|
* @return $this |
136
|
|
|
* @throws ClientException |
137
|
|
|
*/ |
138
|
9 |
|
public function endpointType($endpointType) |
139
|
|
|
{ |
140
|
9 |
|
$this->endpointType = ApiFilter::endpointType($endpointType); |
141
|
|
|
|
142
|
7 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $serviceCode |
147
|
|
|
* |
148
|
|
|
* @return $this |
149
|
|
|
* @throws ClientException |
150
|
|
|
*/ |
151
|
11 |
|
public function serviceCode($serviceCode) |
152
|
|
|
{ |
153
|
11 |
|
$this->serviceCode = ApiFilter::serviceCode($serviceCode); |
154
|
|
|
|
155
|
9 |
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Resolve Host. |
160
|
|
|
* |
161
|
|
|
* @throws ClientException |
162
|
|
|
* @throws ServerException |
163
|
|
|
*/ |
164
|
76 |
|
public function resolveHost() |
165
|
|
|
{ |
166
|
|
|
// Return if specified |
167
|
76 |
|
if ($this->uri->getHost() !== 'localhost') { |
168
|
25 |
|
return; |
169
|
|
|
} |
170
|
|
|
|
171
|
57 |
|
$region_id = $this->realRegionId(); |
172
|
57 |
|
$host = ''; |
173
|
|
|
|
174
|
57 |
|
$this->resolveHostWays($host, $region_id); |
175
|
|
|
|
176
|
53 |
|
if (!$host) { |
177
|
1 |
|
throw new ClientException( |
178
|
1 |
|
"No host found for {$this->product} in the {$region_id}, you can specify host by host() method. " . |
179
|
1 |
|
'Like $request->host(\'xxx.xxx.aliyuncs.com\')', |
180
|
|
|
SDK::HOST_NOT_FOUND |
181
|
1 |
|
); |
182
|
|
|
} |
183
|
|
|
|
184
|
52 |
|
$this->uri = $this->uri->withHost($host); |
185
|
52 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param string $host |
189
|
|
|
* @param string $region_id |
190
|
|
|
* |
191
|
|
|
* @throws ClientException |
192
|
|
|
* @throws ServerException |
193
|
|
|
*/ |
194
|
57 |
|
private function resolveHostWays(&$host, $region_id) |
195
|
|
|
{ |
196
|
|
|
// 1. Find host by map. |
197
|
57 |
|
if ($this->network === 'public' && isset($this->endpointMap[$region_id])) { |
198
|
1 |
|
$host = $this->endpointMap[$region_id]; |
199
|
1 |
|
} |
200
|
|
|
|
201
|
|
|
// 2. Find host by rules. |
202
|
57 |
|
if (!$host && $this->endpointRegional !== null) { |
203
|
3 |
|
$host = AlibabaCloud::resolveHostByRule($this); |
204
|
2 |
|
} |
205
|
|
|
|
206
|
|
|
// 3. Find in the local array file. |
207
|
56 |
|
if (!$host) { |
208
|
54 |
|
$host = AlibabaCloud::resolveHost($this->product, $region_id); |
209
|
51 |
|
} |
210
|
|
|
|
211
|
|
|
// 4. Find in the Location service. |
212
|
53 |
|
if (!$host && $this->serviceCode) { |
213
|
2 |
|
$host = LocationService::resolveHost($this); |
214
|
2 |
|
} |
215
|
53 |
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return string |
219
|
|
|
* @throws ClientException |
220
|
|
|
*/ |
221
|
96 |
|
public function realRegionId() |
222
|
|
|
{ |
223
|
96 |
|
if ($this->regionId !== null) { |
224
|
40 |
|
return $this->regionId; |
225
|
|
|
} |
226
|
|
|
|
227
|
64 |
|
if ($this->httpClient()->regionId !== null) { |
|
|
|
|
228
|
59 |
|
return $this->httpClient()->regionId; |
229
|
|
|
} |
230
|
|
|
|
231
|
5 |
|
if (AlibabaCloud::getDefaultRegionId() !== null) { |
232
|
4 |
|
return AlibabaCloud::getDefaultRegionId(); |
233
|
|
|
} |
234
|
|
|
|
235
|
1 |
|
throw new ClientException("Missing required 'RegionId' for Request", SDK::INVALID_REGION_ID); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|