-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
268 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.12.7" | ||
__version__ = "0.12.8" |
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,31 +1,37 @@ | ||
from __future__ import annotations | ||
|
||
import traceback | ||
from contextlib import AsyncExitStack | ||
|
||
from lilya.protocols.middleware import MiddlewareProtocol | ||
from lilya.types import ASGIApp, Receive, Scope, Send | ||
|
||
|
||
class AsyncExitStackMiddleware(MiddlewareProtocol): | ||
def __init__(self, app: ASGIApp): | ||
def __init__(self, app: ASGIApp, debug: bool = False) -> None: | ||
"""AsyncExitStack Middleware class. | ||
Args: | ||
app: The 'next' ASGI app to call. | ||
""" | ||
super().__init__(app) | ||
self.app = app | ||
self.debug = debug | ||
|
||
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: | ||
if not AsyncExitStack: | ||
await self.app(scope, receive, send) # pragma: no cover | ||
|
||
exception: Exception | None = None | ||
async with AsyncExitStack() as stack: | ||
scope["lilya_astack"] = stack | ||
scope["lilya_asyncexitstack"] = stack | ||
try: | ||
await self.app(scope, receive, send) | ||
except Exception as e: | ||
exception = e | ||
|
||
if exception and self.debug: | ||
traceback.print_exception(exception, exception, exception.__traceback__) # type: ignore | ||
|
||
if exception: | ||
raise exception |
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 |
---|---|---|
|
@@ -96,6 +96,7 @@ testing = [ | |
"ptpython", | ||
"ipdb", | ||
"pdbpp", | ||
"structlog", | ||
] | ||
|
||
docs = [ | ||
|
Empty file.
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,76 @@ | ||
from structlog import get_logger | ||
|
||
from lilya.responses import PlainText | ||
from lilya.routing import Include, Path | ||
from lilya.testclient import create_client | ||
|
||
logger = get_logger() | ||
|
||
|
||
async def before_path_request(scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
logger.info(f"Before path request: {app.state.app_request}") | ||
|
||
|
||
async def after_path_request(scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
|
||
logger.info(f"After path request: {app.state.app_request}") | ||
|
||
|
||
async def before_include_request(scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
logger.info(f"Before include request: {app.state.app_request}") | ||
|
||
|
||
async def after_include_request(scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
|
||
logger.info(f"After include request: {app.state.app_request}") | ||
|
||
|
||
async def before_app_request(scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request = 1 | ||
logger.info(f"Before app request: {app.state.app_request}") | ||
|
||
|
||
async def after_app_request(scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
|
||
logger.info(f"After app request: {app.state.app_request}") | ||
|
||
|
||
def test_all_layers_request(): | ||
async def index(request): | ||
state = request.app.state | ||
return PlainText(f"State: {state.app_request}") | ||
|
||
with create_client( | ||
routes=[ | ||
Include( | ||
"/", | ||
routes=[ | ||
Path( | ||
"/", | ||
index, | ||
before_request=[before_path_request], | ||
after_request=[after_path_request], | ||
) | ||
], | ||
before_request=[before_include_request], | ||
after_request=[after_include_request], | ||
), | ||
], | ||
before_request=[before_app_request], | ||
after_request=[after_app_request], | ||
) as client: | ||
response = client.get("/") | ||
|
||
assert response.status_code == 200 | ||
assert response.text == "State: 3" |
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,107 @@ | ||
from structlog import get_logger | ||
|
||
from lilya.responses import PlainText | ||
from lilya.routing import Include, Path | ||
from lilya.testclient import create_client | ||
|
||
logger = get_logger() | ||
|
||
|
||
async def before_path_request(scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request = 1 | ||
logger.info(f"Before path request: {app.state.app_request}") | ||
|
||
|
||
async def after_path_request(scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
|
||
logger.info(f"After path request: {app.state.app_request}") | ||
|
||
|
||
def test_path_before_request(): | ||
async def index(request): | ||
state = request.app.state | ||
return PlainText(f"State: {state.app_request}") | ||
|
||
with create_client( | ||
routes=[ | ||
Path( | ||
"/", | ||
index, | ||
before_request=[before_path_request], | ||
after_request=[after_path_request], | ||
) | ||
], | ||
) as client: | ||
response = client.get("/") | ||
|
||
assert response.status_code == 200 | ||
assert response.text == "State: 1" | ||
|
||
|
||
async def before_include_request(scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request = 1 | ||
logger.info(f"Before include request: {app.state.app_request}") | ||
|
||
|
||
async def after_include_request(scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
|
||
logger.info(f"After include request: {app.state.app_request}") | ||
|
||
|
||
def test_include_before_request(): | ||
async def index(request): | ||
state = request.app.state | ||
return PlainText(f"State: {state.app_request}") | ||
|
||
with create_client( | ||
routes=[ | ||
Include( | ||
"/", | ||
Path( | ||
"/", | ||
index, | ||
), | ||
before_request=[before_include_request], | ||
after_request=[after_include_request], | ||
) | ||
], | ||
) as client: | ||
response = client.get("/") | ||
|
||
assert response.status_code == 200 | ||
assert response.text == "State: 1" | ||
|
||
|
||
async def before_app_request(scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request = 1 | ||
logger.info(f"Before app request: {app.state.app_request}") | ||
|
||
|
||
async def after_app_request(scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
|
||
logger.info(f"After app request: {app.state.app_request}") | ||
|
||
|
||
def test_app_before_request(): | ||
async def index(request): | ||
state = request.app.state | ||
return PlainText(f"State: {state.app_request}") | ||
|
||
with create_client( | ||
routes=[Path("/", index)], | ||
before_request=[before_app_request], | ||
after_request=[after_app_request], | ||
) as client: | ||
response = client.get("/") | ||
|
||
assert response.status_code == 200 | ||
assert response.text == "State: 1" |