Yardstick API refactor 21/26321/2
authorchenjiankun <chenjiankun1@huawei.com>
Wed, 21 Dec 2016 01:07:26 +0000 (01:07 +0000)
committerchenjiankun <chenjiankun1@huawei.com>
Thu, 22 Dec 2016 11:11:37 +0000 (11:11 +0000)
JIRA: YARDSTICK-503

Now in api/views.py there are many redundant code.
So I do some refactoring and make it to be a lightweight framework.

Change-Id: Id7cecc95e60f5403b2d26239a3ef41d01bbb542a
Signed-off-by: chenjiankun <chenjiankun1@huawei.com>
api/base.py [new file with mode: 0644]
api/resources/__init__.py [moved from api/actions/__init__.py with 100% similarity]
api/resources/env_action.py [moved from api/actions/env.py with 100% similarity]
api/resources/release_action.py [moved from api/actions/test.py with 100% similarity]
api/resources/results.py [moved from api/actions/result.py with 97% similarity]
api/resources/samples_action.py [moved from api/actions/samples.py with 100% similarity]
api/urls.py
api/views.py

diff --git a/api/base.py b/api/base.py
new file mode 100644 (file)
index 0000000..7671527
--- /dev/null
@@ -0,0 +1,55 @@
+##############################################################################
+# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+import re
+import importlib
+import logging
+
+from flask import request
+from flask_restful import Resource
+
+from api.utils import common as common_utils
+
+logger = logging.getLogger(__name__)
+logger.setLevel(logging.DEBUG)
+
+
+class ApiResource(Resource):
+
+    def _post_args(self):
+        params = common_utils.translate_to_str(request.json)
+        action = params.get('action', '')
+        args = params.get('args', {})
+        logger.debug('Input args is: action: %s, args: %s', action, args)
+
+        return action, args
+
+    def _get_args(self):
+        args = common_utils.translate_to_str(request.args)
+        logger.debug('Input args is: args: %s', args)
+
+        return args
+
+    def _dispatch_post(self):
+        action, args = self._post_args()
+        return self._dispatch(args, action)
+
+    def _dispatch_get(self):
+        args = self._get_args()
+        return self._dispatch(args)
+
+    def _dispatch(self, args, action='default'):
+        module_name = re.sub(r'([A-Z][a-z]*)', r'_\1',
+                             self.__class__.__name__)[1:].lower()
+
+        module_name = 'api.resources.%s' % module_name
+        resources = importlib.import_module(module_name)
+        try:
+            return getattr(resources, action)(args)
+        except NameError:
+            common_utils.error_handler('Wrong action')
similarity index 97%
rename from api/actions/result.py
rename to api/resources/results.py
index 1f200fb..3de09fd 100644 (file)
@@ -17,6 +17,10 @@ from api import conf
 logger = logging.getLogger(__name__)
 
 
+def default(args):
+    return getResult(args)
+
+
 def getResult(args):
     try:
         measurement = args['measurement']
index 50be91e..0fffd12 100644 (file)
@@ -11,8 +11,8 @@ from api.utils.common import Url
 
 
 urlpatterns = [
-    Url('/yardstick/testcases/release/action', views.Release, 'release'),
-    Url('/yardstick/testcases/samples/action', views.Samples, 'samples'),
+    Url('/yardstick/testcases/release/action', views.ReleaseAction, 'release'),
+    Url('/yardstick/testcases/samples/action', views.SamplesAction, 'samples'),
     Url('/yardstick/results', views.Results, 'results'),
-    Url('/yardstick/env/action', views.Env, 'env')
+    Url('/yardstick/env/action', views.EnvAction, 'env')
 ]
index 928d8e9..ee13b47 100644 (file)
@@ -9,18 +9,13 @@
 import logging
 import os
 
-from flask import request
-from flask_restful import Resource
 from flasgger.utils import swag_from
 
-from api.utils import common as common_utils
+from api.base import ApiResource
 from api.swagger import models
-from api.actions import test as test_action
-from api.actions import samples as samples_action
-from api.actions import result as result_action
-from api.actions import env as env_action
 
 logger = logging.getLogger(__name__)
+logger.setLevel(logging.DEBUG)
 
 
 TestCaseActionModel = models.TestCaseActionModel
@@ -29,54 +24,26 @@ TestCaseActionArgsOptsModel = models.TestCaseActionArgsOptsModel
 TestCaseActionArgsOptsTaskArgModel = models.TestCaseActionArgsOptsTaskArgModel
 
 
-class Release(Resource):
+class ReleaseAction(ApiResource):
     @swag_from(os.getcwd() + '/swagger/docs/testcases.yaml')
     def post(self):
-        action = common_utils.translate_to_str(request.json.get('action', ''))
-        args = common_utils.translate_to_str(request.json.get('args', {}))
-        logger.debug('Input args is: action: %s, args: %s', action, args)
+        return self._dispatch_post()
 
-        try:
-            return getattr(test_action, action)(args)
-        except AttributeError:
-            return common_utils.error_handler('Wrong action')
 
-
-class Samples(Resource):
+class SamplesAction(ApiResource):
     def post(self):
-        action = common_utils.translate_to_str(request.json.get('action', ''))
-        args = common_utils.translate_to_str(request.json.get('args', {}))
-        logger.debug('Input args is: action: %s, args: %s', action, args)
-
-        try:
-            return getattr(samples_action, action)(args)
-        except AttributeError:
-            return common_utils.error_handler('Wrong action')
+        return self._dispatch_post()
 
 
 ResultModel = models.ResultModel
 
 
-class Results(Resource):
+class Results(ApiResource):
     @swag_from(os.getcwd() + '/swagger/docs/results.yaml')
     def get(self):
-        args = common_utils.translate_to_str(request.args)
-        action = args.get('action', '')
-        logger.debug('Input args is: action: %s, args: %s', action, args)
+        return self._dispatch_get()
 
-        try:
-            return getattr(result_action, action)(args)
-        except AttributeError:
-            return common_utils.error_handler('Wrong action')
 
-
-class Env(Resource):
+class EnvAction(ApiResource):
     def post(self):
-        action = common_utils.translate_to_str(request.json.get('action', ''))
-        args = common_utils.translate_to_str(request.json.get('args', {}))
-        logger.debug('Input args is: action: %s, args: %s', action, args)
-
-        try:
-            return getattr(env_action, action)(args)
-        except AttributeError:
-            return common_utils.error_handler('Wrong action')
+        return self._dispatch_post()