SielBotWebsite/siel.py
Maaxxs 585cbc5ac2 Begrüßung: Willkommensnachricht mit Karte, private Nachricht, Startrollen und Abschied
Nachrichten-Editor mit Discord-Vorschau (Text oder Einbettung), Live-Vorschau der
Willkommenskarte, eigener Kartenhintergrund und Test per Direktnachricht.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 06:59:52 +02:00

78 lines
2.6 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)
async def raw(method: str, path: str, *, json=None, content: bytes | None = None,
content_type: str | None = None) -> tuple[bytes, str]:
"""Anfrage, deren Antwort ein Bild ist (Vorschau, Hintergrund)."""
headers = {"X-API-Key": config.SIEL_API_KEY}
if content_type:
headers["Content-Type"] = content_type
try:
async with httpx.AsyncClient(base_url=config.SIEL_API_URL, timeout=30) as client:
res = await client.request(method, path, json=json, content=content, 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
raise SielError(detail if isinstance(detail, str) else f"Siel hat mit Status {res.status_code} geantwortet.")
return res.content, res.headers.get("content-type", "application/octet-stream")
async def post(path: str, data: dict) -> dict:
return await _request("POST", path, json=data)
async def delete(path: str) -> dict:
return await _request("DELETE", path)