Files
2026-06-14 15:18:56 -04:00

38 lines
1.2 KiB
Python

"""FastAPI routes for the Hermes digital pet dashboard plugin."""
from __future__ import annotations
import importlib.util
import sys
from pathlib import Path
from typing import Any, Dict
from fastapi import APIRouter
from pydantic import BaseModel, Field
_PLUGIN_ROOT = Path(__file__).resolve().parents[1]
_STATE_PATH = _PLUGIN_ROOT / "state.py"
_SPEC = importlib.util.spec_from_file_location("hermes_digital_pet_state", _STATE_PATH)
if _SPEC is None or _SPEC.loader is None: # pragma: no cover
raise RuntimeError("Unable to load digital pet state module")
_state_mod = importlib.util.module_from_spec(_SPEC)
sys.modules.setdefault("hermes_digital_pet_state", _state_mod)
_SPEC.loader.exec_module(_state_mod)
router = APIRouter()
class PetEvent(BaseModel):
type: str = Field(..., min_length=1, max_length=64)
metadata: Dict[str, Any] = Field(default_factory=dict)
@router.get("/state")
async def get_state():
return {"ok": True, "state": _state_mod.load_state().public()}
@router.post("/event")
async def post_event(event: PetEvent):
state = _state_mod.apply_event(event.type, metadata=event.metadata, persist=True)
return {"ok": True, "state": state.public()}