Last active
March 3, 2023 09:53
-
-
Save imankulov/6e73806b017f28c34fdee60ac56ece90 to your computer and use it in GitHub Desktop.
FastAPI service with and without dependencies
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
""" | |
A simple contrived example of a FastAPI application that heavily uses | |
the FastAPI dependency injection system. | |
Sample request http://127.0.0.1:9876/tasks?user_id=123456 | |
returns [{"id":1,"name":"Task 1","user":{"id":123456,"name":"John Doe"}}] | |
Dependency tree: | |
get_me() | |
current_user() | |
Cache.get_user_by_id() | |
UserService.get_user_by_id() | |
get_tasks() | |
user_tasks() | |
current_user() | |
Cache.get_user_by_id() | |
UserService.get_user_by_id() | |
""" | |
import fastapi | |
from fastapi import Depends | |
from pydantic import BaseModel | |
api = fastapi.FastAPI() | |
class User(BaseModel): | |
id: int | |
name: str | |
class Task(BaseModel): | |
id: int | |
name: str | |
user: User | |
class UserService: | |
def get_user_by_id(self, user_id: int) -> User | None: | |
return User(id=user_id, name="John Doe") | |
class Cache: | |
def get_user_by_id(self, user_id: int) -> User | None: | |
return None | |
def current_user( | |
user_id: int, | |
user_service: UserService = Depends(UserService), | |
cache: Cache = Depends(Cache), | |
): | |
user = cache.get_user_by_id(user_id) | |
if user is None: | |
user = user_service.get_user_by_id(user_id) | |
return user | |
def user_tasks(user: User = Depends(current_user)): | |
return [Task(id=1, name="Task 1", user=user)] | |
@api.get("/me") | |
def get_me(user: User = Depends(current_user)): | |
return user | |
@api.get("/tasks") | |
def get_tasks(tasks: list[Task] = Depends(user_tasks)): | |
return tasks | |
if __name__ == "__main__": | |
import uvicorn | |
uvicorn.run("fastapi_dependencies:api", host="127.0.0.1", port=9876, reload=True) |
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
""" | |
A simple contrived example of a FastAPI application that doesn't use depoendencies. | |
Sample request http://127.0.0.1:9876/tasks?user_id=123456 | |
returns [{"id":1,"name":"Task 1","user":{"id":123456,"name":"John Doe"}}] | |
""" | |
import fastapi | |
from pydantic import BaseModel | |
api = fastapi.FastAPI() | |
class User(BaseModel): | |
id: int | |
name: str | |
class Task(BaseModel): | |
id: int | |
name: str | |
user: User | |
class UserService: | |
def get_user_by_id(self, user_id: int) -> User | None: | |
return User(id=user_id, name="John Doe") | |
class Cache: | |
def get_user_by_id(self, user_id: int) -> User | None: | |
return None | |
def current_user( | |
user_id: int, | |
user_service: UserService, | |
cache: Cache, | |
): | |
user = cache.get_user_by_id(user_id) | |
if user is None: | |
user = user_service.get_user_by_id(user_id) | |
return user | |
def user_tasks(user: User): | |
return [Task(id=1, name="Task 1", user=user)] | |
@api.get("/me") | |
def get_me(user_id: int): | |
user_service = UserService() | |
cache = Cache() | |
user = current_user(user_id, user_service, cache) | |
return user | |
@api.get("/tasks") | |
def get_tasks(user_id: int): | |
user_service = UserService() | |
cache = Cache() | |
user = current_user(user_id, user_service, cache) | |
tasks = user_tasks(user) | |
return tasks | |
if __name__ == "__main__": | |
import uvicorn | |
uvicorn.run("fastapi_no_dependencies:api", host="127.0.0.1", port=9875, reload=True) |
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
""" | |
A simple contrived example of a FastAPI application that doesn't use depoendencies. | |
Sample request http://127.0.0.1:9876/tasks?user_id=123456 | |
returns [{"id":1,"name":"Task 1","user":{"id":123456,"name":"John Doe"}}] | |
""" | |
import fastapi | |
from pydantic import BaseModel | |
api = fastapi.FastAPI() | |
class User(BaseModel): | |
id: int | |
name: str | |
class Task(BaseModel): | |
id: int | |
name: str | |
user: User | |
class UserService: | |
def get_user_by_id(self, user_id: int) -> User | None: | |
return User(id=user_id, name="John Doe") | |
class Cache: | |
def get_user_by_id(self, user_id: int) -> User | None: | |
return None | |
cache = Cache() | |
user_service = UserService() | |
def current_user(user_id: int): | |
user = cache.get_user_by_id(user_id) | |
if user is None: | |
user = user_service.get_user_by_id(user_id) | |
return user | |
def user_tasks(user_id: int): | |
user = current_user(user_id) | |
return [Task(id=1, name="Task 1", user=user)] | |
@api.get("/me") | |
def get_me(user_id: int): | |
user = current_user(user_id) | |
return user | |
@api.get("/tasks") | |
def get_tasks(user_id: int): | |
tasks = user_tasks(user_id) | |
return tasks | |
if __name__ == "__main__": | |
import uvicorn | |
uvicorn.run("fastapi_no_dependencies:api", host="127.0.0.1", port=9874, reload=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment