SielBotWebsite/siel.py
2026-09-29 06:34:54 +02:00

50 lines
1.5 KiB
Python

"""Zugriff auf die interne API von Siel."""
from __future__ import annotations
import httpx
import config
class SielError(Exception):
"""Siel ist nicht erreichbar oder hat eine Anfrage abgelehnt."""
async def _request(method: str, path: str, *, json=None, user: str = "") -> dict:
headers = {"X-API-Key": config.SIEL_API_KEY}
if user:
headers["X-Panel-User"] = user
try:
async with httpx.AsyncClient(base_url=config.SIEL_API_URL, timeout=10) as client:
res = await client.request(method, path, json=json, headers=headers)
except httpx.HTTPError:
raise SielError("Siel ist gerade nicht erreichbar.")
if res.status_code >= 400:
try:
detail = res.json().get("detail")
except ValueError:
detail = None
if isinstance(detail, list): # Pruefungsfehler von FastAPI
detail = "Ungültige Eingabe"
raise SielError(detail or f"Siel hat mit Status {res.status_code} geantwortet.")
return res.json()
async def status() -> dict:
return await _request("GET", "/api/status")
async def guild() -> dict:
return await _request("GET", "/api/guild")
async def user(discord_id: str) -> dict:
return await _request("GET", f"/api/users/{discord_id}")
async def get_module(name: str) -> dict:
return await _request("GET", f"/api/modules/{name}")
async def put_module(name: str, data: dict, by: str) -> dict:
return await _request("PUT", f"/api/modules/{name}", json=data, user=by)