Skip to content

Commit

Permalink
fix: add /api/v1 prefix to all API routes (#34)
Browse files Browse the repository at this point in the history
and add a /status endpoint
  • Loading branch information
raphael0202 committed Feb 22, 2024
1 parent dbfad96 commit fff9add
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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 {
Expand All @@ -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:
Expand All @@ -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)

0 comments on commit fff9add

Please sign in to comment.