Add redirect from /tasks to /api/v1/tasks for backward compatibility

This commit is contained in:
Automated Action 2025-05-16 06:15:11 +00:00
parent 1280fe986a
commit f8685abae0
2 changed files with 62 additions and 2 deletions

View File

@ -21,7 +21,11 @@ A RESTful API for managing tasks, built with FastAPI and SQLite.
## API Endpoints
### Task Management
### API Information
- `GET /`: Get API information and available endpoints
### Task Management (Versioned API)
- `GET /api/v1/tasks`: Get all tasks
- `POST /api/v1/tasks`: Create a new task
@ -30,7 +34,18 @@ A RESTful API for managing tasks, built with FastAPI and SQLite.
- `DELETE /api/v1/tasks/{task_id}`: Delete a task
- `POST /api/v1/tasks/{task_id}/complete`: Mark a task as completed
### Health Check
### Task Management (Legacy Unversioned API - Redirects to Versioned API)
The following endpoints are maintained for backward compatibility and will redirect to the versioned API:
- `GET /tasks`: Redirects to `/api/v1/tasks`
- `POST /tasks`: Redirects to `/api/v1/tasks`
- `GET /tasks/{task_id}`: Redirects to `/api/v1/tasks/{task_id}`
- `PUT /tasks/{task_id}`: Redirects to `/api/v1/tasks/{task_id}`
- `DELETE /tasks/{task_id}`: Redirects to `/api/v1/tasks/{task_id}`
- `POST /tasks/{task_id}/complete`: Redirects to `/api/v1/tasks/{task_id}/complete`
### Health and Diagnostic Endpoints
- `GET /health`: Application health check
- `GET /db-test`: Database connection diagnostic endpoint

45
main.py
View File

@ -47,8 +47,53 @@ async def validation_exception_handler(request: Request, exc: Exception):
content=error_detail,
)
# Include the API router with the version prefix
app.include_router(api_router, prefix=settings.API_V1_STR)
# Add support for compatibility with non-versioned endpoints
from fastapi import Request
from fastapi.responses import RedirectResponse
# Create a catch-all route for /tasks paths to redirect to versioned API
@app.get("/tasks", include_in_schema=False)
@app.post("/tasks", include_in_schema=False)
@app.get("/tasks/{task_id:path}", include_in_schema=False)
@app.put("/tasks/{task_id:path}", include_in_schema=False)
@app.delete("/tasks/{task_id:path}", include_in_schema=False)
@app.post("/tasks/{task_id:path}/complete", include_in_schema=False)
async def redirect_to_versioned_api(request: Request, task_id: str = None):
"""
Redirect unversioned API requests to the versioned API path
"""
target_url = str(request.url)
# Replace the /tasks part with /api/v1/tasks
versioned_url = target_url.replace("/tasks", f"{settings.API_V1_STR}/tasks", 1)
# Add debugging info
print(f"Redirecting from {target_url} to {versioned_url}")
# Use 307 to preserve the method and body
return RedirectResponse(url=versioned_url, status_code=307)
@app.get("/", tags=["info"])
def api_info():
"""
API information endpoint with links to documentation and versioned endpoints
"""
return {
"name": settings.PROJECT_NAME,
"version": "1.0.0",
"description": "A RESTful API for managing tasks",
"endpoints": {
"api": f"{settings.API_V1_STR}",
"tasks": f"{settings.API_V1_STR}/tasks",
"docs": "/docs",
"redoc": "/redoc",
"health": "/health",
"db_test": "/db-test"
}
}
@app.get("/health", tags=["health"])
def health_check():