3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
11 The base class to dispatch request
17 from flask import request
18 from flask_restful import Resource
20 from functest.api.common import api_utils
23 LOGGER = logging.getLogger(__name__)
26 class ApiResource(Resource):
27 """ API Resource class"""
30 super(ApiResource, self).__init__()
32 def _post_args(self): # pylint: disable=no-self-use
33 # pylint: disable=maybe-no-member
34 """ Return action and args after parsing request """
36 data = request.json if request.json else {}
37 params = api_utils.change_to_str_in_dict(data)
38 action = params.get('action', request.form.get('action', ''))
39 args = params.get('args', {})
41 args['file'] = request.files['file']
44 LOGGER.debug('Input args are: action: %s, args: %s', action, args)
48 def _get_args(self): # pylint: disable=no-self-use
49 """ Convert the unicode to string for request.args """
50 args = api_utils.change_to_str_in_dict(request.args)
53 def _dispatch_post(self):
54 """ Dispatch request """
55 action, args = self._post_args()
56 return self._dispatch(args, action)
58 def _dispatch(self, args, action):
60 Dynamically load the classes with reflection and
61 obtain corresponding methods
64 return getattr(self, action)(args)
65 except AttributeError:
66 api_utils.result_handler(status=1, data='No such action')
69 # Import modules from package "functest.api.resources"
70 # and append them into sys.modules
71 api_utils.import_modules_from_package("functest.api.resources")