provide get_one_exceptions to unify get_xx process 57/31157/1
authorSerenaFeng <feng.xiaowei@zte.com.cn>
Tue, 14 Mar 2017 02:06:41 +0000 (10:06 +0800)
committerYujun Zhang <zhang.yujunz@zte.com.cn>
Tue, 21 Mar 2017 09:09:51 +0000 (09:09 +0000)
add refactor get_metric()

Change-Id: I972c77e63a654eeb286c573d75efe842b2887c1d
Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
(cherry picked from commit 9f87878becc36b19110e151e802180aa5f7fb305)

qtip/api/controllers/common.py [new file with mode: 0644]
qtip/api/controllers/metric.py

diff --git a/qtip/api/controllers/common.py b/qtip/api/controllers/common.py
new file mode 100644 (file)
index 0000000..6cabbc7
--- /dev/null
@@ -0,0 +1,19 @@
+import httplib
+
+import connexion
+
+from qtip.base import error
+
+
+def get_one_exceptions(resource):
+    def _decorator(func):
+        def _execute(name):
+            try:
+                return func(name), httplib.OK
+            except error.NotFoundError:
+                return connexion.problem(
+                    httplib.NOT_FOUND,
+                    '{} Not Found'.format(resource),
+                    'Requested {} `{}` not found.'.format(resource, name))
+        return _execute
+    return _decorator
index 86bf70f..dd4c8ac 100644 (file)
@@ -9,9 +9,7 @@
 
 import httplib
 
-import connexion
-
-from qtip.base import error
+from qtip.api.controllers import common
 from qtip.loader import metric
 
 
@@ -20,13 +18,9 @@ def list_metrics():
     return metric_list, httplib.OK
 
 
+@common.get_one_exceptions(resource='metric')
 def get_metric(name):
-    try:
         metric_spec = metric.MetricSpec(name)
         return {'name': metric_spec.name,
                 'abspath': metric_spec.abspath,
-                'content': metric_spec.content}, httplib.OK
-    except error.NotFoundError:
-        return connexion.problem(httplib.NOT_FOUND,
-                                 'Metric Not Found',
-                                 'Requested metric `' + name + '` not found.')
+                'content': metric_spec.content}