Replies: 1 comment
-
|
I would personally prefer having the function that implements business logic and calling it from two different endpoints with their own dependencies: @admin_router.get("/info")
async def get_user_info(user_id: Annotated[str, Path()]):
return await business_logic(user_id)
@router.get("/info")
async def get_user_info(user_id: Annotated[str, Depends(get_user)]):
return await business_logic(user_id) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
Migrating from flask and adding some new functionality.
What I'm trying to achieve:
/admin/user/{user_id}prefix that would call the same handler and return the same result./regular_apiand/admin/user/{user_id}/regular_apiuser_idfrom either theAuthorizationheader token, or from thepath(when called through admin)user_id.The token validation happens using an external resource and is different for
usertoken and for theadmintoken. Also, theuser_idof the current user is extracted differently: for the user it will come from the external resource, for admin endpoints it would come from the path.The above example seems to be working the way I expect. I'd like to get some feedback if it's the proper way to do it or there are better ways to achieve the objectives. To test it open Swagger UI at either
/docsor/admin/docsand provide anything in theAuthorizebearer token, then call the/infoAPI.Few specific questions I have are:
user_idinto the request object when processing a dependency? Or is it better to make the dependency return theuser_idand use it as a handler parameter (e.g.async def get_user_info(user: Annotated[str, Depends(auth_check)]):)domain.tld/api/infoexecute same handler asdomain.tld/api/v1/info? I tried creating a new router with different prefix, but then it shows in the Swagger. I'm just looking for it to just silently execute the same handler.Operating System
Linux
Operating System Details
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy
FastAPI Version
0.103.0
Pydantic Version
2.3.0
Python Version
3.11.4
Additional Context
This is my second day with FastAPI and with
asyncpython, so I'm still learning a lot. Right now I'm just building a simple proof of concept to make sure that FastAPI can be used to achieve certain things.Beta Was this translation helpful? Give feedback.
All reactions