diff --git a/telethon/client/account.py b/telethon/client/account.py index b6a2e1e7..b8d6a9b9 100644 --- a/telethon/client/account.py +++ b/telethon/client/account.py @@ -54,7 +54,7 @@ class _TakeoutClient: self.__success)) if not result: raise ValueError("Failed to finish the takeout.") - self.session.takeout_id = None + self._session.takeout_id = None __enter__ = helpers._sync_enter __exit__ = helpers._sync_exit @@ -211,7 +211,7 @@ class AccountMethods(UserMethods): ) arg_specified = (arg is not None for arg in request_kwargs.values()) - if self.session.takeout_id is None or any(arg_specified): + if self._session.takeout_id is None or any(arg_specified): request = functions.account.InitTakeoutSessionRequest( **request_kwargs) else: diff --git a/telethon/client/auth.py b/telethon/client/auth.py index 1ebd9901..d143dd0e 100644 --- a/telethon/client/auth.py +++ b/telethon/client/auth.py @@ -330,7 +330,7 @@ class AuthMethods(MessageParseMethods, UserMethods): elif bot_token: result = await self(functions.auth.ImportBotAuthorizationRequest( flags=0, bot_auth_token=bot_token, - api_id=self.api_id, api_hash=self.api_hash + api_id=self._api_id, api_hash=self._api_hash )) else: raise ValueError('You must provide a code, password or bot token') @@ -465,7 +465,7 @@ class AuthMethods(MessageParseMethods, UserMethods): if not phone_hash: try: result = await self(functions.auth.SendCodeRequest( - phone, self.api_id, self.api_hash, types.CodeSettings())) + phone, self._api_id, self._api_hash, types.CodeSettings())) except errors.AuthRestartError: return await self.send_code_request(phone, force_sms=force_sms) @@ -508,7 +508,7 @@ class AuthMethods(MessageParseMethods, UserMethods): self._state_cache.reset() await self.disconnect() - self.session.delete() + self._session.delete() return True async def edit_2fa( diff --git a/telethon/client/telegrambaseclient.py b/telethon/client/telegrambaseclient.py index 310058e3..e200a0e9 100644 --- a/telethon/client/telegrambaseclient.py +++ b/telethon/client/telegrambaseclient.py @@ -245,10 +245,10 @@ class TelegramBaseClient(abc.ABC): # them to disk, and to save additional useful information. # TODO Session should probably return all cached # info of entities, not just the input versions - self.session = session + self._session = session self._entity_cache = EntityCache() - self.api_id = int(api_id) - self.api_hash = api_hash + self._api_id = int(api_id) + self._api_hash = api_hash self._request_retries = request_retries self._connection_retries = connection_retries @@ -267,7 +267,7 @@ class TelegramBaseClient(abc.ABC): system = platform.uname() self._init_with = lambda x: functions.InvokeWithLayerRequest( LAYER, functions.InitConnectionRequest( - api_id=self.api_id, + api_id=self._api_id, device_model=device_model or system.system or 'Unknown', system_version=system_version or system.release or '1.0', app_version=app_version or self.__version__, @@ -280,7 +280,7 @@ class TelegramBaseClient(abc.ABC): ) self._sender = MTProtoSender( - self.session.auth_key, self._loop, + self._session.auth_key, self._loop, loggers=self._log, retries=self._connection_retries, delay=self._retry_delay, @@ -318,7 +318,7 @@ class TelegramBaseClient(abc.ABC): # Update state (for catching up after a disconnection) # TODO Get state from channels too self._state_cache = StateCache( - self.session.get_update_state(0), self._log) + self._session.get_update_state(0), self._log) # Some further state for subclasses self._event_builders = [] @@ -362,6 +362,18 @@ class TelegramBaseClient(abc.ABC): """ return self._loop + @property + def session(self) -> Session: + """ + The ``Session`` instance used by the client. + + Example + .. code-block:: python + + client.session.set_dc(dc_id, ip, port) + """ + return self._session + @property def disconnected(self: 'TelegramClient') -> asyncio.Future: """ @@ -405,15 +417,15 @@ class TelegramBaseClient(abc.ABC): print('Failed to connect') """ await self._sender.connect(self._connection( - self.session.server_address, - self.session.port, - self.session.dc_id, + self._session.server_address, + self._session.port, + self._session.dc_id, loop=self._loop, loggers=self._log, proxy=self._proxy )) - self.session.auth_key = self._sender.auth_key - self.session.save() + self._session.auth_key = self._sender.auth_key + self._session.save() await self._sender.send(self._init_with( functions.help.GetConfigRequest())) @@ -476,7 +488,7 @@ class TelegramBaseClient(abc.ABC): pts, date = self._state_cache[None] if pts and date: - self.session.set_update_state(0, types.updates.State( + self._session.set_update_state(0, types.updates.State( pts=pts, qts=0, date=date, @@ -484,7 +496,7 @@ class TelegramBaseClient(abc.ABC): unread_count=0 )) - self.session.close() + self._session.close() async def _disconnect(self: 'TelegramClient'): """ @@ -504,12 +516,12 @@ class TelegramBaseClient(abc.ABC): self._log[__name__].info('Reconnecting to new data center %s', new_dc) dc = await self._get_dc(new_dc) - self.session.set_dc(dc.id, dc.ip_address, dc.port) + self._session.set_dc(dc.id, dc.ip_address, dc.port) # auth_key's are associated with a server, which has now changed # so it's not valid anymore. Set to None to force recreating it. self._sender.auth_key.key = None - self.session.auth_key = None - self.session.save() + self._session.auth_key = None + self._session.save() await self._disconnect() return await self.connect() @@ -518,8 +530,8 @@ class TelegramBaseClient(abc.ABC): Callback from the sender whenever it needed to generate a new authorization key. This means we are not authorized. """ - self.session.auth_key = auth_key - self.session.save() + self._session.auth_key = auth_key + self._session.save() # endregion @@ -622,13 +634,13 @@ class TelegramBaseClient(abc.ABC): session = self._exported_sessions.get(cdn_redirect.dc_id) if not session: dc = await self._get_dc(cdn_redirect.dc_id, cdn=True) - session = self.session.clone() + session = self._session.clone() await session.set_dc(dc.id, dc.ip_address, dc.port) self._exported_sessions[cdn_redirect.dc_id] = session self._log[__name__].info('Creating new CDN client') client = TelegramBareClient( - session, self.api_id, self.api_hash, + session, self._api_id, self._api_hash, proxy=self._sender.connection.conn.proxy, timeout=self._sender.connection.get_timeout() ) diff --git a/telethon/client/updates.py b/telethon/client/updates.py index 69333a8a..bda6bac1 100644 --- a/telethon/client/updates.py +++ b/telethon/client/updates.py @@ -225,7 +225,7 @@ class UpdateMethods(UserMethods): if not pts: return - self.session.catching_up = True + self._session.catching_up = True try: while True: d = await self(functions.updates.GetDifferenceRequest( @@ -275,7 +275,7 @@ class UpdateMethods(UserMethods): finally: # TODO Save new pts to session self._state_cache._pts_date = (pts, date) - self.session.catching_up = False + self._session.catching_up = False # endregion @@ -285,7 +285,7 @@ class UpdateMethods(UserMethods): # the order that the updates arrive in to update the pts and date to # be always-increasing. There is also no need to make this async. def _handle_update(self: 'TelegramClient', update): - self.session.process_entities(update) + self._session.process_entities(update) self._entity_cache.add(update) if isinstance(update, (types.Updates, types.UpdatesCombined)): @@ -347,7 +347,7 @@ class UpdateMethods(UserMethods): # inserted because this is a rather expensive operation # (default's sqlite3 takes ~0.1s to commit changes). Do # it every minute instead. No-op if there's nothing new. - self.session.save() + self._session.save() # We need to send some content-related request at least hourly # for Telegram to keep delivering updates, otherwise they will diff --git a/telethon/client/users.py b/telethon/client/users.py index e5c31b11..53a467b6 100644 --- a/telethon/client/users.py +++ b/telethon/client/users.py @@ -52,7 +52,7 @@ class UserMethods(TelegramBaseClient): exceptions.append(e) results.append(None) continue - self.session.process_entities(result) + self._session.process_entities(result) self._entity_cache.add(result) exceptions.append(None) results.append(result) @@ -63,7 +63,7 @@ class UserMethods(TelegramBaseClient): return results else: result = await future - self.session.process_entities(result) + self._session.process_entities(result) self._entity_cache.add(result) return result except (errors.ServerError, errors.RpcCallFailError, @@ -377,7 +377,7 @@ class UserMethods(TelegramBaseClient): # No InputPeer, cached peer, or known string. Fetch from disk cache try: - return self.session.get_input_entity(peer) + return self._session.get_input_entity(peer) except ValueError: pass @@ -513,7 +513,7 @@ class UserMethods(TelegramBaseClient): try: # Nobody with this username, maybe it's an exact name/title return await self.get_entity( - self.session.get_input_entity(string)) + self._session.get_input_entity(string)) except ValueError: pass