diff options
Diffstat (limited to 'api')
-rw-r--r-- | api/base.py | 55 | ||||
-rw-r--r-- | api/resources/__init__.py (renamed from api/actions/__init__.py) | 0 | ||||
-rw-r--r-- | api/resources/env_action.py (renamed from api/actions/env.py) | 0 | ||||
-rw-r--r-- | api/resources/release_action.py (renamed from api/actions/test.py) | 0 | ||||
-rw-r--r-- | api/resources/results.py (renamed from api/actions/result.py) | 4 | ||||
-rw-r--r-- | api/resources/samples_action.py (renamed from api/actions/samples.py) | 0 | ||||
-rw-r--r-- | api/urls.py | 6 | ||||
-rw-r--r-- | api/views.py | 53 |
8 files changed, 72 insertions, 46 deletions
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 index e69de29bb..e69de29bb 100644 --- a/api/actions/__init__.py +++ b/api/resources/__init__.py diff --git a/api/actions/env.py b/api/resources/env_action.py index fa0f95d90..fa0f95d90 100644 --- a/api/actions/env.py +++ b/api/resources/env_action.py diff --git a/api/actions/test.py b/api/resources/release_action.py index fda0ffd32..fda0ffd32 100644 --- a/api/actions/test.py +++ b/api/resources/release_action.py diff --git a/api/actions/result.py b/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 index 545447aec..545447aec 100644 --- a/api/actions/samples.py +++ b/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() |