import calendar from datetime import date from typing import Any from fastapi import APIRouter, HTTPException, Query router = APIRouter(tags=["lunaire"]) # Cache en mémoire : {mois_str: list[dict]} _CACHE: dict[str, list[dict]] = {} @router.get("/lunar") def get_lunar( month: str = Query(..., description="Format YYYY-MM"), ) -> list[dict[str, Any]]: if month in _CACHE: return _CACHE[month] try: year, mon = int(month[:4]), int(month[5:7]) except (ValueError, IndexError): raise HTTPException(400, "Format attendu : YYYY-MM") last_day = calendar.monthrange(year, mon)[1] start = date(year, mon, 1) end = date(year, mon, last_day) try: from app.services.lunar import build_calendar from dataclasses import asdict result = [asdict(d) for d in build_calendar(start, end)] except ImportError: raise HTTPException(503, "Service lunaire non disponible (skyfield non installé)") except Exception as e: raise HTTPException(500, f"Erreur calcul lunaire : {e}") _CACHE[month] = result return result