LocationService.php
3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
namespace AlibabaCloud\Client\Regions;
use Exception;
use RuntimeException;
use AlibabaCloud\Client\SDK;
use AlibabaCloud\Client\Config\Config;
use AlibabaCloud\Client\Request\Request;
use AlibabaCloud\Client\Filter\ApiFilter;
use AlibabaCloud\Client\Filter\HttpFilter;
use AlibabaCloud\Client\Filter\ClientFilter;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
/**
* Class LocationService
*
* @package AlibabaCloud\Client\Regions
*/
class LocationService
{
/**
* Global Region Name
*/
const GLOBAL_REGION = 'global';
/**
* @var array
*/
protected static $hosts = [];
/**
* @var Request
*/
protected $request;
/**
* LocationService constructor.
*
* @param Request $request
*/
private function __construct(Request $request)
{
$this->request = $request;
}
/**
* @param Request $request
* @param string $domain
*
* @return string
* @throws ClientException
* @throws ServerException
* @deprecated
* @codeCoverageIgnore
*/
public static function findProductDomain(Request $request, $domain = 'location.aliyuncs.com')
{
return self::resolveHost($request, $domain);
}
/**
* @param $regionId
* @param $product
* @param $domain
*
* @throws ClientException
* @deprecated
* @codeCoverageIgnore
*/
public static function addEndPoint($regionId, $product, $domain)
{
self::addHost($product, $domain, $regionId);
}
/**
* @param Request $request
* @param string $domain
*
* @return string
* @throws ClientException
* @throws ServerException
*/
public static function resolveHost(Request $request, $domain = 'location.aliyuncs.com')
{
$locationService = new static($request);
$product = $locationService->request->product;
$regionId = $locationService->request->realRegionId();
if (!isset(self::$hosts[$product][$regionId])) {
self::$hosts[$product][$regionId] = self::getResult($locationService, $domain);
}
return self::$hosts[$product][$regionId];
}
/**
* @param static $locationService
* @param string $domain
*
* @return string
* @throws ClientException
* @throws ServerException
*/
private static function getResult($locationService, $domain)
{
$locationRequest = new LocationServiceRequest($locationService->request, $domain);
$result = $locationRequest->request();
if (!isset($result['Endpoints']['Endpoint'][0]['Endpoint'])) {
throw new ClientException(
'Not found Region ID in ' . $domain,
SDK::INVALID_REGION_ID
);
}
return $result['Endpoints']['Endpoint'][0]['Endpoint'];
}
/**
* @param string $product
* @param string $host
* @param string $regionId
*
* @throws ClientException
*/
public static function addHost($product, $host, $regionId = self::GLOBAL_REGION)
{
ApiFilter::product($product);
HttpFilter::host($host);
ClientFilter::regionId($regionId);
self::$hosts[$product][$regionId] = $host;
}
/**
* Update endpoints from OSS.
*
* @codeCoverageIgnore
* @throws Exception
*/
public static function updateEndpoints()
{
$ossUrl = 'https://openapi-endpoints.oss-cn-hangzhou.aliyuncs.com/endpoints.json';
$json = \file_get_contents($ossUrl);
$list = \json_decode($json, true);
foreach ($list['endpoints'] as $endpoint) {
Config::set(
"endpoints.{$endpoint['service']}.{$endpoint['regionid']}",
\strtolower($endpoint['endpoint'])
);
}
}
}