Move connection parameters to the session

This commit is contained in:
Lonami Exo 2017-06-10 13:15:04 +02:00
parent 1119a2737a
commit a73be04da7
3 changed files with 36 additions and 48 deletions

View File

@ -70,8 +70,7 @@ class TelegramBareClient:
# region Connecting
def connect(self, device_model, system_version, app_version, lang_code,
exported_auth=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
@ -103,10 +102,10 @@ class TelegramBareClient:
request = InitConnectionRequest(
api_id=self.api_id,
device_model=device_model,
system_version=system_version,
app_version=app_version,
lang_code=lang_code,
device_model=self.session.device_model,
system_version=self.session.system_version,
app_version=self.session.app_version,
lang_code=self.session.lang_code,
query=query)
result = self.invoke(
@ -136,8 +135,7 @@ class TelegramBareClient:
self.sender.disconnect()
self.sender = None
def reconnect(self, device_model, system_version, app_version, lang_code,
new_dc=None):
def reconnect(self, new_dc=None):
"""Disconnects and connects again (effectively reconnecting).
If 'new_dc' is not None, the current authorization key is
@ -152,7 +150,7 @@ class TelegramBareClient:
self.session.port = dc.port
self.session.save()
self.connect(device_model, system_version, app_version, lang_code)
self.connect()
# endregion
@ -194,10 +192,7 @@ class TelegramBareClient:
except ConnectionResetError:
self._logger.info('Server disconnected us. Reconnecting and '
'resending request...')
# TODO Don't actually use these values
import platform
self.reconnect(platform.node(), platform.system(), self.__version__, 'en')
self.reconnect()
return self.invoke(request, timeout=timeout)
except FloodWaitError:

View File

@ -27,8 +27,7 @@ from .tl.functions.auth import (CheckPasswordRequest, LogOutRequest,
SignUpRequest, ImportBotAuthorizationRequest)
# Required to work with different data centers
from .tl.functions.auth import (ExportAuthorizationRequest,
ImportAuthorizationRequest)
from .tl.functions.auth import ExportAuthorizationRequest
# Easier access to common methods
from .tl.functions.messages import (
@ -103,14 +102,17 @@ class TelegramClient(TelegramBareClient):
self._updates_thread_receiving = Event()
# Used on connection - the user may modify these and reconnect
self.device_model = \
device_model if device_model else platform.node()
if device_model:
self.session.device_model = device_model
self.system_version = \
system_version if system_version else platform.system()
if system_version:
self.session.system_version = system_version
self.app_version = app_version if app_version else self.__version__
self.lang_code = lang_code if lang_code else 'en'
self.session.app_version = \
app_version if app_version else self.__version__
if lang_code:
self.session.lang_code = lang_code
# Cache "exported" senders 'dc_id: MtProtoSender' and
# their corresponding sessions not to recreate them all
@ -131,12 +133,7 @@ class TelegramClient(TelegramBareClient):
*args will be ignored.
"""
return super(TelegramClient, self).connect(
device_model=self.device_model,
system_version=self.system_version,
app_version=self.app_version,
lang_code=self.lang_code
)
return super(TelegramClient, self).connect()
def disconnect(self):
"""Disconnects from the Telegram server
@ -150,7 +147,7 @@ class TelegramClient(TelegramBareClient):
self._cached_clients.clear()
def reconnect(self, new_dc=None, *args):
def reconnect(self, new_dc=None):
"""Disconnects and connects again (effectively reconnecting).
If 'new_dc' is not None, the current authorization key is
@ -158,13 +155,7 @@ class TelegramClient(TelegramBareClient):
*args will be ignored.
"""
super(TelegramClient, self).reconnect(
device_model=self.device_model,
system_version=self.system_version,
app_version=self.app_version,
lang_code=self.lang_code,
new_dc=new_dc
)
super(TelegramClient, self).reconnect(new_dc=new_dc)
# endregion
@ -186,13 +177,7 @@ class TelegramClient(TelegramBareClient):
client = self._cached_clients.get(dc_id)
if client:
if init_connection:
client.reconnect(
device_model=self.device_model,
system_version=self.system_version,
app_version=self.app_version,
lang_code=self.lang_code
)
client.reconnect()
return client
else:
dc = self._get_dc(dc_id)
@ -206,9 +191,7 @@ class TelegramClient(TelegramBareClient):
session.server_address = dc.ip_address
session.port = dc.port
client = TelegramBareClient(session, self.api_id, self.api_hash)
client.connect(self.device_model, self.system_version,
self.app_version, self.lang_code,
exported_auth=export_auth)
client.connect(exported_auth=export_auth)
# Don't go through this expensive process every time.
self._cached_clients[dc_id] = client

View File

@ -1,6 +1,7 @@
import json
import os
import pickle
import platform
import random
import time
from threading import Lock
@ -98,7 +99,19 @@ class JsonSession:
through an official Telegram client to revoke the authorization.
"""
def __init__(self, session_user_id):
# These values will NOT be saved
self.session_user_id = session_user_id
# For connection purposes
self.device_model = platform.node()
self.system_version = platform.system()
self.app_version = '0'
self.lang_code = 'en'
# Cross-thread safety
self._lock = Lock()
# These values will be saved
self.server_address = '91.108.56.165'
self.port = 443
self.auth_key = None
@ -108,9 +121,6 @@ class JsonSession:
self.time_offset = 0
self.last_message_id = 0 # Long
# Cross-thread safety
self._lock = Lock()
def save(self):
"""Saves the current session object as session_user_id.session"""
if self.session_user_id: