All checks were successful
Run linters on applied template / Python 3.13 lint and build (push) Successful in 54s
Changes: - add metrics dispencer - add basic authentication dependency - enable GZIP middleware - add !env() example to deploy section - update dependencies state attribute name
27 lines
987 B
Django/Jinja
27 lines
987 B
Django/Jinja
"""Metrics dependency functions are defined here."""
|
|
|
|
from fastapi import FastAPI, Request
|
|
|
|
from {{project_slug}}.observability.metrics import Metrics
|
|
|
|
|
|
def init_dispencer(app: FastAPI, connection_manager: Metrics) -> None:
|
|
"""Initialize Metrics dispencer at app's state."""
|
|
if hasattr(app.state, "metrics_dep"):
|
|
if not isinstance(app.state.metrics_dep, Metrics):
|
|
raise ValueError(
|
|
"metrics_dep attribute of app's state is already set" f"with other value ({app.state.metrics_dep})"
|
|
)
|
|
return
|
|
|
|
app.state.metrics_dep = connection_manager
|
|
|
|
|
|
def obtain(app_or_request: FastAPI | Request) -> Metrics:
|
|
"""Get a Metrics from request's app state."""
|
|
if isinstance(app_or_request, Request):
|
|
app_or_request = app_or_request.app
|
|
if not hasattr(app_or_request.state, "metrics_dep"):
|
|
raise ValueError("Metrics dispencer was not initialized at app preparation")
|
|
return app_or_request.state.metrics_dep
|