Replace .to_bytes() with the special .__bytes__ function

This commit is contained in:
Lonami Exo
2017-10-17 19:54:24 +02:00
parent 63dfb1e3ea
commit adb79b21cf
7 changed files with 22 additions and 22 deletions

View File

@@ -13,21 +13,21 @@ class GzipPacked(TLObject):
@staticmethod
def gzip_if_smaller(request):
"""Calls request.to_bytes(), and based on a certain threshold,
"""Calls bytes(request), and based on a certain threshold,
optionally gzips the resulting data. If the gzipped data is
smaller than the original byte array, this is returned instead.
Note that this only applies to content related requests.
"""
data = request.to_bytes()
data = bytes(request)
# TODO This threshold could be configurable
if request.content_related and len(data) > 512:
gzipped = GzipPacked(data).to_bytes()
gzipped = bytes(GzipPacked(data))
return gzipped if len(gzipped) < len(data) else data
else:
return data
def to_bytes(self):
def __bytes__(self):
# TODO Maybe compress level could be an option
return struct.pack('<I', GzipPacked.CONSTRUCTOR_ID) + \
TLObject.serialize_bytes(gzip.compress(self.data))