--- /dev/null
+##############################################################################
+# 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')
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')
]
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
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()