I have a case where I'd like to do the following.
@app.on_event('response')
def response_handler(response):
return JSONResponse(response[1], status_code=response[0])
@app.route('/')
def root(request):
return 200, { 'hello': 'world' }
Is this sort of response handler possible?
No, there's nothing like that available.
You'll need to return response instances from endpoints.
@dgrahn if you really wanted to follow that pattern, you could write a decorator to place in between @app.route('/') and def root(request): that takes the result and packs it into the json response.
If you didn't want to place that decorator everywhere, you could override the app.route method to do it for you.
Would this be something you guys would be interested in as an enhancement?
Not currently, no. I think it's a reasonable design constraint for us to impose, in order to provide consistency.
Most helpful comment
@dgrahn if you really wanted to follow that pattern, you could write a decorator to place in between
@app.route('/')anddef root(request):that takes the result and packs it into the json response.If you didn't want to place that decorator everywhere, you could override the
app.routemethod to do it for you.