From 7846f644365bd401d31efa0bf086d30059229fef Mon Sep 17 00:00:00 2001 From: Bluenix Date: Fri, 11 Feb 2022 21:10:59 +0100 Subject: [PATCH 1/4] Document middleware `on_request` and `on_response` Without docstrings these methods do not show up in the autogenerated documentation. --- sanic/mixins/middleware.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/sanic/mixins/middleware.py b/sanic/mixins/middleware.py index 1049aacf6b..7cc9c75b65 100644 --- a/sanic/mixins/middleware.py +++ b/sanic/mixins/middleware.py @@ -16,9 +16,9 @@ def middleware( self, middleware_or_request, attach_to="request", apply=True ): """ - Decorate and register middleware to be called before a request. - Can either be called as *@app.middleware* or - *@app.middleware('request')* + Decorate and register middleware to be called before a request + is handled or a response sent. Can either be called as + *@app.middleware* or *@app.middleware('request')*. `See user guide re: middleware `__ @@ -47,12 +47,24 @@ def register_middleware(middleware, attach_to="request"): ) def on_request(self, middleware=None): + """Register a middleware to be called before a request is handled. + + This is the same as *@app.middleware('request')*. + + :param: middleware: A callable that takes in request. + """ if callable(middleware): return self.middleware(middleware, "request") else: return partial(self.middleware, attach_to="request") def on_response(self, middleware=None): + """Register a middleware to be called before a response is sent. + + This is the same as *@app.middleware('response')*. + + :param: middleware: A callable that takes in a response. + """ if callable(middleware): return self.middleware(middleware, "response") else: From a5927b839947195b1675a8797d6a41af73017818 Mon Sep 17 00:00:00 2001 From: Bluenix Date: Fri, 11 Feb 2022 22:17:37 +0100 Subject: [PATCH 2/4] Fix `on_response()` middleware arguments --- sanic/mixins/middleware.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanic/mixins/middleware.py b/sanic/mixins/middleware.py index 7cc9c75b65..8c0023960c 100644 --- a/sanic/mixins/middleware.py +++ b/sanic/mixins/middleware.py @@ -63,7 +63,7 @@ def on_response(self, middleware=None): This is the same as *@app.middleware('response')*. - :param: middleware: A callable that takes in a response. + :param: middleware: A callable that takes in a request and its response. """ if callable(middleware): return self.middleware(middleware, "response") From bbc761d96646858e6cb90c87c5e4de695c241d15 Mon Sep 17 00:00:00 2001 From: Bluenix Date: Sat, 12 Feb 2022 00:54:22 +0100 Subject: [PATCH 3/4] Fix too long `:param:` docstring line --- sanic/mixins/middleware.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sanic/mixins/middleware.py b/sanic/mixins/middleware.py index 8c0023960c..cd5e176a52 100644 --- a/sanic/mixins/middleware.py +++ b/sanic/mixins/middleware.py @@ -63,7 +63,8 @@ def on_response(self, middleware=None): This is the same as *@app.middleware('response')*. - :param: middleware: A callable that takes in a request and its response. + :param: middleware: + A callable that takes in a request and its response. """ if callable(middleware): return self.middleware(middleware, "response") From 240ae583df114230649b8304783f9454ef464d2a Mon Sep 17 00:00:00 2001 From: Bluenix Date: Sun, 13 Feb 2022 02:39:48 +0100 Subject: [PATCH 4/4] Update explanations of response middlewares --- sanic/mixins/middleware.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sanic/mixins/middleware.py b/sanic/mixins/middleware.py index cd5e176a52..5ef9dc77a1 100644 --- a/sanic/mixins/middleware.py +++ b/sanic/mixins/middleware.py @@ -17,7 +17,7 @@ def middleware( ): """ Decorate and register middleware to be called before a request - is handled or a response sent. Can either be called as + is handled or after a response is created. Can either be called as *@app.middleware* or *@app.middleware('request')*. `See user guide re: middleware @@ -59,7 +59,7 @@ def on_request(self, middleware=None): return partial(self.middleware, attach_to="request") def on_response(self, middleware=None): - """Register a middleware to be called before a response is sent. + """Register a middleware to be called after a response is created. This is the same as *@app.middleware('response')*.