| 1 | <?php |
||
| 2 | |||
| 3 | namespace AlibabaCloud\Client\Request\Traits; |
||
| 4 | |||
| 5 | use AlibabaCloud\Client\AlibabaCloud; |
||
| 6 | use AlibabaCloud\Client\Exception\ClientException; |
||
| 7 | use AlibabaCloud\Client\Exception\ServerException; |
||
| 8 | use AlibabaCloud\Client\Filter\ApiFilter; |
||
| 9 | use AlibabaCloud\Client\Regions\LocationService; |
||
| 10 | use AlibabaCloud\Client\Request\Request; |
||
| 11 | use AlibabaCloud\Client\SDK; |
||
| 12 | use GuzzleHttp\Psr7\Uri; |
||
| 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 | 1 | public function network($network) |
|
| 100 | { |
||
| 101 | 1 | $this->network = ApiFilter::network($network); |
|
| 102 | |||
| 103 | 1 | 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 | 57 | $host = AlibabaCloud::resolveHostByStatic($this->product, $region_id); |
|
| 197 | |||
| 198 | // 1. Find host by map. |
||
| 199 | 57 | if (!$host && $this->network === 'public' && isset($this->endpointMap[$region_id])) { |
|
| 200 | 1 | $host = $this->endpointMap[$region_id]; |
|
| 201 | 1 | } |
|
| 202 | |||
| 203 | 57 | if (!$host) { |
|
| 204 | 57 | $this->hostResolver($host, $region_id); |
|
| 205 | 53 | } |
|
| 206 | 53 | } |
|
| 207 | |||
| 208 | /** |
||
| 209 | * @codeCoverageIgnore |
||
| 210 | * |
||
| 211 | * @param string $host |
||
| 212 | * @param string $region_id |
||
| 213 | * |
||
| 214 | * @throws ClientException |
||
| 215 | * @throws ServerException |
||
| 216 | */ |
||
| 217 | private function hostResolver(&$host, $region_id) |
||
| 218 | { |
||
| 219 | // 2. Find host by rules. |
||
| 220 | if ($this->endpointRegional !== null) { |
||
| 221 | $host = AlibabaCloud::resolveHostByRule($this); |
||
| 222 | } |
||
| 223 | |||
| 224 | // 3. Find in the local array file. |
||
| 225 | if (!$host) { |
||
| 226 | $host = AlibabaCloud::resolveHost($this->product, $region_id); |
||
| 227 | } |
||
| 228 | |||
| 229 | // 4. Find in the Location service. |
||
| 230 | if (!$host && $this->serviceCode) { |
||
| 231 | $host = LocationService::resolveHost($this); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return string |
||
| 237 | * @throws ClientException |
||
| 238 | */ |
||
| 239 | public function realRegionId() |
||
| 240 | 96 | { |
|
| 241 | if ($this->regionId !== null) { |
||
| 242 | 96 | return $this->regionId; |
|
| 243 | 40 | } |
|
| 244 | |||
| 245 | if ($this->httpClient()->regionId !== null) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 246 | 64 | return $this->httpClient()->regionId; |
|
| 247 | 59 | } |
|
| 248 | |||
| 249 | if (AlibabaCloud::getDefaultRegionId() !== null) { |
||
| 250 | 5 | return AlibabaCloud::getDefaultRegionId(); |
|
| 251 | 4 | } |
|
| 252 | |||
| 253 | if ($this->product && AlibabaCloud::isGlobalProduct($this->product)) { |
||
| 254 | 1 | return 'global'; |
|
| 255 | } |
||
| 256 | |||
| 257 | throw new ClientException("Missing required 'RegionId' for Request", SDK::INVALID_REGION_ID); |
||
| 258 | } |
||
| 259 | } |
||
| 260 |