Skip to content

Instantly share code, notes, and snippets.

@jfhbrook
Created April 19, 2019 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfhbrook/dbce447d3f2546475e1b035f0a0912df to your computer and use it in GitHub Desktop.
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
# 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