From fff9add510836d8833e6a11b5d063d2c892ebd83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Bournhonesque?= Date: Thu, 22 Feb 2024 16:59:08 +0100 Subject: [PATCH] fix: add /api/v1 prefix to all API routes (#34) and add a /status endpoint --- app/api.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/app/api.py b/app/api.py index cc5f900..609e0c4 100644 --- a/app/api.py +++ b/app/api.py @@ -4,7 +4,7 @@ from pathlib import Path from typing import Any -from fastapi import FastAPI, HTTPException, Request +from fastapi import APIRouter, FastAPI, HTTPException, Request from fastapi.responses import HTMLResponse, PlainTextResponse from fastapi.templating import Jinja2Templates from openfoodfacts import Flavor @@ -32,6 +32,7 @@ "url": "https://www.gnu.org/licenses/agpl-3.0.en.html", }, ) +api_v1_router = APIRouter(prefix="/api/v1") templates = Jinja2Templates(directory=Path(__file__).parent / "templates") init_sentry(settings.sentry_dns) @@ -211,7 +212,7 @@ class Flag(FlagCreate): # Create a flag (one to one relationship) -@app.post("/flags") +@api_v1_router.post("/flags") def create_flag(flag: FlagCreate, request: Request): with db: # Check if the flag already exists @@ -261,14 +262,14 @@ def create_flag(flag: FlagCreate, request: Request): # Get all flags (one to many relationship) -@app.get("/flags") +@api_v1_router.get("/flags") def get_flags(): with db: return {"flags": list(FlagModel.select().dicts().iterator())} # Get flag by ID (one to one relationship) -@app.get("/flags/{flag_id}") +@api_v1_router.get("/flags/{flag_id}") def get_flag(flag_id: int): with db: try: @@ -282,21 +283,21 @@ def _create_ticket(ticket: TicketCreate): # Create a ticket (one to one relationship) -@app.post("/tickets") +@api_v1_router.post("/tickets") def create_ticket(ticket: TicketCreate) -> Ticket: with db: return _create_ticket(ticket) # Get all tickets (one to many relationship) -@app.get("/tickets") +@api_v1_router.get("/tickets") def get_tickets(): with db: return {"tickets": list(TicketModel.select().dicts().iterator())} # Get ticket by id (one to one relationship) -@app.get("/tickets/{ticket_id}") +@api_v1_router.get("/tickets/{ticket_id}") def get_ticket(ticket_id: int): with db: try: @@ -306,7 +307,7 @@ def get_ticket(ticket_id: int): # Get all flags for a ticket by id (one to many relationship) -@app.get("/tickets/{ticket_id}/flags") +@api_v1_router.get("/tickets/{ticket_id}/flags") def get_flags_by_ticket(ticket_id: int): with db: return { @@ -320,7 +321,7 @@ def get_flags_by_ticket(ticket_id: int): # Update ticket status by id with enum : open, closed (soft delete) -@app.put("/tickets/{ticket_id}/status") +@api_v1_router.put("/tickets/{ticket_id}/status") def update_ticket_status(ticket_id: int, status: TicketStatus): with db: try: @@ -330,3 +331,12 @@ def update_ticket_status(ticket_id: int, status: TicketStatus): return model_to_dict(ticket) except DoesNotExist: raise HTTPException(status_code=404, detail="Not found") + + +@api_v1_router.get("/status") +def status(): + """Health check endpoint.""" + return {"status": "ok"} + + +app.include_router(api_v1_router)