Add signals before and after handler execution (#2540)
This commit is contained in:
parent
e5010286b4
commit
d352a4155e
10
sanic/app.py
10
sanic/app.py
|
@ -894,9 +894,19 @@ class Sanic(BaseSanic, RunnerMixin, metaclass=TouchUpMeta):
|
||||||
)
|
)
|
||||||
|
|
||||||
# Run response handler
|
# Run response handler
|
||||||
|
await self.dispatch(
|
||||||
|
"http.handler.before",
|
||||||
|
inline=True,
|
||||||
|
context={"request": request},
|
||||||
|
)
|
||||||
response = handler(request, **request.match_info)
|
response = handler(request, **request.match_info)
|
||||||
if isawaitable(response):
|
if isawaitable(response):
|
||||||
response = await response
|
response = await response
|
||||||
|
await self.dispatch(
|
||||||
|
"http.handler.after",
|
||||||
|
inline=True,
|
||||||
|
context={"request": request},
|
||||||
|
)
|
||||||
|
|
||||||
if request.responded:
|
if request.responded:
|
||||||
if response is not None:
|
if response is not None:
|
||||||
|
|
|
@ -30,6 +30,8 @@ class Event(Enum):
|
||||||
HTTP_LIFECYCLE_RESPONSE = "http.lifecycle.response"
|
HTTP_LIFECYCLE_RESPONSE = "http.lifecycle.response"
|
||||||
HTTP_ROUTING_AFTER = "http.routing.after"
|
HTTP_ROUTING_AFTER = "http.routing.after"
|
||||||
HTTP_ROUTING_BEFORE = "http.routing.before"
|
HTTP_ROUTING_BEFORE = "http.routing.before"
|
||||||
|
HTTP_HANDLER_AFTER = "http.handler.after"
|
||||||
|
HTTP_HANDLER_BEFORE = "http.handler.before"
|
||||||
HTTP_LIFECYCLE_SEND = "http.lifecycle.send"
|
HTTP_LIFECYCLE_SEND = "http.lifecycle.send"
|
||||||
HTTP_MIDDLEWARE_AFTER = "http.middleware.after"
|
HTTP_MIDDLEWARE_AFTER = "http.middleware.after"
|
||||||
HTTP_MIDDLEWARE_BEFORE = "http.middleware.before"
|
HTTP_MIDDLEWARE_BEFORE = "http.middleware.before"
|
||||||
|
@ -53,6 +55,8 @@ RESERVED_NAMESPACES = {
|
||||||
Event.HTTP_LIFECYCLE_RESPONSE.value,
|
Event.HTTP_LIFECYCLE_RESPONSE.value,
|
||||||
Event.HTTP_ROUTING_AFTER.value,
|
Event.HTTP_ROUTING_AFTER.value,
|
||||||
Event.HTTP_ROUTING_BEFORE.value,
|
Event.HTTP_ROUTING_BEFORE.value,
|
||||||
|
Event.HTTP_HANDLER_AFTER.value,
|
||||||
|
Event.HTTP_HANDLER_BEFORE.value,
|
||||||
Event.HTTP_LIFECYCLE_SEND.value,
|
Event.HTTP_LIFECYCLE_SEND.value,
|
||||||
Event.HTTP_MIDDLEWARE_AFTER.value,
|
Event.HTTP_MIDDLEWARE_AFTER.value,
|
||||||
Event.HTTP_MIDDLEWARE_BEFORE.value,
|
Event.HTTP_MIDDLEWARE_BEFORE.value,
|
||||||
|
|
|
@ -531,6 +531,8 @@ async def test_signals_triggered(app):
|
||||||
"http.lifecycle.handle",
|
"http.lifecycle.handle",
|
||||||
"http.routing.before",
|
"http.routing.before",
|
||||||
"http.routing.after",
|
"http.routing.after",
|
||||||
|
"http.handler.before",
|
||||||
|
"http.handler.after",
|
||||||
"http.lifecycle.response",
|
"http.lifecycle.response",
|
||||||
# "http.lifecycle.send",
|
# "http.lifecycle.send",
|
||||||
# "http.lifecycle.complete",
|
# "http.lifecycle.complete",
|
||||||
|
|
36
tests/test_handler.py
Normal file
36
tests/test_handler.py
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
from sanic.app import Sanic
|
||||||
|
from sanic.response import empty
|
||||||
|
from sanic.signals import Event
|
||||||
|
|
||||||
|
|
||||||
|
def test_handler_operation_order(app: Sanic):
|
||||||
|
operations = []
|
||||||
|
|
||||||
|
@app.on_request
|
||||||
|
async def on_request(_):
|
||||||
|
nonlocal operations
|
||||||
|
operations.append(1)
|
||||||
|
|
||||||
|
@app.on_response
|
||||||
|
async def on_response(*_):
|
||||||
|
nonlocal operations
|
||||||
|
operations.append(5)
|
||||||
|
|
||||||
|
@app.get("/")
|
||||||
|
async def handler(_):
|
||||||
|
nonlocal operations
|
||||||
|
operations.append(3)
|
||||||
|
return empty()
|
||||||
|
|
||||||
|
@app.signal(Event.HTTP_HANDLER_BEFORE)
|
||||||
|
async def handler_before(**_):
|
||||||
|
nonlocal operations
|
||||||
|
operations.append(2)
|
||||||
|
|
||||||
|
@app.signal(Event.HTTP_HANDLER_AFTER)
|
||||||
|
async def handler_after(**_):
|
||||||
|
nonlocal operations
|
||||||
|
operations.append(4)
|
||||||
|
|
||||||
|
app.test_client.get("/")
|
||||||
|
assert operations == [1, 2, 3, 4, 5]
|
Loading…
Reference in New Issue
Block a user