From c84e54b647923fca3a158fe0e93e0d159682c289 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sun, 17 Sep 2017 16:06:43 +0200 Subject: [PATCH] Get rid of the initial_query= parameter on .connect() --- telethon/crypto/cdn_decrypter.py | 14 ++++++++----- telethon/telegram_bare_client.py | 35 +++++++++++--------------------- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/telethon/crypto/cdn_decrypter.py b/telethon/crypto/cdn_decrypter.py index 01a35916..000cfc30 100644 --- a/telethon/crypto/cdn_decrypter.py +++ b/telethon/crypto/cdn_decrypter.py @@ -48,11 +48,10 @@ class CdnDecrypter: # # We assume that cdn_redirect.cdn_file_hashes are ordered by offset, # and that there will be enough of these to retrieve the whole file. - cdn_file = cdn_client.connect(initial_query=GetCdnFileRequest( - file_token=cdn_redirect.file_token, - offset=cdn_redirect.cdn_file_hashes[0].offset, - limit=cdn_redirect.cdn_file_hashes[0].limit - )) + # + # This relies on the fact that TelegramBareClient._dc_options is + # static and it won't be called from this DC (it would fail). + cdn_client.connect() # CDN client is ready, create the resulting CdnDecrypter decrypter = CdnDecrypter( @@ -60,6 +59,11 @@ class CdnDecrypter: cdn_aes, cdn_redirect.cdn_file_hashes ) + cdn_file = client(GetCdnFileRequest( + file_token=cdn_redirect.file_token, + offset=cdn_redirect.cdn_file_hashes[0].offset, + limit=cdn_redirect.cdn_file_hashes[0].limit + )) if isinstance(cdn_file, CdnFileReuploadNeeded): # We need to use the original client here client(ReuploadCdnFileRequest( diff --git a/telethon/telegram_bare_client.py b/telethon/telegram_bare_client.py index de9aaf4a..5f0ba1a0 100644 --- a/telethon/telegram_bare_client.py +++ b/telethon/telegram_bare_client.py @@ -94,7 +94,7 @@ class TelegramBareClient: # region Connecting - def connect(self, exported_auth=None, initial_query=None): + def connect(self, exported_auth=None): """Connects to the Telegram servers, executing authentication if required. Note that authenticating to the Telegram servers is not the same as authenticating the desired user itself, which @@ -102,20 +102,13 @@ class TelegramBareClient: If 'exported_auth' is not None, it will be used instead to determine the authorization key for the current session. - - If 'initial_query' is not None, it will override the default - 'GetConfigRequest()', and its result will be returned ONLY - if the client wasn't connected already. """ if self._sender and self._sender.is_connected(): # Try sending a ping to make sure we're connected already # TODO Maybe there's a better way to check this try: - if initial_query is None: - self(PingRequest(utils.generate_random_long())) - return True - else: - return self(initial_query) + self(PingRequest(utils.generate_random_long())) + return True except: # If ping failed, ensure we're disconnected first self.disconnect() @@ -146,22 +139,18 @@ class TelegramBareClient: self._init_connection(ImportAuthorizationRequest( exported_auth.id, exported_auth.bytes )) - elif initial_query: - return self._init_connection(initial_query) else: TelegramBareClient._dc_options = \ self._init_connection(GetConfigRequest()).dc_options - else: - # TODO Avoid duplicated code - if exported_auth is not None: - self(ImportAuthorizationRequest( - exported_auth.id, exported_auth.bytes - )) - elif initial_query: - return self(initial_query) - if TelegramBareClient._dc_options is None: - TelegramBareClient._dc_options = \ - self(GetConfigRequest()).dc_options + + elif exported_auth is not None: + self(ImportAuthorizationRequest( + exported_auth.id, exported_auth.bytes + )) + + if TelegramBareClient._dc_options is None: + TelegramBareClient._dc_options = \ + self(GetConfigRequest()).dc_options return True