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-06 19:41:45 +01:00
committed by Lonami
parent c9e9b82eac
commit c8f16a4e89
5 changed files with 50 additions and 30 deletions
+1 -1
View File
@@ -53,7 +53,7 @@ class _TakeoutClient:
wrapped = []
for r in requests:
if not isinstance(r, TLRequest):
raise _NOT_A_REQUEST
raise _NOT_A_REQUEST()
await r.resolve(self, utils)
wrapped.append(functions.InvokeWithTakeoutRequest(takeout_id, r))
+10 -10
View File
@@ -71,23 +71,23 @@ class TelegramBaseClient(abc.ABC):
invoked requests, and you should use ``asyncio.wait`` or
``asyncio.wait_for`` for that.
request_retries (`int`, optional):
request_retries (`int` | `None`, optional):
How many times a request should be retried. Request are retried
when Telegram is having internal issues (due to either
``errors.ServerError`` or ``errors.RpcCallFailError``),
when there is a ``errors.FloodWaitError`` less than
`flood_sleep_threshold`, or when there's a migrate error.
May set to a false-y value (``0`` or ``None``) for infinite
retries, but this is not recommended, since some requests can
always trigger a call fail (such as searching for messages).
May take a negative or ``None`` value for infinite retries, but
this is not recommended, since some requests can always trigger
a call fail (such as searching for messages).
connection_retries (`int`, optional):
connection_retries (`int` | `None`, optional):
How many times the reconnection should retry, either on the
initial connection or when Telegram disconnects us. May be
set to a false-y value (``0`` or ``None``) for infinite
retries, but this is not recommended, since the program can
get stuck in an infinite loop.
set to a negative or ``None`` value for infinite retries, but
this is not recommended, since the program can get stuck in an
infinite loop.
retry_delay (`int` | `float`, optional):
The delay in seconds to sleep between automatic reconnections.
@@ -236,8 +236,8 @@ class TelegramBaseClient(abc.ABC):
self.api_id = int(api_id)
self.api_hash = api_hash
self._request_retries = request_retries or sys.maxsize
self._connection_retries = connection_retries or sys.maxsize
self._request_retries = request_retries
self._connection_retries = connection_retries
self._retry_delay = retry_delay or 0
self._proxy = proxy
self._timeout = timeout
+6 -4
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