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.

55 lines
1.2 KiB

from dataclasses import dataclass
from typing import Annotated
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 = Annotated[
dict, Depends(get_configuration, dependency_scope="lifespan")
]
async def get_database_connection(configuration: GlobalConfiguration):
async with MyDatabaseConnection(configuration["database_url"]) as connection:
yield connection
GlobalDatabaseConnection = Annotated[
get_database_connection,
Depends(get_database_connection, dependency_scope="lifespan"),
]
@app.get("/users/{user_id}")
async def read_user(
database_connection: GlobalDatabaseConnection, user_id: Annotated[str, Path()]
):
return await database_connection.get_record("users", user_id)