-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add raw context middleware
- Loading branch information
Showing
27 changed files
with
388 additions
and
202 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
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from starlette.applications import Starlette | ||
from starlette.middleware import Middleware | ||
from starlette.requests import Request | ||
from starlette.responses import JSONResponse | ||
|
||
import uvicorn | ||
from starlette_context import context, plugins | ||
from starlette_context.middleware import RawContextMiddleware | ||
|
||
middleware = [ | ||
Middleware( | ||
RawContextMiddleware, | ||
plugins=(plugins.RequestIdPlugin(), plugins.CorrelationIdPlugin()), | ||
) | ||
] | ||
|
||
app = Starlette(debug=True, middleware=middleware) | ||
|
||
|
||
@app.route("/") | ||
async def index(request: Request): | ||
return JSONResponse(context.data) | ||
|
||
|
||
uvicorn.run(app, host="0.0.0.0") |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .context_middleware import ContextMiddleware | ||
from .raw_middleware import RawContextMiddleware |
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from contextvars import Token | ||
from typing import Optional, Sequence, Union | ||
|
||
from starlette.requests import Request, HTTPConnection | ||
from starlette.types import ASGIApp, Scope, Receive, Send, Message | ||
|
||
from starlette_context import _request_scope_context_storage | ||
from starlette_context.plugins import Plugin | ||
|
||
|
||
class RawContextMiddleware: | ||
def __init__( | ||
self, app: ASGIApp, plugins: Optional[Sequence[Plugin]] = None | ||
) -> None: | ||
self.app = app | ||
self.plugins = plugins or () | ||
if not all([isinstance(plugin, Plugin) for plugin in self.plugins]): | ||
raise TypeError("This is not a valid instance of a plugin") | ||
|
||
async def set_context( | ||
self, request: Union[Request, HTTPConnection] | ||
) -> dict: | ||
""" | ||
You might want to override this method. | ||
The dict it returns will be saved in the scope of a context. | ||
You can always do that later. | ||
""" | ||
return { | ||
plugin.key: await plugin.process_request(request) | ||
for plugin in self.plugins | ||
} | ||
|
||
@staticmethod | ||
def get_request_object( | ||
scope, receive, send | ||
) -> Union[Request, HTTPConnection]: | ||
# here we instantiate HTTPConnection instead of a Request object | ||
# because using the latter one might cause some memory problems | ||
# If you need the payload etc for your plugin instantiate Request(scope, receive, send) | ||
return HTTPConnection(scope) | ||
|
||
async def __call__( | ||
self, scope: Scope, receive: Receive, send: Send | ||
) -> None: | ||
if scope["type"] not in ("http", "websocket"): # pragma: no cover | ||
await self.app(scope, receive, send) | ||
return | ||
|
||
async def send_wrapper(message: Message) -> None: | ||
for plugin in self.plugins: | ||
await plugin.enrich_response(message) | ||
await send(message) | ||
|
||
request = self.get_request_object(scope, receive, send) | ||
|
||
_starlette_context_token: Token = _request_scope_context_storage.set( | ||
await self.set_context(request) # noqa | ||
) | ||
|
||
try: | ||
await self.app(scope, receive, send_wrapper) | ||
finally: | ||
_request_scope_context_storage.reset(_starlette_context_token) |
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,7 +1,6 @@ | ||
# flake8: noqa | ||
from .correlation_id import CorrelationIdPlugin | ||
from .date_header import DateHeaderPlugin | ||
from .forwarded_for import ForwardedForPlugin | ||
from .plugin import Plugin | ||
from .request_id import RequestIdPlugin | ||
from .user_agent import UserAgentPlugin | ||
from .base import Plugin |
Oops, something went wrong.