mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2026-09-02 23:20:42 +00:00
Completely refactored unit tests, removed unused code
This commit is contained in:
@@ -22,19 +22,25 @@ class Session:
|
||||
|
||||
def save(self):
|
||||
"""Saves the current session object as session_user_id.session"""
|
||||
with open('{}.session'.format(self.session_user_id), 'wb') as file:
|
||||
pickle.dump(self, file)
|
||||
if self.session_user_id:
|
||||
with open('{}.session'.format(self.session_user_id), 'wb') as file:
|
||||
pickle.dump(self, file)
|
||||
|
||||
@staticmethod
|
||||
def try_load_or_create_new(session_user_id):
|
||||
"""Loads a saved session_user_id session, or creates a new one if none existed before"""
|
||||
filepath = '{}.session'.format(session_user_id)
|
||||
"""Loads a saved session_user_id session, or creates a new one if none existed before.
|
||||
If the given session_user_id is None, we assume that it is for testing purposes"""
|
||||
if session_user_id is None:
|
||||
return Session(None)
|
||||
|
||||
if file_exists(filepath):
|
||||
with open(filepath, 'rb') as file:
|
||||
return pickle.load(file)
|
||||
else:
|
||||
return Session(session_user_id)
|
||||
filepath = '{}.session'.format(session_user_id)
|
||||
|
||||
if file_exists(filepath):
|
||||
with open(filepath, 'rb') as file:
|
||||
return pickle.load(file)
|
||||
else:
|
||||
return Session(session_user_id)
|
||||
|
||||
def get_new_msg_id(self):
|
||||
"""Generates a new message ID based on the current time (in ms) since epoch"""
|
||||
|
||||
@@ -45,30 +45,34 @@ class TelegramClient:
|
||||
"""Connects to the Telegram servers, executing authentication if required.
|
||||
Note that authenticating to the Telegram servers is not the same as authenticating
|
||||
the app, which requires to send a code first."""
|
||||
try:
|
||||
if not self.session.auth_key or reconnect:
|
||||
self.session.auth_key, self.session.time_offset = \
|
||||
network.authenticator.do_authentication(self.transport)
|
||||
|
||||
if not self.session.auth_key or reconnect:
|
||||
self.session.auth_key, self.session.time_offset = network.authenticator.do_authentication(self.transport)
|
||||
self.session.save()
|
||||
self.session.save()
|
||||
|
||||
self.sender = MtProtoSender(self.transport, self.session)
|
||||
self.sender.add_update_handler(self.on_update)
|
||||
self.sender = MtProtoSender(self.transport, self.session)
|
||||
self.sender.add_update_handler(self.on_update)
|
||||
|
||||
# Always init connection by using the latest layer, not only when not reconnecting (as in original TLSharp's)
|
||||
# Otherwise, the server thinks that were using the oldest layer!
|
||||
# (Note that this is mainly untested, but it seems like it since some errors point in that direction)
|
||||
request = InvokeWithLayerRequest(layer=self.layer,
|
||||
query=InitConnectionRequest(api_id=self.api_id,
|
||||
device_model=platform.node(),
|
||||
system_version=platform.system(),
|
||||
app_version='0.2',
|
||||
lang_code='en',
|
||||
query=GetConfigRequest()))
|
||||
# Now it's time to send an InitConnectionRequest
|
||||
# This must always be invoked with the layer we'll be using
|
||||
request = InvokeWithLayerRequest(layer=self.layer,
|
||||
query=InitConnectionRequest(api_id=self.api_id,
|
||||
device_model=platform.node(),
|
||||
system_version=platform.system(),
|
||||
app_version='0.2',
|
||||
lang_code='en',
|
||||
query=GetConfigRequest()))
|
||||
|
||||
self.sender.send(request)
|
||||
self.sender.receive(request)
|
||||
self.sender.send(request)
|
||||
self.sender.receive(request)
|
||||
|
||||
self.dc_options = request.result.dc_options
|
||||
return True
|
||||
self.dc_options = request.result.dc_options
|
||||
return True
|
||||
except RPCError as error:
|
||||
print('Could not stabilise initial connection: {}'.format(error))
|
||||
return False
|
||||
|
||||
def reconnect_to_dc(self, dc_id):
|
||||
"""Reconnects to the specified DC ID. This is automatically called after an InvalidDCError is raised"""
|
||||
|
||||
Reference in New Issue
Block a user