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:
async with session.post(endpoint, **kwargs) as res: try:
try: async with session.post(endpoint, **kwargs) as res:
data = await res.json(content_type=None) try:
except JSONDecodeError: data = await res.json(content_type=None)
raise AuthException(endpoint, res.status, {"invalid": await res.text()}, kwargs) except JSONDecodeError:
logger.debug("POST /%s [%s] : %s", endpoint, str(kwargs), str(data)) raise AuthException(endpoint, res.status, {"error": "Invalid JSON response", "response": await res.text()}, kwargs)
if res.status >= 400: logger.debug("POST /%s [%s] : %s", endpoint, str(kwargs), str(data))
raise AuthException(endpoint, res.status, data, kwargs) if res.status >= 400:
return data raise AuthException(endpoint, res.status, data, kwargs)
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:
async with session.get(endpoint, **kwargs) as res: try:
try: async with session.get(endpoint, **kwargs) as res:
data = await res.json(content_type=None) try:
except JSONDecodeError: data = await res.json(content_type=None)
raise AuthException(endpoint, res.status, {"invalid": await res.text()}, kwargs) except JSONDecodeError:
logger.debug("GET /%s [%s] : %s", endpoint, str(kwargs), str(data)) raise AuthException(endpoint, res.status, {"error": "Invalid JSON response", "response": await res.text()}, kwargs)
if res.status >= 400: logger.debug("GET /%s [%s] : %s", endpoint, str(kwargs), str(data))
raise AuthException(endpoint, res.status, data, kwargs) if res.status >= 400:
return data raise AuthException(endpoint, res.status, data, kwargs)
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=''):