Created
January 10, 2014 18:05
-
-
Save cybertoast/8359387 to your computer and use it in GitHub Desktop.
Changing the response structure of any flask module after the fact
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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