From 707935b5681c9670aa11ad2dcebebec065cff84f Mon Sep 17 00:00:00 2001 From: ItsNeil17 Date: Sun, 9 Jun 2024 20:42:22 +0530 Subject: [PATCH 1/4] implement exceptions Signed-off-by: ItsNeil17 --- backend/exceptions.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 backend/exceptions.py diff --git a/backend/exceptions.py b/backend/exceptions.py new file mode 100644 index 0000000..b9322b0 --- /dev/null +++ b/backend/exceptions.py @@ -0,0 +1,19 @@ +from __future__ import annotations + + +class BackendException(BaseException): + """Base exception for all exceptions in this module""" + + +class AccessForbidden(BackendException): + def __init__(self, status_code: int) -> None: + super().__init__(f"API returned {status_code}") + + +class ValidationError(BackendException): + def __init__(self, status_code: int, payload: dict) -> None: + self.payload = payload + super().__init__( + f"API returned {status_code}. This usually occurs to arguments not being specified or the API being unable to process this request. ", + payload, + ) From 61a519c7029d4a5cb6cd4b8f6355db4ef96255f7 Mon Sep 17 00:00:00 2001 From: ItsNeil17 Date: Sun, 9 Jun 2024 20:43:30 +0530 Subject: [PATCH 2/4] use implemented exceptions rather than Exception Signed-off-by: ItsNeil17 --- backend/api.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/backend/api.py b/backend/api.py index 35f44de..d429a68 100644 --- a/backend/api.py +++ b/backend/api.py @@ -1,20 +1,26 @@ +from __future__ import annotations + import requests from backend import Route from models import Player, Alliance +from .exceptions import AccessForbidden, ValidationError + class API: def __init__(self, api_key: str): self.api_key = api_key - self.headers = { - "API-Key": api_key - } + self.headers = {"API-Key": api_key} def request(self, route: Route, data: dict = None) -> dict: - response = requests.request(route.method, route.path, headers=self.headers, json=data) + response = requests.request( + route.method, route.path, headers=self.headers, json=data + ) if response.status_code == 403: - raise Exception("Access Forbidden") + raise AccessForbidden(response.status_code) # handle error here however you want to # error 403 & 401 are Access Forbidden + elif response.status_code == 422: + raise ValidationError(response.status_code, data) return response @@ -24,10 +30,10 @@ def get_player(self) -> Player: data = response.json() player = Player.from_data(data) return player - + def get_alliance(self) -> Alliance: route = Route("/alliance", "GET") response = self.request(route) data = response.json() alliance = Alliance.from_data(data) - return alliance \ No newline at end of file + return alliance From cc35c97c9126dcb55d783157019b9d83d451d1c7 Mon Sep 17 00:00:00 2001 From: ItsNeil17 Date: Sun, 9 Jun 2024 20:52:32 +0530 Subject: [PATCH 3/4] fix linting Signed-off-by: ItsNeil17 --- backend/__init__.py | 2 -- backend/api.py | 2 ++ backend/exceptions.py | 2 ++ backend/route.py | 6 +++--- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/backend/__init__.py b/backend/__init__.py index eec76f2..e69de29 100644 --- a/backend/__init__.py +++ b/backend/__init__.py @@ -1,2 +0,0 @@ -from .route import Route -from .api import API diff --git a/backend/api.py b/backend/api.py index d429a68..d584c7f 100644 --- a/backend/api.py +++ b/backend/api.py @@ -5,6 +5,8 @@ from models import Player, Alliance from .exceptions import AccessForbidden, ValidationError +__all__ = ("API",) + class API: def __init__(self, api_key: str): diff --git a/backend/exceptions.py b/backend/exceptions.py index b9322b0..64ec24b 100644 --- a/backend/exceptions.py +++ b/backend/exceptions.py @@ -1,5 +1,7 @@ from __future__ import annotations +__all__ = ("BackendException", "AccessForbidden", "ValidationError") + class BackendException(BaseException): """Base exception for all exceptions in this module""" diff --git a/backend/route.py b/backend/route.py index 0ac310e..a81cfb7 100644 --- a/backend/route.py +++ b/backend/route.py @@ -1,8 +1,8 @@ -import requests # this is a synchronous application -from models import Player, Alliance +__all__ = "Route" API_URL = "https://api.willofsteel.me" + class Route: def __init__(self, path, method): self.path = API_URL + path @@ -18,4 +18,4 @@ def __eq__(self, other): return self.path == other.path and self.method == other.method def __hash__(self): - return hash((self.path, self.method)) \ No newline at end of file + return hash((self.path, self.method)) From 89ebb74b8ca218caab6afdcd820955c311744cb7 Mon Sep 17 00:00:00 2001 From: ItsNeil17 Date: Sun, 9 Jun 2024 23:46:02 +0530 Subject: [PATCH 4/4] fix linting Signed-off-by: ItsNeil17 --- backend/api.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/backend/api.py b/backend/api.py index 7754da5..5803cd3 100644 --- a/backend/api.py +++ b/backend/api.py @@ -4,10 +4,6 @@ from backend import Route from models import Player, Alliance from .exceptions import AccessForbidden, ValidationError - -__all__ = ("API",) - - from typing import Dict __all__ = ("API",) @@ -31,14 +27,13 @@ def request( response = requests.request( method, url, headers=headers, params=query_params, json=json, **kwargs - ) if response.status_code == 403: raise AccessForbidden(response.status_code) # handle error here however you want to # error 403 & 401 are Access Forbidden elif response.status_code == 422: - raise ValidationError(response.status_code, data) + raise ValidationError(response.status_code, json) return response