Skip to content

Commit 885b536

Browse files
committed
Make objects returned by REST follow the standard
Making objects returned by REST follow the standard (wrapped by a dictionary, in either 'data' or 'error' field) Temporally added the client to REST so REST can make changes to HTTP Status code and Header committer: Ralf Schlatterbeck <[email protected]>
1 parent a802717 commit 885b536

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

roundup/cgi/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ def handle_rest(self):
497497
self.check_anonymous_access()
498498

499499
# Call rest library to handle the request
500-
handler = rest.RestfulInstance(self.db)
500+
handler = rest.RestfulInstance(self, self.db)
501501
output = handler.dispatch(self.env['REQUEST_METHOD'], self.path,
502502
self.form)
503503

roundup/rest.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,32 @@ def props_from_args(db, cl, args, itemid=None):
4141
return props
4242

4343

44+
def error_obj(status, msg, source=None):
45+
result = {
46+
'error': {
47+
'status': status,
48+
'msg': msg
49+
}
50+
}
51+
if source is not None:
52+
result['error']['source'] = source
53+
54+
return result
55+
56+
57+
def data_obj(data):
58+
result = {
59+
'data': data
60+
}
61+
return result
62+
63+
4464
class RestfulInstance(object):
4565
"""Dummy Handler for REST
4666
"""
4767

48-
def __init__(self, db):
49-
# TODO: database, translator and instance.actions
68+
def __init__(self, client, db):
69+
self.client = client # it might be unnecessary to receive the client
5070
self.db = db
5171

5272
def get_collection(self, class_name, input):
@@ -188,17 +208,22 @@ def dispatch(self, method, uri, input):
188208
class_name, item_id = hyperdb.splitDesignator(resource_uri)
189209
output = getattr(self, "%s_element" % method.lower())(
190210
class_name, item_id, input)
191-
except (hyperdb.DesignatorError, UsageError, Unauthorised), msg:
192-
output = {'status': 'error', 'msg': msg}
211+
212+
output = data_obj(output)
213+
except Unauthorised, msg:
214+
output = error_obj(403, msg)
215+
except (hyperdb.DesignatorError, UsageError), msg:
216+
output = error_obj(400, msg)
193217
except (AttributeError, Reject):
194-
output = {'status': 'error', 'msg': 'Method is not allowed'}
218+
output = error_obj(405, 'Method Not Allowed')
195219
except NotImplementedError:
196-
output = {'status': 'error', 'msg': 'Method is under development'}
220+
output = error_obj(402, 'Method is under development')
221+
# nothing to pay, just mark that this is under development
197222
except:
198223
# if self.DEBUG_MODE in roundup_server
199224
# else msg = 'An error occurred. Please check...',
200225
exc, val, tb = sys.exc_info()
201-
output = {'status': 'error', 'msg': val}
226+
output = error_obj(400, val)
202227

203228
# out to the logfile, it would be nice if the server do it for me
204229
print 'EXCEPTION AT', time.ctime()

0 commit comments

Comments
 (0)