squeaky-pl / japronto

Screaming-fast Python 3.5+ HTTP toolkit integrated with pipelining HTTP server based on uvloop and picohttpparser.
MIT License
8.61k stars 581 forks source link

Request.Response can only be called once per request when using decorators on routes #91

Open jonathan-kosgei opened 6 years ago

jonathan-kosgei commented 6 years ago

I'm working on implementing a decorator for a small Japronto app that handles rate limiting.

However I get the error

ValueError: View did not return Response instance
Unhandled exception in event loop
Traceback (most recent call last):
  File "uvloop/handles/stream.pyx", line 785, in uvloop.loop.__uv_stream_on_read_impl (uvloop/loop.c:76307)
  File "uvloop/handles/stream.pyx", line 559, in uvloop.loop.UVStream._on_read (uvloop/loop.c:73571)
  File "/usr/local/lib/python3.5/dist-packages/japronto/app/__init__.py", line 104, in error_handler
    return self.default_error_handler(request, exception)
  File "/usr/local/lib/python3.5/dist-packages/japronto/app/__init__.py", line 86, in default_error_handler
    text=tb if self._debug else 'Internal Server Error')
RuntimeError: Request.Response can only be called once per request

When implementing the following decorator

from japronto import Application

values = [1,2,3,4,5,6]

def rate_limit(*args):
    def wrapper(*args):
        # if count > 1000000
        # return request.Response(text='Exceeded Limit.')
        function(*args)
    return wrapper

@rate_limit
def lookup(request):
    value = request.match_dict['value']
    if value in values:
        return request.Response(text='Lookup succeeded')

app = Application()'
app.router.add_route('/{value}', lookup)

app.run(debug=True)