fix: catch timed out error in auth requests

This commit is contained in:
əlemi 2022-06-18 16:11:06 +02:00
parent 4d4ad4a6d6
commit 778fbe76f3
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E

View file

@ -1,6 +1,6 @@
"""Minecraft authentication interface""" """Minecraft authentication interface"""
import logging import logging
from multiprocessing.sharedctypes import Value from asyncio.exceptions import TimeoutError
from typing import Optional, Dict, Any from typing import Optional, Dict, Any
import aiohttp import aiohttp
@ -66,28 +66,34 @@ class AuthInterface:
@classmethod @classmethod
async def _post(cls, endpoint:str, **kwargs) -> Dict[str, Any]: async def _post(cls, endpoint:str, **kwargs) -> Dict[str, Any]:
async with aiohttp.ClientSession(timeout=cls.TIMEOUT) as session: async with aiohttp.ClientSession(timeout=cls.TIMEOUT) as session:
try:
async with session.post(endpoint, **kwargs) as res: async with session.post(endpoint, **kwargs) as res:
try: try:
data = await res.json(content_type=None) data = await res.json(content_type=None)
except JSONDecodeError: except JSONDecodeError:
raise AuthException(endpoint, res.status, {"invalid": await res.text()}, kwargs) raise AuthException(endpoint, res.status, {"error": "Invalid JSON response", "response": await res.text()}, kwargs)
logger.debug("POST /%s [%s] : %s", endpoint, str(kwargs), str(data)) logger.debug("POST /%s [%s] : %s", endpoint, str(kwargs), str(data))
if res.status >= 400: if res.status >= 400:
raise AuthException(endpoint, res.status, data, kwargs) raise AuthException(endpoint, res.status, data, kwargs)
return data return data
except TimeoutError:
raise AuthException(endpoint, res.status, {"error": "request timed out"}, kwargs)
@classmethod @classmethod
async def _get(cls, endpoint:str, **kwargs) -> Dict[str, Any]: async def _get(cls, endpoint:str, **kwargs) -> Dict[str, Any]:
async with aiohttp.ClientSession(timeout=cls.TIMEOUT) as session: async with aiohttp.ClientSession(timeout=cls.TIMEOUT) as session:
try:
async with session.get(endpoint, **kwargs) as res: async with session.get(endpoint, **kwargs) as res:
try: try:
data = await res.json(content_type=None) data = await res.json(content_type=None)
except JSONDecodeError: except JSONDecodeError:
raise AuthException(endpoint, res.status, {"invalid": await res.text()}, kwargs) raise AuthException(endpoint, res.status, {"error": "Invalid JSON response", "response": await res.text()}, kwargs)
logger.debug("GET /%s [%s] : %s", endpoint, str(kwargs), str(data)) logger.debug("GET /%s [%s] : %s", endpoint, str(kwargs), str(data))
if res.status >= 400: if res.status >= 400:
raise AuthException(endpoint, res.status, data, kwargs) raise AuthException(endpoint, res.status, data, kwargs)
return data return data
except TimeoutError:
raise AuthException(endpoint, res.status, {"error": "request timed out"}, kwargs)
class OfflineAuthenticator(AuthInterface): class OfflineAuthenticator(AuthInterface):
def __init__(self, name:str, id:str=''): def __init__(self, name:str, id:str=''):