"""Контекст одного HTTP-запроса: данные живут в ASGI-scope.""" from __future__ import annotations from typing import Any, MutableMapping CONTEXT_KEY = "messenger.request_context" REQUEST_ID = "request_id" USER_ID = "user_id" def context_of(scope: MutableMapping[str, Any]) -> dict[str, Any]: """Возвращает (при необходимости создавая) словарь контекста запроса.""" context = scope.get(CONTEXT_KEY) if context is None: context = {} scope[CONTEXT_KEY] = context return context def request_context(request: Any) -> dict[str, Any]: """Контекст по объекту Request/WebSocket.""" return context_of(request.scope)