Added error information to api

This commit is contained in:
2025-08-29 22:55:20 +02:00
parent d8dd7c2c5e
commit 8e794ec7f5
2 changed files with 12 additions and 4 deletions

View File

@@ -6,6 +6,7 @@ class service:
status: Optional[int] status: Optional[int]
online: bool online: bool
public: bool public: bool
error: Optional[str]
def __init__(self, url: str = "", public: bool = True): def __init__(self, url: str = "", public: bool = True):
self.url = url self.url = url
@@ -13,6 +14,7 @@ class service:
self.online = False self.online = False
self.status = None self.status = None
self.error = None
def to_dict(self) -> dict[str, Any]: def to_dict(self) -> dict[str, Any]:
return { return {
@@ -20,14 +22,18 @@ class service:
"status": self.status, "status": self.status,
"public": self.public, "public": self.public,
"online": self.online, "online": self.online,
"error": self.error,
} }
def set_status(self, status: int): def set_status(self, status: Optional[int]):
self.status = status self.status = status
def set_online(self, b: bool): def set_online(self, b: bool):
self.online = b self.online = b
def set_error(self, s: Optional[str]):
self.error = s
services: list[service] = [ services: list[service] = [
service("https://git.ihatemen.uk"), service("https://git.ihatemen.uk"),

View File

@@ -13,11 +13,13 @@ async def check_service(client: httpx.AsyncClient, s: service):
follow_redirects=True, follow_redirects=True,
timeout=1, timeout=1,
) )
print(r.status_code) s.set_error(None)
s.set_online(r.status_code == 200) s.set_online(r.status_code == 200)
except httpx.RequestError as e: s.set_status(r.status_code)
print(e) except httpx.HTTPError as e:
s.set_error(str(e))
s.set_online(False) s.set_online(False)
s.set_status(None)
def start_async_loop(): def start_async_loop():