Mercurial > p > roundup > code
comparison roundup/rest.py @ 5568:edab9daa8015 REST-rebased
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 <rsc@runtux.com>
| author | Chau Nguyen <dangchau1991@yahoo.com> |
|---|---|
| date | Wed, 30 Jan 2019 10:26:34 +0100 |
| parents | 1af57f9d5bf7 |
| children | 2718aeb55ffa |
comparison
equal
deleted
inserted
replaced
| 5567:1af57f9d5bf7 | 5568:edab9daa8015 |
|---|---|
| 39 props[key] = None | 39 props[key] = None |
| 40 | 40 |
| 41 return props | 41 return props |
| 42 | 42 |
| 43 | 43 |
| 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 | |
| 44 class RestfulInstance(object): | 64 class RestfulInstance(object): |
| 45 """Dummy Handler for REST | 65 """Dummy Handler for REST |
| 46 """ | 66 """ |
| 47 | 67 |
| 48 def __init__(self, db): | 68 def __init__(self, client, db): |
| 49 # TODO: database, translator and instance.actions | 69 self.client = client # it might be unnecessary to receive the client |
| 50 self.db = db | 70 self.db = db |
| 51 | 71 |
| 52 def get_collection(self, class_name, input): | 72 def get_collection(self, class_name, input): |
| 53 if not self.db.security.hasPermission('View', self.db.getuid(), | 73 if not self.db.security.hasPermission('View', self.db.getuid(), |
| 54 class_name): | 74 class_name): |
| 186 resource_uri, input) | 206 resource_uri, input) |
| 187 else: | 207 else: |
| 188 class_name, item_id = hyperdb.splitDesignator(resource_uri) | 208 class_name, item_id = hyperdb.splitDesignator(resource_uri) |
| 189 output = getattr(self, "%s_element" % method.lower())( | 209 output = getattr(self, "%s_element" % method.lower())( |
| 190 class_name, item_id, input) | 210 class_name, item_id, input) |
| 191 except (hyperdb.DesignatorError, UsageError, Unauthorised), msg: | 211 |
| 192 output = {'status': 'error', 'msg': msg} | 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) | |
| 193 except (AttributeError, Reject): | 217 except (AttributeError, Reject): |
| 194 output = {'status': 'error', 'msg': 'Method is not allowed'} | 218 output = error_obj(405, 'Method Not Allowed') |
| 195 except NotImplementedError: | 219 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 | |
| 197 except: | 222 except: |
| 198 # if self.DEBUG_MODE in roundup_server | 223 # if self.DEBUG_MODE in roundup_server |
| 199 # else msg = 'An error occurred. Please check...', | 224 # else msg = 'An error occurred. Please check...', |
| 200 exc, val, tb = sys.exc_info() | 225 exc, val, tb = sys.exc_info() |
| 201 output = {'status': 'error', 'msg': val} | 226 output = error_obj(400, val) |
| 202 | 227 |
| 203 # out to the logfile, it would be nice if the server do it for me | 228 # out to the logfile, it would be nice if the server do it for me |
| 204 print 'EXCEPTION AT', time.ctime() | 229 print 'EXCEPTION AT', time.ctime() |
| 205 traceback.print_exc() | 230 traceback.print_exc() |
| 206 finally: | 231 finally: |
