-
-
Notifications
You must be signed in to change notification settings - Fork 761
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR contains 2 main changes: - Expose additional context. We now expose the scope, operation, connexion context, and receive channel as context aware globals. This makes them available to the decorators independent of the framework in between. The user will also be able to use these. I also implemented a `TestContext` class which can be used to populate the context during testing. It's minimal, but can be extended towards the future. - Rename the decorators to be framework specific. This is part of a bigger change for which I'll submit a follow up PR. I was working on this first when it became clear that the context would need to be extended, which is why this is already included.
- Loading branch information
1 parent
f064fd0
commit 2bf18f6
Showing
19 changed files
with
302 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,24 @@ | ||
from contextvars import ContextVar | ||
|
||
from starlette.types import Scope | ||
from starlette.types import Receive, Scope | ||
from werkzeug.local import LocalProxy | ||
|
||
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." | ||
) | ||
|
||
_scope: ContextVar[Scope] = ContextVar("SCOPE") | ||
|
||
_context: ContextVar[dict] = ContextVar("CONTEXT") | ||
context = LocalProxy(_context, unbound_message=UNBOUND_MESSAGE) | ||
|
||
def __getattr__(name): | ||
if name == "scope": | ||
return _scope.get() | ||
if name == "context": | ||
return _scope.get().get("extensions", {}).get("connexion_context", {}) | ||
_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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
""" | ||
This module defines decorators which Connexion uses to wrap user provided view functions. | ||
""" | ||
from .main import AsyncDecorator, SyncDecorator # noqa | ||
from .main import FlaskDecorator, StarletteDecorator # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.