Fix a couple of inconsistencies in the public interface (#1102)

* Create `_NOT_A_REQUEST` when needed. Currently, modifications    
  in the raised exception would be "global".    
* `retries` parameters were actually attempts. This has been fixed    
  to actually be the amount of retries, so 0 now means don't retry.    
* Helper function to deal with retries instead of using a range with    
  different styles every time.
This commit is contained in:
Dmitry D. Chernov
2019-02-07 04:41:45 +10:00
committed by Lonami
parent c9e9b82eac
commit c8f16a4e89
5 changed files with 50 additions and 30 deletions

View File

@@ -6,8 +6,9 @@ from .telegrambaseclient import TelegramBaseClient
from .. import errors, utils
from ..errors import MultiError, RPCError
from ..tl import TLObject, TLRequest, types, functions
from ..helpers import retry_range
_NOT_A_REQUEST = TypeError('You can only invoke requests, not types!')
_NOT_A_REQUEST = lambda: TypeError('You can only invoke requests, not types!')
class UserMethods(TelegramBaseClient):
@@ -15,7 +16,7 @@ class UserMethods(TelegramBaseClient):
requests = (request if utils.is_list_like(request) else (request,))
for r in requests:
if not isinstance(r, TLRequest):
raise _NOT_A_REQUEST
raise _NOT_A_REQUEST()
await r.resolve(self, utils)
# Avoid making the request if it's already in a flood wait
@@ -34,7 +35,7 @@ class UserMethods(TelegramBaseClient):
request_index = 0
self._last_request = time.time()
for _ in range(self._request_retries):
for attempt in retry_range(self._request_retries):
try:
future = self._sender.send(request, ordered=ordered)
if isinstance(future, list):
@@ -86,7 +87,8 @@ class UserMethods(TelegramBaseClient):
raise
await self._switch_dc(e.new_dc)
raise ValueError('Number of retries reached 0')
raise ValueError('Request was unsuccessful {} time(s)'
.format(attempt))
# region Public methods