1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from __future__ import absolute_import
14 from flask import request
15 from flask_restful import Resource
17 from api.utils import common as common_utils
19 logger = logging.getLogger(__name__)
20 logger.setLevel(logging.DEBUG)
23 class ApiResource(Resource):
26 params = common_utils.translate_to_str(request.json)
27 action = params.get('action', '')
28 args = params.get('args', {})
29 logger.debug('Input args is: action: %s, args: %s', action, args)
34 args = common_utils.translate_to_str(request.args)
35 logger.debug('Input args is: args: %s', args)
39 def _dispatch_post(self):
40 action, args = self._post_args()
41 return self._dispatch(args, action)
43 def _dispatch_get(self):
44 args = self._get_args()
45 return self._dispatch(args)
47 def _dispatch(self, args, action='default'):
48 module_name = re.sub(r'([A-Z][a-z]*)', r'_\1',
49 self.__class__.__name__)[1:].lower()
51 module_name = 'api.resources.%s' % module_name
52 resources = importlib.import_module(module_name)
54 return getattr(resources, action)(args)
56 common_utils.error_handler('Wrong action')