Skip to content
This repository has been archived by the owner on Mar 28, 2022. It is now read-only.

Better JSON handling in requests and responses #45

Merged
merged 3 commits into from
Apr 30, 2019

Conversation

brannondorsey
Copy link
Member

  • Always return content-type: application/json as response MIME type
  • Sniff body in POST requests for JSON even if content-type: application/json is not specified. If the sniff is successful, allow the request. If it fails return a nicer 400 explaining the body must be JSON instead of a 500 internal server error.
  • Return JSON for common server errors (401, 403, 404, 405, and 500).

My hope is that this gets us into the territory where the model server now only speaks JSON as responses. I have yet to get an html/text since implementing these changes but we should be on the lookout in case we missed something.

Closes #1
Closes #6

def define_routes(self):
@self.app.route('/')
def manifest():
return json.dumps(dict(
return jsonify(dict(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flask's jsonify() is a helpful replacement for json.dumps() that wraps its parameter in a Flask Response object and sets the MIME type to application/json.

return wrapped

def get_json_or_none_if_invalid(request):
return request.get_json(force=True, silent=True)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a thin wrapper around Flask's request.get_json() but it ensures that we are calling it such that it will sniff JSON even if the content-type: application/json MIME type is not set, and will return None if the sniff fails instead of throwing an error.

We can then use this util function elsewhere to make sure that we are parsing JSON from Flask response objects in a consistent way.

else:
err_msg = 'The body of all POST requests must contain JSON'
return jsonify(dict(error=err_msg)), 400
return wrapped
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A decorator to wrap around POST endpoints to ensure that requests that are sent to them contain JSON bodies. If they don't we immediately return a JSON error message and a 400 error code.

runway/model.py Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Apr 30, 2019

Codecov Report

Merging #45 into master will increase coverage by 2.1%.
The diff coverage is 89.28%.

@@            Coverage Diff            @@
##           master      #45     +/-   ##
=========================================
+ Coverage   91.98%   94.09%   +2.1%     
=========================================
  Files           5        5             
  Lines         424      440     +16     
=========================================
+ Hits          390      414     +24     
+ Misses         34       26      -8

Copy link
Member

@agermanidis agermanidis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@brannondorsey brannondorsey merged commit e289025 into master Apr 30, 2019
@brannondorsey brannondorsey deleted the always-return-json branch April 30, 2019 18:53
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants