You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The validate_json_api_content_type helper checks whether the Content-Type header of a passed in request is JSON:API and otherwise returns an error response object.
Because it just returns an error object that the user needs to handle manually (as opposed to the automatic halting of the request handling as implemented in the ruby-template) its use is a bit confusing.
fromhelpersimportvalidate_json_api_content_typefromflaskimportrequest@app.route("/")defindex():
validate_json_api_content_type(request)
return"Hello from the mu-python-template!"
Given the above code and based on the function name and its docstring one might assume that the route handler would automatically return an error if the request didn't contain JSON:API, but in fact it does nothing. The proper code would look something like this:
fromhelpersimportvalidate_json_api_content_typefromflaskimportrequest@app.route("/")defindex():
maybe_error=validate_json_api_content_type(request)
ifmaybe_error:
returnmaybe_errorreturn"Hello from the mu-python-template!"
Possible solutions
Make validate_json_api_content_type raise an exception instead of returning an error object. Users of the template can then either manually catch and handle the raised exception as they deem fit, or add an error handler to their service that automatically handles all such errors:
# In template: helpers.pyclassInvalidJSONAPIContentTypeException(Exception):
"""Request does not contain valid JSON:API"""defvalidate_json_api_content_type(request):
"""Validate whether the request contains the JSONAPI content-type header (application/vnd.api+json). Raises an exception of type InvalidJSONAPIContentTypeException otherwise"""if"application/vnd.api+json"notinrequest.content_type:
raiseInvalidJSONAPIContentTypeException("Content-Type must be application/vnd.api+json instead of "+request.content_type)
# In service: web.pyfromhelpersimportvalidate_json_api_content_type, InvalidJSONAPIContentTypeExceptionfromflaskimportrequest@app.route("/")defindex():
try:
validate_json_api_content_type(request)
exceptInvalidJSONAPIContentTypeExceptionase:
returnerror(str(e), 400)
return"Hello from the mu-python-template!"# Or a service-level error handler, which would make the above try-except unnecessary@app.errorhandler(InvalidJSONAPIContentTypeException)defhandle_invalid_json_api_content_type(e):
returnerror(str(e), 400)
Alternatively, there might be a way to make the validate_json_api_content_type abort the request using some Flask function, emulating the behaviour of the ruby-template.
The helper can also be renamed and its documentation can be clarified to make its usage more obvious.
EDIT: the reasoning for opening an issue that I've found one place in Kaleidos code where someone wrote validate_json_api_content_type(request) and it effectively did nothing, making me assume they got confused about what the function actually did.
The text was updated successfully, but these errors were encountered:
Context
The
validate_json_api_content_type
helper checks whether theContent-Type
header of a passed in request is JSON:API and otherwise returns an error response object.Because it just returns an error object that the user needs to handle manually (as opposed to the automatic halting of the request handling as implemented in the ruby-template) its use is a bit confusing.
Given the above code and based on the function name and its docstring one might assume that the route handler would automatically return an error if the request didn't contain JSON:API, but in fact it does nothing. The proper code would look something like this:
Possible solutions
Make
validate_json_api_content_type
raise an exception instead of returning an error object. Users of the template can then either manually catch and handle the raised exception as they deem fit, or add an error handler to their service that automatically handles all such errors:Alternatively, there might be a way to make the
validate_json_api_content_type
abort the request using some Flask function, emulating the behaviour of the ruby-template.The helper can also be renamed and its documentation can be clarified to make its usage more obvious.
EDIT: the reasoning for opening an issue that I've found one place in Kaleidos code where someone wrote
validate_json_api_content_type(request)
and it effectively did nothing, making me assume they got confused about what the function actually did.The text was updated successfully, but these errors were encountered: