44
55from ably .transport .defaults import Defaults
66from ably .types .authoptions import AuthOptions
7+ from ably .util .exceptions import AblyException
78
89log = logging .getLogger (__name__ )
910
@@ -37,9 +38,14 @@ def __init__(self, client_id=None, log_level=0, tls=True, rest_host=None, realti
3738
3839 super ().__init__ (** kwargs )
3940
41+ # REC1b1: endpoint is incompatible with deprecated options
4042 if endpoint is not None :
4143 if environment is not None or rest_host is not None or realtime_host is not None :
42- raise ValueError ('endpoint is incompatible with any of environment, rest_host or realtime_host' )
44+ raise AblyException (
45+ message = 'endpoint is incompatible with any of environment, rest_host or realtime_host' ,
46+ status_code = 400 ,
47+ code = 40106 ,
48+ )
4349
4450 # TODO check these defaults
4551 if fallback_retry_timeout is None :
@@ -60,26 +66,43 @@ def __init__(self, client_id=None, log_level=0, tls=True, rest_host=None, realti
6066 suspended_retry_timeout = Defaults .suspended_retry_timeout
6167
6268 if environment is not None and rest_host is not None :
63- raise ValueError ('specify rest_host or environment, not both' )
69+ raise AblyException (
70+ message = 'specify rest_host or environment, not both' ,
71+ status_code = 400 ,
72+ code = 40106 ,
73+ )
6474
6575 if environment is not None and realtime_host is not None :
66- raise ValueError ('specify realtime_host or environment, not both' )
76+ raise AblyException (
77+ message = 'specify realtime_host or environment, not both' ,
78+ status_code = 400 ,
79+ code = 40106 ,
80+ )
6781
6882 if idempotent_rest_publishing is None :
6983 from ably import api_version
7084 idempotent_rest_publishing = api_version >= '1.2'
7185
7286 if environment is not None and endpoint is None :
87+ log .warning ("environment client option is deprecated, please use endpoint instead" )
7388 endpoint = environment
7489
90+ # REC1d: restHost or realtimeHost option
91+ # REC1d1: restHost takes precedence over realtimeHost
92+ if rest_host is not None and endpoint is None :
93+ log .warning ("rest_host client option is deprecated, please use endpoint instead" )
94+ endpoint = rest_host
95+ elif realtime_host is not None and endpoint is None :
96+ # REC1d2: realtimeHost if restHost not specified
97+ log .warning ("realtime_host client option is deprecated, please use endpoint instead" )
98+ endpoint = realtime_host
99+
75100 if endpoint is None :
76101 endpoint = Defaults .endpoint
77102
78103 self .__client_id = client_id
79104 self .__log_level = log_level
80105 self .__tls = tls
81- self .__rest_host = rest_host
82- self .__realtime_host = realtime_host
83106 self .__port = port
84107 self .__tls_port = tls_port
85108 self .__use_binary_protocol = use_binary_protocol
@@ -91,6 +114,8 @@ def __init__(self, client_id=None, log_level=0, tls=True, rest_host=None, realti
91114 self .__realtime_request_timeout = realtime_request_timeout
92115 self .__http_max_retry_count = http_max_retry_count
93116 self .__http_max_retry_duration = http_max_retry_duration
117+ # Field for internal use only
118+ self .__fallback_host = None
94119 self .__fallback_hosts = fallback_hosts
95120 self .__fallback_retry_timeout = fallback_retry_timeout
96121 self .__disconnected_retry_timeout = disconnected_retry_timeout
@@ -101,13 +126,10 @@ def __init__(self, client_id=None, log_level=0, tls=True, rest_host=None, realti
101126 self .__connection_state_ttl = connection_state_ttl
102127 self .__suspended_retry_timeout = suspended_retry_timeout
103128 self .__connectivity_check_url = connectivity_check_url
104- self .__fallback_realtime_host = None
105129 self .__add_request_ids = add_request_ids
106130 self .__vcdiff_decoder = vcdiff_decoder
107131 self .__transport_params = transport_params or {}
108-
109- self .__rest_hosts = self .__get_rest_hosts ()
110- self .__realtime_hosts = self .__get_realtime_hosts ()
132+ self .__hosts = self .__get_hosts ()
111133
112134 @property
113135 def client_id (self ):
@@ -133,23 +155,6 @@ def tls(self):
133155 def tls (self , value ):
134156 self .__tls = value
135157
136- @property
137- def rest_host (self ):
138- return self .__rest_host
139-
140- @rest_host .setter
141- def rest_host (self , value ):
142- self .__rest_host = value
143-
144- # RTC1d
145- @property
146- def realtime_host (self ):
147- return self .__realtime_host
148-
149- @realtime_host .setter
150- def realtime_host (self , value ):
151- self .__realtime_host = value
152-
153158 @property
154159 def port (self ):
155160 return self .__port
@@ -276,12 +281,18 @@ def connectivity_check_url(self):
276281 return self .__connectivity_check_url
277282
278283 @property
279- def fallback_realtime_host (self ):
280- return self .__fallback_realtime_host
284+ def fallback_host (self ):
285+ """
286+ For internal use only, can be deleted in future
287+ """
288+ return self .__fallback_host
281289
282- @fallback_realtime_host .setter
283- def fallback_realtime_host (self , value ):
284- self .__fallback_realtime_host = value
290+ @fallback_host .setter
291+ def fallback_host (self , value ):
292+ """
293+ For internal use only, can be deleted in future
294+ """
295+ self .__fallback_host = value
285296
286297 @property
287298 def add_request_ids (self ):
@@ -295,29 +306,20 @@ def vcdiff_decoder(self):
295306 def transport_params (self ):
296307 return self .__transport_params
297308
298- def __get_rest_hosts (self ):
309+ def __get_hosts (self ):
299310 """
300311 Return the list of hosts as they should be tried. First comes the main
301312 host. Then the fallback hosts in random order.
302313 The returned list will have a length of up to http_max_retry_count.
303314 """
304- # Defaults
305- host = self .rest_host
306- if host is None :
307- host = Defaults .get_hostname (self .endpoint )
315+ host = Defaults .get_hostname (self .endpoint )
316+ # REC2: Determine fallback hosts
317+ fallback_hosts = self .get_fallback_hosts ()
308318
309319 http_max_retry_count = self .http_max_retry_count
310320 if http_max_retry_count is None :
311321 http_max_retry_count = Defaults .http_max_retry_count
312322
313- # Fallback hosts
314- fallback_hosts = self .fallback_hosts
315- if fallback_hosts is None :
316- if self .rest_host is not None :
317- fallback_hosts = []
318- else :
319- fallback_hosts = Defaults .get_fallback_hosts (self .endpoint )
320-
321323 # Shuffle
322324 fallback_hosts = list (fallback_hosts )
323325 random .shuffle (fallback_hosts )
@@ -328,28 +330,19 @@ def __get_rest_hosts(self):
328330 hosts = hosts [:http_max_retry_count ]
329331 return hosts
330332
331- def __get_realtime_hosts (self ):
332- if self .realtime_host is not None :
333- host = self .realtime_host
334- return [host ]
335-
336- host = Defaults .get_hostname (self .endpoint )
337- return [host ] + self .__fallback_hosts
338-
339- def get_rest_hosts (self ):
340- return self .__rest_hosts
341-
342- def get_rest_host (self ):
343- return self .__rest_hosts [0 ]
344-
345- def get_realtime_hosts (self ):
346- return self .__realtime_hosts
333+ def get_hosts (self ):
334+ return self .__hosts
347335
348- def get_realtime_host (self ):
349- return self .__realtime_hosts [0 ]
336+ def get_host (self ):
337+ return self .__hosts [0 ]
350338
351- def get_fallback_rest_hosts (self ):
352- return self .__rest_hosts [1 :]
339+ # REC2: Various client options collectively determine a set of fallback domains
340+ def get_fallback_hosts (self ):
341+ # REC2a: If the fallbackHosts client option is specified
342+ if self .__fallback_hosts is not None :
343+ # REC2a2: the set of fallback domains is given by the value of the fallbackHosts option
344+ return self .__fallback_hosts
353345
354- def get_fallback_realtime_hosts (self ):
355- return self .__realtime_hosts [1 :]
346+ # REC2c: Otherwise, the set of fallback domains is defined implicitly by the options
347+ # used to define the primary domain as specified in (REC1)
348+ return Defaults .get_fallback_hosts (self .endpoint )
0 commit comments