Subclass TLRequest for content-related objects

This commit is contained in:
Lonami Exo
2018-06-12 20:05:05 +02:00
parent d1afc70963
commit 3f16c92eb3
7 changed files with 71 additions and 81 deletions

View File

@@ -3,17 +3,17 @@ import itertools
from .telegrambaseclient import TelegramBaseClient
from .. import errors, utils
from ..tl import TLObject, types, functions
from ..tl import TLObject, TLRequest, types, functions
_NOT_A_REQUEST = TypeError('You can only invoke requests, not types!')
class UserMethods(TelegramBaseClient):
async def __call__(self, request, retries=5, ordered=False):
requests = (request,) if not utils.is_list_like(request) else request
if not all(isinstance(x, TLObject) and
x.content_related for x in requests):
raise TypeError('You can only invoke requests, not types!')
for r in requests:
for r in (request if utils.is_list_like(request) else (request,)):
if not isinstance(r, TLRequest):
raise _NOT_A_REQUEST
await r.resolve(self, utils)
for _ in range(retries):

View File

@@ -8,6 +8,7 @@ from ..crypto import AES
from ..errors import SecurityError, BrokenAuthKeyError
from ..extensions import BinaryReader
from ..tl.core import TLMessage
from ..tl.tlobject import TLRequest
__log__ = logging.getLogger(__name__)
@@ -43,7 +44,7 @@ class MTProtoState:
"""
return TLMessage(
msg_id=self._get_new_msg_id(),
seq_no=self._get_seq_no(obj.content_related),
seq_no=self._get_seq_no(isinstance(obj, TLRequest)),
obj=obj,
after_id=after.msg_id if after else None
)

View File

@@ -1 +1 @@
from .tlobject import TLObject
from .tlobject import TLObject, TLRequest

View File

@@ -1,7 +1,7 @@
import gzip
import struct
from .. import TLObject
from .. import TLObject, TLRequest
class GzipPacked(TLObject):
@@ -21,7 +21,7 @@ class GzipPacked(TLObject):
"""
data = bytes(request)
# TODO This threshold could be configurable
if request.content_related and len(data) > 512:
if isinstance(request, TLRequest) and len(data) > 512:
gzipped = bytes(GzipPacked(data))
return gzipped if len(gzipped) < len(data) else data
else:

View File

@@ -11,13 +11,10 @@ class MessageContainer(TLObject):
CONSTRUCTOR_ID = 0x73f1f8dc
def __init__(self, messages):
super().__init__()
self.content_related = False
self.messages = messages
def to_dict(self, recursive=True):
return {
'content_related': self.content_related,
'messages':
([] if self.messages is None else [
None if x is None else x.to_dict() for x in self.messages

View File

@@ -3,16 +3,11 @@ from datetime import datetime, date
class TLObject:
def __init__(self):
# TODO Perhaps content_related makes more sense as another type?
# Something like class TLRequest(TLObject), request inherit this
self.content_related = False # Only requests/functions/queries are
# These should not be overrode
@staticmethod
def pretty_format(obj, indent=None):
"""Pretty formats the given object as a string which is returned.
If indent is None, a single line will be returned.
"""
Pretty formats the given object as a string which is returned.
If indent is None, a single line will be returned.
"""
if indent is None:
if isinstance(obj, TLObject):
@@ -136,11 +131,6 @@ class TLObject:
raise TypeError('Cannot interpret "{}" as a date.'.format(dt))
# These are nearly always the same for all subclasses
@staticmethod
def read_result(reader):
return reader.tgread_object()
def __eq__(self, o):
return isinstance(o, type(self)) and self.to_dict() == o.to_dict()
@@ -153,16 +143,24 @@ class TLObject:
def stringify(self):
return TLObject.pretty_format(self, indent=0)
# These should be overrode
async def resolve(self, client, utils):
pass
def to_dict(self):
return {}
raise NotImplementedError
def __bytes__(self):
return b''
raise NotImplementedError
@classmethod
def from_reader(cls, reader):
return TLObject()
raise NotImplementedError
class TLRequest(TLObject):
"""
Represents a content-related `TLObject` (a request that can be sent).
"""
@staticmethod
def read_result(reader):
return reader.tgread_object()
async def resolve(self, client, utils):
pass