Skip to content

Instantly share code, notes, and snippets.

@cybertoast
Created January 10, 2014 18:05
Show Gist options
  • Save cybertoast/8359387 to your computer and use it in GitHub Desktop.
Save cybertoast/8359387 to your computer and use it in GitHub Desktop.
Changing the response structure of any flask module after the fact
# To override flask-security's API response add the following into your app's configure_security():
@app.after_request
def after_request(response):
"""Convert flask-security response into a format we can use
Flask-security returns
{'meta': {'code': 200}, 'response': { 'user': {...} }
But we need {'code': 200, 'data': {'user': ...} }
"""
from nose.tools import set_trace; set_trace()
if response.content_type == 'application/json':
try:
rdata = json.loads(response.data)
ndata = dict(code=rdata.get('meta').get('code'),
data=[rdata.get('response')])
response.data = json.dumps(ndata)
except Exception as exc:
app.logger.error('', exc_info=True)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment