@@ -23,13 +23,15 @@ def __init__(
23
23
home_dir = None ,
24
24
scheme = "http" ,
25
25
rs_host = None ,
26
- rsf_host = None ):
26
+ rsf_host = None ,
27
+ api_host = None ):
27
28
"""初始化Zone类"""
28
29
self .up_host = up_host
29
30
self .up_host_backup = up_host_backup
30
31
self .io_host = io_host
31
32
self .rs_host = rs_host
32
33
self .rsf_host = rsf_host
34
+ self .api_host = api_host
33
35
self .home_dir = home_dir
34
36
self .host_cache = host_cache
35
37
self .scheme = scheme
@@ -91,6 +93,20 @@ def get_rsf_host(self, ak, bucket, home_dir=None):
91
93
rsf_hosts = bucket_hosts ['rsfHosts' ]
92
94
return rsf_hosts [0 ]
93
95
96
+ def get_api_host (self , ak , bucket , home_dir = None ):
97
+ from .config import get_default , is_customized_default
98
+ if self .api_host :
99
+ return self .api_host
100
+ if is_customized_default ('default_api_host' ):
101
+ return get_default ('default_api_host' )
102
+ if home_dir is None :
103
+ home_dir = os .getcwd ()
104
+ bucket_hosts = self .get_bucket_hosts (ak , bucket , home_dir )
105
+ if 'apiHosts' not in bucket_hosts :
106
+ bucket_hosts = self .get_bucket_hosts (ak , bucket , home_dir , force = True )
107
+ api_hosts = bucket_hosts ['apiHosts' ]
108
+ return api_hosts [0 ]
109
+
94
110
def get_up_host (self , ak , bucket , home_dir ):
95
111
bucket_hosts = self .get_bucket_hosts (ak , bucket , home_dir )
96
112
if 'upHosts' not in bucket_hosts :
@@ -123,39 +139,38 @@ def get_bucket_hosts(self, ak, bucket, home_dir, force=False):
123
139
if not force and len (bucket_hosts ) > 0 :
124
140
return bucket_hosts
125
141
126
- hosts = {}
127
- hosts .update ({self .scheme : {}})
128
-
129
- hosts [self .scheme ].update ({'up' : []})
130
- hosts [self .scheme ].update ({'io' : []})
142
+ hosts = compat .json .loads (self .bucket_hosts (ak , bucket )).get ('hosts' , [])
131
143
132
- if self . up_host is not None :
133
- hosts [ self . scheme ][ 'up' ]. append ( self . scheme + "://" + self . up_host )
144
+ if type ( hosts ) is not list or len ( hosts ) == 0 :
145
+ raise KeyError ( "Please check your BUCKET_NAME! Server hosts not correct! The hosts is %s" % hosts )
134
146
135
- if self .up_host_backup is not None :
136
- hosts [self .scheme ]['up' ].append (
137
- self .scheme + "://" + self .up_host_backup )
147
+ region = hosts [0 ]
138
148
139
- if self . io_host is not None :
140
- hosts [ self . scheme ][ 'io' ]. append ( self . scheme + "://" + self . io_host )
149
+ default_ttl = 24 * 3600 # 1 day
150
+ region [ 'ttl' ] = region . get ( 'ttl' , default_ttl )
141
151
142
- if len (hosts [self .scheme ]) == 0 or self .io_host is None :
143
- hosts = compat .json .loads (self .bucket_hosts (ak , bucket ))
144
- else :
145
- # 1 year
146
- hosts ['ttl' ] = int (time .time ()) + 31536000
147
- try :
148
- scheme_hosts = hosts [self .scheme ]
149
- except KeyError :
150
- raise KeyError (
151
- "Please check your BUCKET_NAME! The UpHosts is %s" %
152
- hosts )
153
152
bucket_hosts = {
154
- 'upHosts' : scheme_hosts ['up' ],
155
- 'ioHosts' : scheme_hosts ['io' ],
156
- 'rsHosts' : scheme_hosts ['rs' ],
157
- 'rsfHosts' : scheme_hosts ['rsf' ],
158
- 'deadline' : int (time .time ()) + hosts ['ttl' ]
153
+ 'upHosts' : [
154
+ '{0}://{1}' .format (self .scheme , domain )
155
+ for domain in region .get ('up' , {}).get ('domains' , [])
156
+ ],
157
+ 'ioHosts' : [
158
+ '{0}://{1}' .format (self .scheme , domain )
159
+ for domain in region .get ('io' , {}).get ('domains' , [])
160
+ ],
161
+ 'rsHosts' : [
162
+ '{0}://{1}' .format (self .scheme , domain )
163
+ for domain in region .get ('rs' , {}).get ('domains' , [])
164
+ ],
165
+ 'rsfHosts' : [
166
+ '{0}://{1}' .format (self .scheme , domain )
167
+ for domain in region .get ('rsf' , {}).get ('domains' , [])
168
+ ],
169
+ 'apiHosts' : [
170
+ '{0}://{1}' .format (self .scheme , domain )
171
+ for domain in region .get ('api' , {}).get ('domains' , [])
172
+ ],
173
+ 'deadline' : int (time .time ()) + region ['ttl' ]
159
174
}
160
175
home_dir = ""
161
176
self .set_bucket_hosts_to_cache (key , bucket_hosts , home_dir )
@@ -166,9 +181,6 @@ def get_bucket_hosts_to_cache(self, key, home_dir):
166
181
if len (self .host_cache ) == 0 :
167
182
self .host_cache_from_file (home_dir )
168
183
169
- if self .host_cache == {}:
170
- return ret
171
-
172
184
if key not in self .host_cache :
173
185
return ret
174
186
@@ -211,7 +223,7 @@ def bucket_hosts(self, ak, bucket):
211
223
uc_host = UC_HOST
212
224
if is_customized_default ('default_uc_host' ):
213
225
uc_host = get_default ('default_uc_host' )
214
- url = "{0}/v1 /query?ak={1}&bucket={2}" .format (uc_host , ak , bucket )
226
+ url = "{0}/v4 /query?ak={1}&bucket={2}" .format (uc_host , ak , bucket )
215
227
ret = requests .get (url )
216
228
data = compat .json .dumps (ret .json (), separators = (',' , ':' ))
217
229
return data
0 commit comments