From 70d25b87c167bc13e883da2963980cce56410f98 Mon Sep 17 00:00:00 2001 From: chenjiankun Date: Wed, 21 Dec 2016 01:07:26 +0000 Subject: [PATCH] Yardstick API refactor 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 --- api/base.py | 55 ++++++++++++++++++++++ api/{actions => resources}/__init__.py | 0 api/{actions/env.py => resources/env_action.py} | 0 .../test.py => resources/release_action.py} | 0 api/{actions/result.py => resources/results.py} | 4 ++ .../samples.py => resources/samples_action.py} | 0 api/urls.py | 6 +-- api/views.py | 53 ++++----------------- 8 files changed, 72 insertions(+), 46 deletions(-) create mode 100644 api/base.py rename api/{actions => resources}/__init__.py (100%) rename api/{actions/env.py => resources/env_action.py} (100%) rename api/{actions/test.py => resources/release_action.py} (100%) rename api/{actions/result.py => resources/results.py} (97%) rename api/{actions/samples.py => resources/samples_action.py} (100%) diff --git a/api/base.py b/api/base.py new file mode 100644 index 000000000..7671527d4 --- /dev/null +++ b/api/base.py @@ -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') diff --git a/api/actions/__init__.py b/api/resources/__init__.py similarity index 100% rename from api/actions/__init__.py rename to api/resources/__init__.py diff --git a/api/actions/env.py b/api/resources/env_action.py similarity index 100% rename from api/actions/env.py rename to api/resources/env_action.py diff --git a/api/actions/test.py b/api/resources/release_action.py similarity index 100% rename from api/actions/test.py rename to api/resources/release_action.py diff --git a/api/actions/result.py b/api/resources/results.py similarity index 97% rename from api/actions/result.py rename to api/resources/results.py index 1f200fbcc..3de09fdc9 100644 --- a/api/actions/result.py +++ b/api/resources/results.py @@ -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'] diff --git a/api/actions/samples.py b/api/resources/samples_action.py similarity index 100% rename from api/actions/samples.py rename to api/resources/samples_action.py diff --git a/api/urls.py b/api/urls.py index 50be91ead..0fffd12db 100644 --- a/api/urls.py +++ b/api/urls.py @@ -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') ] diff --git a/api/views.py b/api/views.py index 928d8e9eb..ee13b47a9 100644 --- a/api/views.py +++ b/api/views.py @@ -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() -- 2.16.6