Skip to content

Instantly share code, notes, and snippets.

@siteshen
Created September 18, 2013 16:49
Show Gist options
  • Select an option

  • Save siteshen/6612013 to your computer and use it in GitHub Desktop.

Select an option

Save siteshen/6612013 to your computer and use it in GitHub Desktop.
A Mixin for Tornado and JSend specification. Django version: https://gist.github.com/siteshen/7688169
class JSendMixin(object):
"""http://labs.omniti.com/labs/jsend
JSend is a specification that lays down some rules for how JSON
responses from web servers should be formatted.
JSend focuses on application-level (as opposed to protocol- or
transport-level) messaging which makes it ideal for use in
REST-style applications and APIs.
"""
def success(self, data):
"""When an API call is successful, the JSend object is used as a simple
envelope for the results, using the data key.
"""
self.write({'status': 'success', 'data': data})
def fail(self, data):
"""There was a problem with the data submitted, or some pre-condition
of the API call wasn't satisfied.
"""
self.write({'status': 'fail', 'data': data})
def error(self, message, data=None, code=None):
"""An error occurred in processing the request, i.e. an exception was
thrown.
"""
result = {'status': 'error', 'message': message}
if data:
result['data'] = data
if code:
result['code'] = code
self.write(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment