From bf5438d5733c85c72dc092365a9570bdd2531e00 Mon Sep 17 00:00:00 2001 From: Johnny Rocketfingers Date: Thu, 18 May 2017 06:34:28 +0200 Subject: [PATCH 1/2] Extract register_middleware into a method. --- sanic/app.py | 17 +++++++++-------- sanic/blueprints.py | 7 ++++--- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index 8d846c3e..174c763b 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -272,24 +272,25 @@ class Sanic: return response + def register_middleware(self, middleware, attach_to='request'): + if attach_to == 'request': + self.request_middleware.append(middleware) + if attach_to == 'response': + self.response_middleware.appendleft(middleware) + return middleware + # Decorator def middleware(self, middleware_or_request): """Decorate and register middleware to be called before a request. Can either be called as @app.middleware or @app.middleware('request') """ - def register_middleware(middleware, attach_to='request'): - if attach_to == 'request': - self.request_middleware.append(middleware) - if attach_to == 'response': - self.response_middleware.appendleft(middleware) - return middleware # Detect which way this was called, @middleware or @middleware('AT') if callable(middleware_or_request): - return register_middleware(middleware_or_request) + return self.register_middleware(middleware_or_request) else: - return partial(register_middleware, + return partial(Sanic.register_middleware, self, attach_to=middleware_or_request) # Static Files diff --git a/sanic/blueprints.py b/sanic/blueprints.py index 7e9953e0..0d3f0524 100644 --- a/sanic/blueprints.py +++ b/sanic/blueprints.py @@ -65,10 +65,11 @@ class Blueprint: # Middleware for future in self.middlewares: if future.args or future.kwargs: - app.middleware(*future.args, - **future.kwargs)(future.middleware) + app.register_middleware(future.middleware, + *future.args, + **future.kwargs) else: - app.middleware(future.middleware) + app.register_middleware(future.middleware) # Exceptions for future in self.exceptions: From 3f841f3b21cabf656367ecba578816568634a6cf Mon Sep 17 00:00:00 2001 From: Johnny Rocketfingers Date: Thu, 18 May 2017 22:08:44 +0200 Subject: [PATCH 2/2] Switch to non-hardcoded register_middleware. --- sanic/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanic/app.py b/sanic/app.py index 174c763b..8b836151 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -290,7 +290,7 @@ class Sanic: return self.register_middleware(middleware_or_request) else: - return partial(Sanic.register_middleware, self, + return partial(self.register_middleware, attach_to=middleware_or_request) # Static Files