state["config"].username can't be accessed, I get the errror:
correct_username_bytes = state["config"].username.encode("utf8")
TypeError: 'State' object is not subscriptable
Why is that?
config = AppConfig()
@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncIterator[AppState]:
"""Lifespan event executed before the application starts handling requests."""
yield {
"config": config,
"rabbitmq": RabbitMQ(config),
}
app = FastAPI(lifespan=lifespan)
app.include_router(customers.router, dependencies=[Depends(validate_auth)])
app.include_router(mails.router, dependencies=[Depends(validate_auth)])
class AppState(TypedDict):
"""Model that represents FastApi app.state."""
config: AppConfig
rabbitmq: RabbitMQ
if TYPE_CHECKING:
from misc.app_state import AppState
security = HTTPBasic()
def validate_auth(
request: Request,
credentials: Annotated[HTTPBasicCredentials, Depends(security)],
) -> None:
"""Enforces user authentication."""
state = cast("AppState", request.state)
current_username_bytes = credentials.username.encode("utf8")
correct_username_bytes = state["config"].username.encode("utf8")