diff 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
line wrap: on
line diff
--- a/roundup/rest.py	Wed Jan 30 10:26:34 2019 +0100
+++ b/roundup/rest.py	Wed Jan 30 10:26:34 2019 +0100
@@ -41,12 +41,32 @@
     return props
 
 
+def error_obj(status, msg, source=None):
+    result = {
+        'error': {
+            'status': status,
+            'msg': msg
+        }
+    }
+    if source is not None:
+        result['error']['source'] = source
+
+    return result
+
+
+def data_obj(data):
+    result = {
+        'data': data
+    }
+    return result
+
+
 class RestfulInstance(object):
     """Dummy Handler for REST
     """
 
-    def __init__(self, db):
-        # TODO: database, translator and instance.actions
+    def __init__(self, client, db):
+        self.client = client  # it might be unnecessary to receive the client
         self.db = db
 
     def get_collection(self, class_name, input):
@@ -188,17 +208,22 @@
                 class_name, item_id = hyperdb.splitDesignator(resource_uri)
                 output = getattr(self, "%s_element" % method.lower())(
                     class_name, item_id, input)
-        except (hyperdb.DesignatorError, UsageError, Unauthorised), msg:
-            output = {'status': 'error', 'msg': msg}
+
+            output = data_obj(output)
+        except Unauthorised, msg:
+            output = error_obj(403, msg)
+        except (hyperdb.DesignatorError, UsageError), msg:
+            output = error_obj(400, msg)
         except (AttributeError, Reject):
-            output = {'status': 'error', 'msg': 'Method is not allowed'}
+            output = error_obj(405, 'Method Not Allowed')
         except NotImplementedError:
-            output = {'status': 'error', 'msg': 'Method is under development'}
+            output = error_obj(402, 'Method is under development')
+            # nothing to pay, just mark that this is under development
         except:
             # if self.DEBUG_MODE in roundup_server
             # else msg = 'An error occurred. Please check...',
             exc, val, tb = sys.exc_info()
-            output = {'status': 'error', 'msg': val}
+            output = error_obj(400, val)
 
             # out to the logfile, it would be nice if the server do it for me
             print 'EXCEPTION AT', time.ctime()

Roundup Issue Tracker: http://roundup-tracker.org/