Skip to content

Commit

Permalink
use implemented exceptions rather than Exception
Browse files Browse the repository at this point in the history
Signed-off-by: ItsNeil17 <[email protected]>
  • Loading branch information
ItsNeil17 committed Jun 9, 2024
1 parent 707935b commit 61a519c
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions backend/api.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
return alliance

0 comments on commit 61a519c

Please sign in to comment.