Version 0.2.0
All checks were successful
Run linters on applied template / Python 3.13 lint and build (push) Successful in 54s
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
This commit is contained in:
26
{{project_slug}}/dependencies/metrics_dep.py.jinja
Normal file
26
{{project_slug}}/dependencies/metrics_dep.py.jinja
Normal file
@@ -0,0 +1,26 @@
|
||||
"""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
|
||||
Reference in New Issue
Block a user