You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.1 KiB

from dataclasses import dataclass
from fastapi import Depends, FastAPI, Path
from typing_extensions import Self
@dataclass
class MyDatabaseConnection:
"""
This is a mock just for example purposes.
"""
connection_string: str
async def __aenter__(self) -> Self:
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
pass
async def get_record(self, table_name: str, record_id: str) -> dict:
pass
app = FastAPI()
async def get_configuration() -> dict:
return {
"database_url": "sqlite:///database.db",
}
GlobalConfiguration = Depends(get_configuration, dependency_scope="lifespan")
async def get_database_connection(configuration: dict = GlobalConfiguration):
async with MyDatabaseConnection(configuration["database_url"]) as connection:
yield connection
GlobalDatabaseConnection = Depends(get_database_connection, dependency_scope="lifespan")
@app.get("/users/{user_id}")
async def read_user(
database_connection: MyDatabaseConnection = GlobalDatabaseConnection,
user_id: str = Path(),
):
return await database_connection.get_record("users", user_id)