mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-01-25 00:34:19 +03:00
Make DC options static
Some datacenters don't allow calling GetConfigRequest, this way it can both be reused and such calls omitted.
This commit is contained in:
parent
59a4315c28
commit
2689b0750d
|
@ -53,6 +53,9 @@ class TelegramBareClient:
|
||||||
# Current TelegramClient version
|
# Current TelegramClient version
|
||||||
__version__ = '0.13.3'
|
__version__ = '0.13.3'
|
||||||
|
|
||||||
|
# TODO Make this thread-safe, all connections share the same DC
|
||||||
|
_dc_options = None
|
||||||
|
|
||||||
# region Initialization
|
# region Initialization
|
||||||
|
|
||||||
def __init__(self, session, api_id, api_hash,
|
def __init__(self, session, api_id, api_hash,
|
||||||
|
@ -85,7 +88,6 @@ class TelegramBareClient:
|
||||||
self.updates = UpdateState(process_updates)
|
self.updates = UpdateState(process_updates)
|
||||||
|
|
||||||
# These will be set later
|
# These will be set later
|
||||||
self.dc_options = None
|
|
||||||
self._sender = None
|
self._sender = None
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
@ -147,7 +149,7 @@ class TelegramBareClient:
|
||||||
elif initial_query:
|
elif initial_query:
|
||||||
return self._init_connection(initial_query)
|
return self._init_connection(initial_query)
|
||||||
else:
|
else:
|
||||||
self.dc_options = \
|
TelegramBareClient._dc_options = \
|
||||||
self._init_connection(GetConfigRequest()).dc_options
|
self._init_connection(GetConfigRequest()).dc_options
|
||||||
else:
|
else:
|
||||||
# TODO Avoid duplicated code
|
# TODO Avoid duplicated code
|
||||||
|
@ -157,8 +159,9 @@ class TelegramBareClient:
|
||||||
))
|
))
|
||||||
elif initial_query:
|
elif initial_query:
|
||||||
return self(initial_query)
|
return self(initial_query)
|
||||||
if not self.dc_options:
|
if TelegramBareClient._dc_options is None:
|
||||||
self.dc_options = self(GetConfigRequest()).dc_options
|
TelegramBareClient._dc_options = \
|
||||||
|
self(GetConfigRequest()).dc_options
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -221,7 +224,7 @@ class TelegramBareClient:
|
||||||
|
|
||||||
def _get_dc(self, dc_id, ipv6=False, cdn=False):
|
def _get_dc(self, dc_id, ipv6=False, cdn=False):
|
||||||
"""Gets the Data Center (DC) associated to 'dc_id'"""
|
"""Gets the Data Center (DC) associated to 'dc_id'"""
|
||||||
if not self.dc_options:
|
if TelegramBareClient._dc_options is None:
|
||||||
raise ConnectionError(
|
raise ConnectionError(
|
||||||
'Cannot determine the required data center IP address. '
|
'Cannot determine the required data center IP address. '
|
||||||
'Stabilise a successful initial connection first.')
|
'Stabilise a successful initial connection first.')
|
||||||
|
@ -233,15 +236,15 @@ class TelegramBareClient:
|
||||||
rsa.add_key(pk.public_key)
|
rsa.add_key(pk.public_key)
|
||||||
|
|
||||||
return next(
|
return next(
|
||||||
dc for dc in self.dc_options if dc.id == dc_id and
|
dc for dc in TelegramBareClient._dc_options if dc.id == dc_id
|
||||||
bool(dc.ipv6) == ipv6 and bool(dc.cdn) == cdn
|
and bool(dc.ipv6) == ipv6 and bool(dc.cdn) == cdn
|
||||||
)
|
)
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
if not cdn:
|
if not cdn:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
# New configuration, perhaps a new CDN was added?
|
# New configuration, perhaps a new CDN was added?
|
||||||
self.dc_options = self(GetConfigRequest()).dc_options
|
TelegramBareClient._dc_options = self(GetConfigRequest()).dc_options
|
||||||
return self._get_dc(dc_id, ipv6=ipv6, cdn=cdn)
|
return self._get_dc(dc_id, ipv6=ipv6, cdn=cdn)
|
||||||
|
|
||||||
def _get_exported_client(self, dc_id,
|
def _get_exported_client(self, dc_id,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user