https://github.com/sayanarijit/fastapi-magic-router
🪄 FastAPI router with magical powers ✨
https://github.com/sayanarijit/fastapi-magic-router
Last synced: 3 months ago
JSON representation
🪄 FastAPI router with magical powers ✨
- Host: GitHub
- URL: https://github.com/sayanarijit/fastapi-magic-router
- Owner: sayanarijit
- License: mit
- Created: 2022-09-09T12:18:17.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-12-14T15:07:44.000Z (almost 2 years ago)
- Last Synced: 2025-03-13T23:47:26.180Z (7 months ago)
- Language: Python
- Homepage:
- Size: 25.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🪄 FastAPI Magic Router ✨
Have you come here seeking a magical way to define FastAPI routes?
```python
app = FastAPI()
route = magic_router(app)route("GET /api/users ", user.list_)
route("GET /api/users/{user_id}", user.get)
route("POST /api/users ", user.create)
route("PATCH /api/users/{user_id}", user.update)
route("DELETE /api/users/{user_id}", user.delete_)
```Come, I shall grant you your wish!
### Default Router vs Magic Router
```python
from fastapi import FastAPI
from pydantic import BaseModel
from magic_router import magic_router, magicclass Response(BaseModel):
path: str# Default Router -----------------------------------------------------------------------
app = FastAPI()
not_so_magical_path = "/api/not-so-magical"async def not_so_magical_endpoint():
return Response(path=not_so_magical_path)app.get(
not_so_magical_path,
response_model=Response,
tags=["main"],
operation_id="not_so_magical_endpoint",
name="not_so_magical_endpoint",
)(not_so_magical_endpoint)# Magic Router -------------------------------------------------------------------------
route = magic_router(app)
async def magical_endpoint() -> Response:
return Response(path=magic(magical_endpoint).path)route("GET /api/magical", magical_endpoint)
# --------------------------------------------------------------------------------------
```