diff --git a/backend.py b/backend.py deleted file mode 100644 index cc46f35..0000000 --- a/backend.py +++ /dev/null @@ -1,53 +0,0 @@ -import requests # this is a synchronous application -from models import Player, Alliance - -API_URL = "https://api.willofsteel.me" - - -class Route: - def __init__(self, path, method): - self.path = API_URL + path - self.method = method.upper() - - def __str__(self): - return self.path - - def __repr__(self): - return f"Route({self.method} {self.path})" - - def __eq__(self, other): - return self.path == other.path and self.method == other.method - - def __hash__(self): - return hash((self.path, self.method)) - - -class API: - def __init__(self, api_key: str): - self.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 - ) - if response.status_code == 403: - raise Exception("Access Forbidden") - # handle error here however you want to - # error 403 & 401 are Access Forbidden - - return response - - def get_player(self) -> Player: - route = Route("/player", "GET") - response = self.request(route) - data = response.json() - player = Player.from_data(data, None) - 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 diff --git a/backend/api.py b/backend/api.py index d584c7f..7754da5 100644 --- a/backend/api.py +++ b/backend/api.py @@ -8,14 +8,30 @@ __all__ = ("API",) +from typing import Dict + +__all__ = ("API",) + + class API: def __init__(self, api_key: str): self.api_key = api_key - self.headers = {"API-Key": api_key} - def request(self, route: Route, data: dict = None) -> dict: + def request( + self, route: Route, *, query_params: dict = None, json: dict = None, **kwargs + ): + method = route.method + url = route.url + + headers: Dict[str, str] = { + "User-Agent": "WillofSteel API Client/1.0", + "Content-Type": "application/json", + "API-Key": self.api_key, + } + response = requests.request( - route.method, route.path, headers=self.headers, json=data + method, url, headers=headers, params=query_params, json=json, **kwargs + ) if response.status_code == 403: raise AccessForbidden(response.status_code) diff --git a/backend/route.py b/backend/route.py index a81cfb7..1adedbf 100644 --- a/backend/route.py +++ b/backend/route.py @@ -1,7 +1,7 @@ -__all__ = "Route" - API_URL = "https://api.willofsteel.me" +__all__ = ("Route",) + class Route: def __init__(self, path, method): diff --git a/models/alliance.py b/models/alliance.py index 445c99c..1244d12 100644 --- a/models/alliance.py +++ b/models/alliance.py @@ -1,7 +1,7 @@ from datetime import datetime from typing import NamedTuple -__all__ = ["Alliance"] +__all__ = ("Alliance",) class Alliance(NamedTuple): diff --git a/models/characteristics.py b/models/characteristics.py index 341bf51..444e71d 100644 --- a/models/characteristics.py +++ b/models/characteristics.py @@ -1,6 +1,6 @@ from typing import NamedTuple -__all__ = ["Characteristics"] +__all__ = ("Characteristics",) class Characteristics(NamedTuple): diff --git a/models/order.py b/models/order.py index 7893bb0..2fb8720 100644 --- a/models/order.py +++ b/models/order.py @@ -1,6 +1,6 @@ from typing import NamedTuple -__all__ = ["MarketOrder"] +__all__ = ("MarketOrder",) class MarketOrder(NamedTuple): diff --git a/models/player.py b/models/player.py index 0c59568..c39a9f1 100644 --- a/models/player.py +++ b/models/player.py @@ -4,7 +4,7 @@ from .units import UnitType from .alliance import Alliance -__all__ = ["Player", "EventPlayer"] +__all__ = ("Player", "EventPlayer") class Player(NamedTuple): diff --git a/models/units.py b/models/units.py index 11cae47..81fff42 100644 --- a/models/units.py +++ b/models/units.py @@ -3,7 +3,7 @@ import emote -__all__ = ["UnitType"] +__all__ = ("UnitType",) class UnitProperties(NamedTuple):