1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00

Fixed timeout

This commit is contained in:
Steffo 2017-03-12 18:50:18 +01:00
parent 60876e92b2
commit fd76f5b432

View file

@ -49,7 +49,7 @@ class Bot:
async def get_updates(self): async def get_updates(self):
"""Get the latest updates from the Telegram API with /getUpdates.""" """Get the latest updates from the Telegram API with /getUpdates."""
try: try:
data = await self.api_request("getUpdates", 300, offset=self.offset, timeout=300) data = await self.api_request("getUpdates", offset=self.offset, timeout=300)
except asyncio.TimeoutError: except asyncio.TimeoutError:
return return
for update in data: for update in data:
@ -129,10 +129,13 @@ class Bot:
if chat.chat_id == chat_id: if chat.chat_id == chat_id:
return chat return chat
async def api_request(self, endpoint, t=10, **params): async def api_request(self, endpoint, **params):
"""Send a request to the Telegram API at the specified endpoint.""" """Send a request to the Telegram API at the specified endpoint."""
# Request timeout is 10 seconds. if "timeout" in params:
# TODO: get rid of that t variable t = params["timeout"] + 5
else:
# Default timeout is 5 seconds.
t = 5
with async_timeout.timeout(t): with async_timeout.timeout(t):
# Create a new session for each request. # Create a new session for each request.
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
@ -144,12 +147,12 @@ class Bot:
raise TelegramAPIError(f"Request returned {response.status} {response.reason}") raise TelegramAPIError(f"Request returned {response.status} {response.reason}")
# Parse the json data as soon it's ready # Parse the json data as soon it's ready
data = await response.json() data = await response.json()
# Check for errors in the response # Check for errors in the response
if not data["ok"]: if not data["ok"]:
error = data["description"] error = data["description"]
raise TelegramAPIError(f"Response returned an error: {error}") raise TelegramAPIError(f"Response returned an error: {error}")
# Return a dictionary containing the data # Return a dictionary containing the data
return data["result"] return data["result"]
class Update: class Update: