API host as env var

This commit is contained in:
Corban-Lee Jones 2024-02-07 21:22:45 +00:00
parent 83a889cf61
commit 91077270f7

View File

@ -1,5 +1,6 @@
import logging
from os import getenv
import aiohttp
@ -13,14 +14,26 @@ class APIException(Exception):
class NotCreatedException(APIException):
pass
class BadStatusException(APIException):
pass
def normalize_api_host(host: str):
if not host:
raise ValueError("host cannot be empty")
if not host.endswith("/"):
host += "/"
return host
class API:
"""Interactions with the API."""
API_HOST = "http://localhost:8000/"
API_HOST = normalize_api_host(getenv("API_HOST"))
API_ENDPOINT = API_HOST + "api/"
RSS_FEED_ENDPOINT = API_ENDPOINT + "rssfeed/"
@ -78,11 +91,11 @@ class API:
form = aiohttp.FormData({
"name": name,
"url": url,
"discord_server_id": discord_server_id
"discord_server_id": str(discord_server_id)
})
form.add_field("image", image_data, filename="file.jpg")
data = (await self.make_response("POST", self.RSS_FEED_ENDPOINT, data=form))["json"]
data = (await self.make_request("POST", self.RSS_FEED_ENDPOINT, data=form))["json"]
return data
async def get_rssfeed(self, uuid: str) -> dict: