diff --git a/src/api/rootme_api.py b/src/api/rootme_api.py index 8277639..6a849fa 100644 --- a/src/api/rootme_api.py +++ b/src/api/rootme_api.py @@ -1,6 +1,11 @@ +import logging + from os import getenv -from api.rate_limiter import RateLimiter +from api.rate_limiter import RLRequestFailed, RateLimiter +class RootMeAPIError(Exception): + def __init__(self, *args: object) -> None: + super().__init__(*args) class RootMeAPIManager: """ @@ -18,6 +23,7 @@ def __init__(self, rate_limiter: RateLimiter): else: raise RuntimeError("API_URL is not set.") self.rate_limiter = rate_limiter + self.logger = logging.getLogger(__name__) async def get_challenge_by_id(self, _id): """ @@ -25,11 +31,17 @@ async def get_challenge_by_id(self, _id): -> returns the raw json for now """ # use the api_key in the cookies + request_url = f"{self.API_URL}/challenges/{_id}" cookies = {"api_key": self.API_KEY.strip('"')} + request_method = "GET" # ask the rate limiter for the request - data = await self.rate_limiter.make_request( - f"{self.API_URL}/challenges/{_id}", cookies, "GET" - ) + try: + data = await self.rate_limiter.make_request( + request_url, cookies, request_method + ) + except RLRequestFailed as exc: + self.logger.error("%s Request to get challenge %s failed.", request_method, request_url) + raise RootMeAPIError() # Handle the fact the request is wrong return data async def get_user_by_id(self, _id): @@ -38,7 +50,15 @@ async def get_user_by_id(self, _id): -> returns the raw json for now """ # use the api_key in the cookies + request_url = f"{self.API_URL}/auteurs/{_id}" cookies = {"api_key": self.API_KEY.strip('"')} + request_method = "GET" # ask the rate limiter for the request - data = await self.rate_limiter.make_request(f"{self.API_URL}/auteurs/{_id}", cookies, "GET") + try: + data = await self.rate_limiter.make_request( + request_url, cookies, request_method + ) + except RLRequestFailed as exc: + self.logger.error("%s Request to get user %s failed.", request_method, request_url) + raise RootMeAPIError() return data diff --git a/src/bot/dummy_db_manager.py b/src/bot/dummy_db_manager.py index 79b5c91..e0e3463 100644 --- a/src/bot/dummy_db_manager.py +++ b/src/bot/dummy_db_manager.py @@ -1,5 +1,6 @@ import logging +from api.rootme_api import RootMeAPIError, RootMeAPIManager from classes import User from classes import Challenge @@ -19,7 +20,7 @@ def __init__(self, idx=None, message=None): class DummyDBManager: - def __init__(self, api_manager): + def __init__(self, api_manager: RootMeAPIManager): self.users = [] self.api_manager = api_manager @@ -32,7 +33,10 @@ async def add_user(self, idx): if self.has_user(idx): return None - raw_user_data = await self.api_manager.get_user_by_id(idx) + try: + raw_user_data = await self.api_manager.get_user_by_id(idx) + except RootMeAPIError: + return None user = User(raw_user_data) self.users.append(user) @@ -54,7 +58,14 @@ async def fetch_user_new_solves(self, idx): if user is None: raise InvalidUser(idx, "DummyDBManager.fetch_user_new_solves: User %s not in database") - raw_user_data = await self.api_manager.get_user_by_id(idx) + try: + raw_user_data = await self.api_manager.get_user_by_id(idx) + except RootMeAPIError: + # If for some reason we can get the user on this iteration + # we will get him next time maybe ... + self.logger.error("User %s could not be fetch from the API, yet we keep running", idx) + return + user.update_new_solves(raw_user_data) if not user.has_new_solves(): self.logger.debug("'%s' hasn't any new solves", user) @@ -62,7 +73,11 @@ async def fetch_user_new_solves(self, idx): self.logger.info("'%s' has %s new solves", user, user.nb_new_solves) for challenge_id in user.yield_new_solves(raw_user_data): - challenge_data = await self.api_manager.get_challenge_by_id(challenge_id) + try: + challenge_data = await self.api_manager.get_challenge_by_id(challenge_id) + except RootMeAPIError: + # If we can't fetch the challenge, sadly there is not much we can do + continue challenge = Challenge(challenge_id, challenge_data) self.logger.debug("'%s' solved '%s'", repr(user), repr(challenge)) yield challenge