-
-
Notifications
You must be signed in to change notification settings - Fork 162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: How to add function to route #136
Comments
I don't think there is any official support for this. I have worked around this in the following way. from typing import List, Optional
import uvicorn
from fastapi import FastAPI
from fastapi_crudrouter import MemoryCRUDRouter as CRUDRouter
from pydantic import BaseModel
class Potato(BaseModel):
id: int
color: str
mass: float
app = FastAPI()
router = CRUDRouter(
schema=Potato,
get_all_route=True,
get_one_route=False,
create_route=True,
update_route=False,
delete_one_route=False,
delete_all_route=False,
)
for route in router.routes:
if 'GET' in route.methods and route.path == '/potato':
get_all_callback = route.endpoint
# Delete the existing APIRoute.
router.get("")
def override_get_all(pagination = router.pagination):
print("override")
return get_all_callback(pagination)
router._add_api_route(
"",
override_get_all,
methods=["GET"],
response_model=Optional[List[router.schema]],
summary="Get All",
dependencies=True,
)
app.include_router(router)
if __name__ == '__main__':
uvicorn.run(app, port=9999) For overriding, I think we need a formal implementation that can wrap the existing implementation. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Following the Overriding Example, we can easily override the default router:
However, I would like to use the default put router (to update) but also add function to that router (e.g. notify user / write a log etc.) , may i know if any hints to do so ?
The text was updated successfully, but these errors were encountered: