Created
April 19, 2019 21:00
-
-
Save jfhbrook/dbce447d3f2546475e1b035f0a0912df to your computer and use it in GitHub Desktop.
A snippet from the Kinja data team's implementation of make_response in our subclass of Flask
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
# In the default handler for flask, they don't allow None. We however | |
# allow anything that's json serializable. :) | |
if not isinstance(rv, self.response_class): | |
if isinstance(rv, (bytes, bytearray)): | |
# For now, treat bytes as raw data and don't attempt to | |
# JSON-serialize | |
rv = self.response_class(rv, status=status, headers=headers) | |
status = headers = None | |
# Try to coerce things that are Responses or wsgi callables already | |
elif isinstance(rv, WerkzeugResponse) or callable(rv): | |
rv = self.response_class.force_type(rv, request.environ) | |
else: | |
# If this is gonna be a problem we'll soon find out! | |
rv = success(rv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment