Handle FloodWaitError in client.download_media

Fixes #1426. Not entirely happy with the new indirection layer,
but the original __call__ method is already a mess anyway and
the additional cost of an extra call should be neglible compared
to the actual IO.
This commit is contained in:
Lonami Exo 2020-04-28 20:49:57 +02:00
parent 7f3aa43ad4
commit 7ea4686d6c
3 changed files with 6 additions and 3 deletions

View File

@ -68,7 +68,7 @@ class _DirectDownloadIter(RequestIter):
async def _request(self): async def _request(self):
try: try:
result = await self._sender.send(self.request) result = await self.client._call(self._sender, self.request)
if isinstance(result, types.upload.FileCdnRedirect): if isinstance(result, types.upload.FileCdnRedirect):
raise NotImplementedError # TODO Implement raise NotImplementedError # TODO Implement
else: else:

View File

@ -1039,7 +1039,7 @@ class MessageMethods:
if exported: if exported:
try: try:
sender = await self._borrow_exported_sender(entity.dc_id) sender = await self._borrow_exported_sender(entity.dc_id)
return await sender.send(request) return await self._call(sender, request)
finally: finally:
await self._return_exported_sender(sender) await self._return_exported_sender(sender)
else: else:

View File

@ -27,6 +27,9 @@ def _fmt_flood(delay, request, *, early=False, td=datetime.timedelta):
class UserMethods: class UserMethods:
async def __call__(self: 'TelegramClient', request, ordered=False): async def __call__(self: 'TelegramClient', request, ordered=False):
return await self._call(self._sender, request, ordered=ordered)
async def _call(self: 'TelegramClient', sender, request, ordered=False):
requests = (request if utils.is_list_like(request) else (request,)) requests = (request if utils.is_list_like(request) else (request,))
for r in requests: for r in requests:
if not isinstance(r, TLRequest): if not isinstance(r, TLRequest):
@ -50,7 +53,7 @@ class UserMethods:
self._last_request = time.time() self._last_request = time.time()
for attempt in retry_range(self._request_retries): for attempt in retry_range(self._request_retries):
try: try:
future = self._sender.send(request, ordered=ordered) future = sender.send(request, ordered=ordered)
if isinstance(future, list): if isinstance(future, list):
results = [] results = []
exceptions = [] exceptions = []