Skip to content

Commit

Permalink
Implement fix for how middlewares are created
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed May 18, 2016
1 parent 6c66290 commit a027d42
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions hug/decorators.py
Expand Up @@ -90,8 +90,14 @@ def request_middleware(api=None):
"""Registers a middleware function that will be called on every request"""
def decorator(middleware_method):
apply_to_api = hug.API(api) if api else hug.api.from_object(middleware_method)
middleware_method.__self__ = middleware_method
apply_to_api.http.add_middleware(namedtuple('MiddlewareRouter', ('process_request', ))(middleware_method))

class MiddlewareRouter(object):
__slots__ = ()

def process_request(self, request, response):
return middleware_method(request, response)

apply_to_api.http.add_middleware(MiddlewareRouter())
return middleware_method
return decorator

Expand All @@ -100,8 +106,14 @@ def response_middleware(api=None):
"""Registers a middleware function that will be called on every response"""
def decorator(middleware_method):
apply_to_api = hug.API(api) if api else hug.api.from_object(middleware_method)
middleware_method.__self__ = middleware_method
apply_to_api.http.add_middleware(namedtuple('MiddlewareRouter', ('process_response', ))(middleware_method))

class MiddlewareRouter(object):
__slots__ = ()

def process_response(self, request, response, resource):
return middleware_method(request, response, resource)

apply_to_api.http.add_middleware(MiddlewareRouter())
return middleware_method
return decorator

Expand Down

0 comments on commit a027d42

Please sign in to comment.