Restful

API

exception twopi_flask_utils.restful.ExpectedJSONException

Thrown when JSON was expected in a flask request but was not provided

classmethod handle(exc)

A handler for this type of exception.

Usage:

app.errorhandler(ExpectedJSONException)(ExpectedJSONException.handle)
twopi_flask_utils.restful.get_and_expect_json()

Returns the ``flask.request.get_json()`, however if no JSON data was decoded, will raise a ExpectedJSONException

twopi_flask_utils.restful.output_json(data, code, headers=None)
api.representations['application/json'] = output_json

Generates better formatted responses for RESTFul APIs.

If the restful resource responds with a string, with a non 200 error, the response will look like

{
    "_errors": ["String the user responded with"]
}

Likewise, a string return value with a 200 response will look like:

{
    "message": "String the user returned with."
}

If a Non-200 response occured, and flask-restful added it’s own error message in the “message” field of the response data, this data is moved into “_errors”:

{
    "_errors": ["You don't have the permission to access the requested resource..."]
}

All data is returned using flask’s jsonify method. This means you can use simplejson to return decimal objects from your flask restful resources.