This commit is contained in:
2026-11-06 18:09:01 +03:00
parent 0ab3a341aa
commit 8f89c7c43b
68 changed files with 4432 additions and 29 deletions
+23
View File
@@ -0,0 +1,23 @@
"""Контекст одного 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)