Blame view

vendor/alibabacloud/client/src/Regions/LocationService.php 3.86 KB
f0d6cde5   xu   vendor
1
2
3
4
<?php

namespace AlibabaCloud\Client\Regions;

0c34aba8   xu   更新阿里云短信接口
5
6
7
use Exception;
use RuntimeException;
use AlibabaCloud\Client\SDK;
f0d6cde5   xu   vendor
8
use AlibabaCloud\Client\Config\Config;
0c34aba8   xu   更新阿里云短信接口
9
use AlibabaCloud\Client\Request\Request;
f0d6cde5   xu   vendor
10
use AlibabaCloud\Client\Filter\ApiFilter;
f0d6cde5   xu   vendor
11
use AlibabaCloud\Client\Filter\HttpFilter;
0c34aba8   xu   更新阿里云短信接口
12
13
14
use AlibabaCloud\Client\Filter\ClientFilter;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
f0d6cde5   xu   vendor
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

/**
 * 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;
    }

    /**
f0d6cde5   xu   vendor
49
50
51
     * @param Request $request
     * @param string  $domain
     *
0c34aba8   xu   更新阿里云短信接口
52
     * @return string
f0d6cde5   xu   vendor
53
54
     * @throws ClientException
     * @throws ServerException
0c34aba8   xu   更新阿里云短信接口
55
56
     * @deprecated
     * @codeCoverageIgnore
f0d6cde5   xu   vendor
57
58
59
60
61
62
63
     */
    public static function findProductDomain(Request $request, $domain = 'location.aliyuncs.com')
    {
        return self::resolveHost($request, $domain);
    }

    /**
0c34aba8   xu   更新阿里云短信接口
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
     * @param $regionId
     * @param $product
     * @param $domain
     *
     * @throws ClientException
     * @deprecated
     * @codeCoverageIgnore
     */
    public static function addEndPoint($regionId, $product, $domain)
    {
        self::addHost($product, $domain, $regionId);
    }


    /**
f0d6cde5   xu   vendor
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
     * @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'];
    }

    /**
f0d6cde5   xu   vendor
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
     * @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;
    }

    /**
f0d6cde5   xu   vendor
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
     * 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'])
            );
        }
    }
}