diff options
author | Yujun Zhang <zhang.yujunz@zte.com.cn> | 2017-03-18 06:41:55 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2017-03-18 06:41:55 +0000 |
commit | 6694910aae48dd42e7c777e587c32e03cacd400c (patch) | |
tree | 0ae217a9f2de994fcc18accdca5d1cc5d52f3f74 | |
parent | 62516af2321faa78a85ebcc9c09faf2f38fff65b (diff) | |
parent | 9f87878becc36b19110e151e802180aa5f7fb305 (diff) |
Merge "provide get_one_exceptions to unify get_xx process"
-rw-r--r-- | qtip/api/controllers/common.py | 19 | ||||
-rw-r--r-- | qtip/api/controllers/metric.py | 12 |
2 files changed, 22 insertions, 9 deletions
diff --git a/qtip/api/controllers/common.py b/qtip/api/controllers/common.py new file mode 100644 index 00000000..6cabbc7f --- /dev/null +++ b/qtip/api/controllers/common.py @@ -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 diff --git a/qtip/api/controllers/metric.py b/qtip/api/controllers/metric.py index 86bf70f9..dd4c8ac6 100644 --- a/qtip/api/controllers/metric.py +++ b/qtip/api/controllers/metric.py @@ -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} |