Add FastAPI catalog backend (games/snapshots/diff) + tests
Modular-monolith backend over SQLAlchemy (SQLite by default, Postgres-ready
via DATABASE_URL). The full snapshot.json is stored verbatim; diffing reads it
back through the ams.diff engine, so the DB never mirrors the snapshot schema.
- ams.api.db/models/schemas/service : Game 1-N Snapshot, sha256-deduped upsert
- routes: POST/GET /games, POST/GET /snapshots (import, deduped), GET /diff
(?old&new[&owner]) running compute_diff on stored snapshots, /health
- ams.api.importer : bulk CLI loader (python -m ams.api.importer --game ...)
- run: uvicorn ams.api.app:create_app --factory
11 tests pass (6 diff + 5 API via TestClient over the golden pair). Smoke-tested
live on uvicorn: import -> /snapshots -> /diff returns the BlooMoo deltas.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
0
ams/api/routes/__init__.py
Normal file
0
ams/api/routes/__init__.py
Normal file
30
ams/api/routes/diff.py
Normal file
30
ams/api/routes/diff.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ...diff import compute_diff, filter_by_owner
|
||||
from ...snapshot import Snapshot
|
||||
from .. import models
|
||||
from ..db import get_db
|
||||
|
||||
router = APIRouter(tags=["diff"])
|
||||
|
||||
|
||||
@router.get("/diff")
|
||||
def get_diff(
|
||||
old: int = Query(..., description="older snapshot id"),
|
||||
new: int = Query(..., description="newer snapshot id"),
|
||||
owner: str | None = Query(None, description="restrict to one class, e.g. CMC_Animo"),
|
||||
db: Session = Depends(get_db),
|
||||
) -> dict[str, Any]:
|
||||
a = db.get(models.Snapshot, old)
|
||||
b = db.get(models.Snapshot, new)
|
||||
if a is None or b is None:
|
||||
raise HTTPException(404, "snapshot not found")
|
||||
diff = compute_diff(Snapshot(a.data), Snapshot(b.data))
|
||||
if owner:
|
||||
diff = filter_by_owner(diff, owner)
|
||||
return diff
|
||||
37
ams/api/routes/games.py
Normal file
37
ams/api/routes/games.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from .. import models, schemas
|
||||
from ..db import get_db
|
||||
|
||||
router = APIRouter(prefix="/games", tags=["games"])
|
||||
|
||||
|
||||
@router.post("", response_model=schemas.GameOut, status_code=201)
|
||||
def create_game(body: schemas.GameCreate, db: Session = Depends(get_db)) -> models.Game:
|
||||
game = models.Game(name=body.name, notes=body.notes)
|
||||
db.add(game)
|
||||
try:
|
||||
db.commit()
|
||||
except IntegrityError:
|
||||
db.rollback()
|
||||
raise HTTPException(409, "game with that name already exists")
|
||||
db.refresh(game)
|
||||
return game
|
||||
|
||||
|
||||
@router.get("", response_model=list[schemas.GameOut])
|
||||
def list_games(db: Session = Depends(get_db)) -> list[models.Game]:
|
||||
return list(db.scalars(select(models.Game).order_by(models.Game.name)))
|
||||
|
||||
|
||||
@router.get("/{game_id}", response_model=schemas.GameDetail)
|
||||
def get_game(game_id: int, db: Session = Depends(get_db)) -> models.Game:
|
||||
game = db.get(models.Game, game_id)
|
||||
if game is None:
|
||||
raise HTTPException(404, "game not found")
|
||||
return game
|
||||
41
ams/api/routes/snapshots.py
Normal file
41
ams/api/routes/snapshots.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Body, Depends, HTTPException, Query
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from .. import models, schemas, service
|
||||
from ..db import get_db
|
||||
|
||||
router = APIRouter(prefix="/snapshots", tags=["snapshots"])
|
||||
|
||||
|
||||
@router.post("", response_model=schemas.SnapshotOut, status_code=201)
|
||||
def create_snapshot(
|
||||
data: dict[str, Any] = Body(..., description="a full engine-surface snapshot.json"),
|
||||
game: str | None = Query(None, description="link to / create this game by name"),
|
||||
db: Session = Depends(get_db),
|
||||
) -> models.Snapshot:
|
||||
if not service.looks_like_snapshot(data):
|
||||
raise HTTPException(422, "body is not an engine-surface snapshot (missing binary/types)")
|
||||
return service.import_snapshot(db, data, game)
|
||||
|
||||
|
||||
@router.get("", response_model=list[schemas.SnapshotOut])
|
||||
def list_snapshots(
|
||||
game_id: int | None = Query(None), db: Session = Depends(get_db)
|
||||
) -> list[models.Snapshot]:
|
||||
q = select(models.Snapshot)
|
||||
if game_id is not None:
|
||||
q = q.where(models.Snapshot.game_id == game_id)
|
||||
return list(db.scalars(q.order_by(models.Snapshot.id)))
|
||||
|
||||
|
||||
@router.get("/{snapshot_id}", response_model=schemas.SnapshotDetail)
|
||||
def get_snapshot(snapshot_id: int, db: Session = Depends(get_db)) -> models.Snapshot:
|
||||
snap = db.get(models.Snapshot, snapshot_id)
|
||||
if snap is None:
|
||||
raise HTTPException(404, "snapshot not found")
|
||||
return snap
|
||||
Reference in New Issue
Block a user