Files
home-assistant-core/homeassistant/components/http/request_context.py
T
epenetGitHubcopilot-swe-agent[bot] <[email protected]>frenck
d766aae436 Remove import annotations from components (#169536)
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: frenck <[email protected]>
2026-04-30 21:14:48 +02:00

27 lines
809 B
Python

"""Middleware to set the request context."""
from collections.abc import Awaitable, Callable
from contextvars import ContextVar
from aiohttp.web import Application, Request, StreamResponse, middleware
from homeassistant.core import callback
from homeassistant.helpers.http import current_request # noqa: F401
@callback
def setup_request_context(
app: Application, context: ContextVar[Request | None]
) -> None:
"""Create request context middleware for the app."""
@middleware
async def request_context_middleware(
request: Request, handler: Callable[[Request], Awaitable[StreamResponse]]
) -> StreamResponse:
"""Request context middleware."""
context.set(request)
return await handler(request)
app.middlewares.append(request_context_middleware)