""" Routes API pour l'aide et la documentation. Source unique: app/static/help.md """ from pathlib import Path from fastapi import APIRouter, Depends from fastapi.responses import JSONResponse, Response from app.core.config import settings from app.core.dependencies import verify_api_key from app.utils.help_renderer import render_help_page, get_raw_markdown from app.utils.pdf_generator import markdown_to_pdf_bytes router = APIRouter() # Chemin vers le fichier source Markdown HELP_MD_PATH = settings.base_dir / "static" / "help.md" @router.get("/content") async def get_help_content(api_key_valid: bool = Depends(verify_api_key)): """ Retourne le contenu HTML de la page d'aide généré depuis help.md. Utilisé pour le chargement dynamique de la page d'aide. """ html_content, toc_html = render_help_page(HELP_MD_PATH) return JSONResponse({ "content": html_content, "toc": toc_html }) @router.get("/documentation.md") async def download_help_markdown(api_key_valid: bool = Depends(verify_api_key)): """Télécharge la documentation d'aide en format Markdown.""" markdown_content = get_raw_markdown(HELP_MD_PATH) return Response( content=markdown_content, media_type="text/markdown; charset=utf-8", headers={ "Content-Disposition": "attachment; filename=homelab-automation-help.md" } ) @router.get("/documentation.pdf") async def download_help_pdf(api_key_valid: bool = Depends(verify_api_key)): """Télécharge la documentation d'aide en format PDF.""" markdown_content = get_raw_markdown(HELP_MD_PATH) pdf_bytes = markdown_to_pdf_bytes( markdown_content, title="Homelab Automation - Documentation" ) return Response( content=pdf_bytes, media_type="application/pdf", headers={ "Content-Disposition": "attachment; filename=homelab-automation-help.pdf" } ) @router.get("/catalog") async def get_api_catalog(api_key_valid: bool = Depends(verify_api_key)): """ Retourne un catalogue de tous les points d'entrée de l'API en format JSON. C'est une version simplifiée de la spec OpenAPI (Swagger). """ from app.routes import api_router catalog = [] for route in api_router.routes: # On ne liste que les routes qui ont un chemin et des méthodes (pas les Mount) if hasattr(route, "path") and hasattr(route, "methods"): # Exclure les routes internes ou de base si nécessaire if any(p in route.path for p in ["/openapi.json", "/docs", "/redoc"]): continue catalog.append({ "path": f"/api{route.path}" if not route.path.startswith("/api") else route.path, "methods": list(route.methods), "summary": route.summary if hasattr(route, "summary") and route.summary else route.name, "description": route.description if hasattr(route, "description") and route.description else "", "tags": list(route.tags) if hasattr(route, "tags") and route.tags else [] }) return { "title": "Homelab Automation API Catalog", "version": "2.0.0", "endpoints_count": len(catalog), "endpoints": catalog }