Created
March 29, 2022 07:53
-
-
Save Tishka17/c07bdc8954e34540dae24ee36f8659db to your computer and use it in GitHub Desktop.
Fastapi Depends example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import uuid | |
| from fastapi import FastAPI, Depends | |
| app = FastAPI() | |
| class Session: | |
| pass | |
| class R1: | |
| pass | |
| class R2: | |
| pass | |
| @app.get("/") | |
| async def root(r1: R1 = Depends(), r2: R2 = Depends()): | |
| return {"message": "Hello World"} | |
| def new_r1(s: Session = Depends()): | |
| print("r1", s) | |
| def new_r2(s: Session = Depends()): | |
| print("r2", s) | |
| def new_s(): | |
| return uuid.uuid4() | |
| app.dependency_overrides[Session] = new_s | |
| app.dependency_overrides[R1] = new_r1 | |
| app.dependency_overrides[R2] = new_r2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment