Files
romm/backend/endpoints/tasks.py
Michael Manganiello 8abbae4c02 misc: Make backend handle URLs with trailing slash
According to multiple FastAPI discussions [1], FastAPI only includes a
built-in mechanism to redirect requests including a trailing slash, to
its variation without slash, using a `307` status code.

This can be an issue when certain clients do not send the same headers
on the redirected request.

This change adds a custom FastAPI `APIRouter`, that registers both route
path variations (with and without trailing slash), while only marking
the path without slash for being included in the OpenAPI schema.

[1] https://github.com/fastapi/fastapi/discussions/7298
2024-08-07 00:22:21 -03:00

38 lines
1.0 KiB
Python

from decorators.auth import protected_route
from endpoints.responses import MessageResponse
from fastapi import Request
from tasks.update_switch_titledb import update_switch_titledb_task
from utils.router import APIRouter
router = APIRouter()
@protected_route(router.post, "/tasks/run", ["tasks.run"])
async def run_tasks(request: Request) -> MessageResponse:
"""Run all tasks endpoint
Args:
request (Request): Fastapi Request object
Returns:
RunTasksResponse: Standard message response
"""
await update_switch_titledb_task.run()
return {"msg": "All tasks ran successfully!"}
@protected_route(router.post, "/tasks/{task}/run", ["tasks.run"])
async def run_task(request: Request, task: str) -> MessageResponse:
"""Run all tasks endpoint
Args:
request (Request): Fastapi Request object
Returns:
RunTasksResponse: Standard message response
"""
tasks = {"switch_titledb": update_switch_titledb_task}
await tasks[task].run()
return {"msg": f"Task {task} run successfully!"}