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:
34
{{project_slug}}/dependencies/auth_dep.py
Normal file
34
{{project_slug}}/dependencies/auth_dep.py
Normal file
@@ -0,0 +1,34 @@
|
||||
"""Authentication dependency function is defined here."""
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
import jwt
|
||||
from fastapi import Request
|
||||
|
||||
from . import logger_dep
|
||||
|
||||
|
||||
@dataclass
|
||||
class AuthenticationData:
|
||||
api_key: str | None
|
||||
jwt_payload: dict | None
|
||||
jwt_original: str | None
|
||||
|
||||
|
||||
def obtain(request: Request) -> AuthenticationData:
|
||||
if hasattr(request.state, "auth_dep"):
|
||||
return request.state.auth_dep
|
||||
auth = AuthenticationData(None, None, None)
|
||||
if (value := request.headers.get("X-API-Key")) is not None:
|
||||
auth.api_key = value
|
||||
if (value := request.headers.get("Authorization")) is not None and value.startswith("Bearer "):
|
||||
value = value[7:]
|
||||
auth.jwt_original = value
|
||||
try:
|
||||
auth.jwt_payload = jwt.decode(value, algorithms=["HS256"], options={"verify_signature": False})
|
||||
except Exception: # pylint: disable=broad-except
|
||||
logger = logger_dep.obtain(request)
|
||||
logger.warning("failed to parse Authorization header as jwt", value=value)
|
||||
logger.debug("failed to parse Authorization header as jwt", exc_info=True)
|
||||
request.state.auth_dep = auth
|
||||
return auth
|
||||
@@ -7,19 +7,21 @@ from {{project_slug}}.db.connection.manager import PostgresConnectionManager
|
||||
|
||||
def init_dispencer(app: FastAPI, connection_manager: PostgresConnectionManager) -> None:
|
||||
"""Initialize PostgresConnectionManager dispencer at app's state."""
|
||||
if hasattr(app.state, "postgres_connection_manager"):
|
||||
if not isinstance(app.state.postgres_connection_manager, PostgresConnectionManager):
|
||||
if hasattr(app.state, "postgres_connection_manager_dep"):
|
||||
if not isinstance(app.state.postgres_connection_manager_dep, PostgresConnectionManager):
|
||||
raise ValueError(
|
||||
"postgres_connection_manager attribute of app's state is already set"
|
||||
f"with other value ({app.state.postgres_connection_manager})"
|
||||
"postgres_connection_manager_dep attribute of app's state is already set"
|
||||
f"with other value ({app.state.postgres_connection_manager_dep})"
|
||||
)
|
||||
return
|
||||
|
||||
app.state.postgres_connection_manager = connection_manager
|
||||
app.state.postgres_connection_manager_dep = connection_manager
|
||||
|
||||
|
||||
def obtain(request: Request) -> PostgresConnectionManager:
|
||||
def obtain(app_or_request: FastAPI | Request) -> PostgresConnectionManager:
|
||||
"""Get a PostgresConnectionManager from request's app state."""
|
||||
if not hasattr(request.app.state, "postgres_connection_manager"):
|
||||
if isinstance(app_or_request, Request):
|
||||
app_or_request = app_or_request.app
|
||||
if not hasattr(app_or_request.state, "postgres_connection_manager_dep"):
|
||||
raise ValueError("PostgresConnectionManager dispencer was not initialized at app preparation")
|
||||
return request.app.state.postgres_connection_manager
|
||||
return app_or_request.state.postgres_connection_manager_dep
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
"""PostgresConnectionManager dependency functions are defined here."""
|
||||
"""structlog BoundLogger dependency functions are defined here."""
|
||||
|
||||
from fastapi import FastAPI, Request
|
||||
from structlog.stdlib import BoundLogger
|
||||
@@ -7,27 +7,31 @@ from structlog.stdlib import BoundLogger
|
||||
def init_dispencer(app: FastAPI, logger: BoundLogger) -> None:
|
||||
"""Initialize BoundLogger dispencer at app's state."""
|
||||
if hasattr(app.state, "logger"):
|
||||
if not isinstance(app.state.logger, BoundLogger):
|
||||
raise ValueError("logger attribute of app's state is already set" f"with other value ({app.state.logger})")
|
||||
if not isinstance(app.state.logger_dep, BoundLogger):
|
||||
raise ValueError(
|
||||
"logger attribute of app's state is already set" f"with other value ({app.state.logger_dep})"
|
||||
)
|
||||
return
|
||||
|
||||
app.state.logger = logger
|
||||
app.state.logger_dep = logger
|
||||
|
||||
|
||||
def attach_to_request(request: Request, logger: BoundLogger) -> None:
|
||||
"""Set logger for a concrete request. If request had already had a logger, replace it."""
|
||||
if hasattr(request.state, "logger"):
|
||||
if not isinstance(request.state.logger, BoundLogger):
|
||||
logger.warning("request.state.logger is already set with other value", value=request.state.logger)
|
||||
request.state.logger = logger
|
||||
if hasattr(request.state, "logger_dep"):
|
||||
if not isinstance(request.state.logger_dep, BoundLogger):
|
||||
logger.warning("request.state.logger is already set with other value", value=request.state.logger_dep)
|
||||
request.state.logger_dep = logger
|
||||
|
||||
|
||||
def obtain(request: Request) -> BoundLogger:
|
||||
def obtain(app_or_request: FastAPI | Request) -> BoundLogger:
|
||||
"""Get a logger from request or app state."""
|
||||
if hasattr(request.state, "logger"):
|
||||
logger = request.state.logger
|
||||
if isinstance(logger, BoundLogger):
|
||||
return logger
|
||||
if not hasattr(request.app.state, "logger"):
|
||||
if isinstance(app_or_request, Request):
|
||||
if hasattr(app_or_request.state, "logger_dep"):
|
||||
logger = app_or_request.state.logger_dep
|
||||
if isinstance(logger, BoundLogger):
|
||||
return logger
|
||||
app_or_request = app_or_request.app
|
||||
if not hasattr(app_or_request.state, "logger_dep"):
|
||||
raise ValueError("BoundLogger dispencer was not initialized at app preparation")
|
||||
return request.app.state.logger
|
||||
return app_or_request.state.logger_dep
|
||||
|
||||
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