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