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