signal-bot/fake_signal/routes_fixtures.py
Krzysztof kuhy Rudnicki 79dab5cc01 feat: !gekon posts a research-grade gecko photo
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-29 23:26:40 +02:00

49 lines
1.7 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
"""Local stand-ins for the image APIs, shaped like the real responses."""
from __future__ import annotations
from fastapi import APIRouter, Request
from fastapi.responses import Response
from fake_signal.state import PIXEL_PNG, State
def _image_url(request: Request) -> str:
"""Absolute URL of the fixture image, as this server sees itself."""
state: State = request.app.state.fake
return f"{state.base_url}/_fixtures/image.png"
def build_fixture_router() -> APIRouter:
"""Build the router serving fixture images and image-API responses."""
router = APIRouter(prefix="/_fixtures")
@router.get("/cat-api")
async def cat_api(request: Request) -> list[dict[str, str]]:
return [{"url": _image_url(request)}]
@router.get("/dog-api")
async def dog_api(request: Request) -> dict[str, str]:
return {"message": _image_url(request)}
@router.get("/gecko-api")
async def gecko_api(request: Request) -> dict[str, object]:
state: State = request.app.state.fake
photo = {"url": f"{state.base_url}/_fixtures/photos/square.png"}
return {"results": [{"photos": [photo]}]}
@router.get("/image.png")
async def image() -> Response:
return Response(content=PIXEL_PNG, media_type="image/png")
# Only the rewritten URL is served. iNaturalist names a square thumbnail
# and the bot is supposed to swap it for the full-size photo, so a
# regression in that rewrite must fail the run with a 404 rather than
# quietly posting a 75px image nobody looks at closely.
@router.get("/photos/large.png")
async def large_photo() -> Response:
return Response(content=PIXEL_PNG, media_type="image/png")
return router