/
jsend.py
37 lines (29 loc) · 1.17 KB
/
jsend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Source: http://tornadogists.org/6612013/
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)