-
-
Notifications
You must be signed in to change notification settings - Fork 769
/
context.py
29 lines (20 loc) · 991 Bytes
/
context.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from contextvars import ContextVar
from starlette.types import Receive, Scope
from werkzeug.local import LocalProxy
from connexion.lifecycle import ConnexionRequest
from connexion.operations import AbstractOperation
UNBOUND_MESSAGE = (
"Working outside of operation context. Make sure your app is wrapped in a "
"ContextMiddleware and you're processing a request while accessing the context."
)
_context: ContextVar[dict] = ContextVar("CONTEXT")
context = LocalProxy(_context, unbound_message=UNBOUND_MESSAGE)
_operation: ContextVar[AbstractOperation] = ContextVar("OPERATION")
operation = LocalProxy(_operation, unbound_message=UNBOUND_MESSAGE)
_receive: ContextVar[Receive] = ContextVar("RECEIVE")
receive = LocalProxy(_receive, unbound_message=UNBOUND_MESSAGE)
_scope: ContextVar[Scope] = ContextVar("SCOPE")
scope = LocalProxy(_scope, unbound_message=UNBOUND_MESSAGE)
request = LocalProxy(
lambda: ConnexionRequest(scope, receive), unbound_message=UNBOUND_MESSAGE
)