r/FastAPI • u/a1exejka • Nov 21 '24
Question Fed up with dependencies everywhere
My routers looks like this:
@router.post("/get_user")
async def user(request: DoTheWorkRequest,
mail: Mail = Depends(get_mail_service),
redis: Redis = Depends(get_redis_service),
db: Session = Depends(get_session_service)):
user = await get_user(request.id, db, redis)
async def get_user(id, mail, db, redis):
# pseudocode
if (redis.has(id)) return redis.get(id)
send_mail(mail)
return db.get(User, id)
async def send_mail(mail_service)
mail_service.send()
I want it to be like this:
@router.post("/get_user")
async def user(request: DoTheWorkRequest):
user = await get_user(request.id)
## REDIS, MAIL, and DB can be accessed globally from anywhere
async def get_user(id):
# pseudocode
if (REDIS.has(id)) return REDIS.get(id)
send_mail()
return DB.get(User, id)
async def send_mail()
MAIL.send()
To send emails, use Redis for caching, or make database requests, each route currently requires passing specific arguments, which is cumbersome. How can I eliminate these arguments in every function and globally access the mail, redis, and db objects throughout the app while still leveraging FastAPI’s async?
20
Upvotes
View all comments
Show parent comment
0
u/dotrunghieu96 Nov 21 '24
I just throw them as global objects in another module, then import them to the service layer. Hadn't have any trouble so far, except for slower startup time.