Skip to content

Commit e222f73

Browse files
authored
Async plugins (#6)
change context parent (UserDict), add support for fully async plugins and middleware
1 parent b4a85f3 commit e222f73

34 files changed

+294
-160
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ app.add_middleware(ContextMiddleware.with_plugins( # easily extensible
5151

5252
@app.route('/')
5353
async def index(request: Request):
54-
return JSONResponse(context.dict())
54+
return JSONResponse(context.data)
5555

5656

5757
uvicorn.run(app, host="0.0.0.0")

examples/example_with_exception_handling/app.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
from starlette.applications import Starlette
44
from starlette.exceptions import HTTPException
5-
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
5+
from starlette.middleware.base import (
6+
BaseHTTPMiddleware,
7+
RequestResponseEndpoint,
8+
)
69
from starlette.requests import Request
710
from starlette.responses import JSONResponse, Response
811
from starlette.status import HTTP_500_INTERNAL_SERVER_ERROR

examples/example_with_exception_handling/logger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self, logger, extra=None):
2323

2424
def process(self, msg, kwargs):
2525
extra = self.extra.copy()
26-
extra.update(context.dict())
26+
extra.update(context.data)
2727

2828
kwargs["extra"] = extra
2929
return msg, kwargs

examples/example_with_logger/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@app.route("/")
1313
async def index(request: Request):
1414
log.info("Log from view")
15-
return JSONResponse(context.dict())
15+
return JSONResponse(context.data)
1616

1717

1818
app.add_middleware(

examples/example_with_logger/logger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __init__(self, logger, extra=None):
2424
def process(self, msg, kwargs):
2525
# here we are basically adding context to log
2626
extra = self.extra.copy()
27-
extra.update(context.dict())
27+
extra.update(context.data)
2828

2929
kwargs["extra"] = extra
3030
return msg, kwargs

examples/simple_examples/set_context_in_middleware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
@app.route("/")
1818
async def index(request: Request):
19-
return JSONResponse(context.dict())
19+
return JSONResponse(context.data)
2020

2121

2222
uvicorn.run(app, host="0.0.0.0")

examples/simple_examples/set_context_in_middleware_and_view.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
@app.route("/")
1313
async def index(request: Request):
1414
context["view"] = True
15-
return JSONResponse(context.dict())
15+
return JSONResponse(context.data)
1616

1717

1818
class ContextFromMiddleware(ContextMiddleware):
19-
def set_context(self, request: Request) -> dict:
19+
async def set_context(self, request: Request) -> dict:
2020
return {"middleware": True}
2121

2222

examples/simple_examples/set_context_in_view.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async def index(request: Request):
2020
a="b", ts=str(datetime.datetime.utcnow()), uuid=uuid.uuid4().hex
2121
)
2222

23-
return JSONResponse(context.dict())
23+
return JSONResponse(context.data)
2424

2525

2626
uvicorn.run(app, host="0.0.0.0")

pytest.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ addopts =
77
--doctest-modules
88
--cov=skift
99
-r a
10-
-v
10+
-v
11+
--strict

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pytest==5.3.2
1212
pytest-cov==2.8.1
1313
pytest-sugar==0.9.2
1414
pytest-xdist==1.31.0
15+
pytest-asyncio==0.10.0
1516
codecov
1617

1718
# lint

0 commit comments

Comments
 (0)