Remove checkno.png and checkyes.png due to license issue
[yardstick.git] / api / __init__.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
3 #
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
10 import logging
11
12 from flask import request
13 from flask_restful import Resource
14
15 from yardstick import _init_logging
16 from yardstick.common import constants as consts
17 from yardstick.common import utils as common_utils
18
19 _init_logging()
20 LOG = logging.getLogger(__name__)
21 LOG.setLevel(logging.DEBUG)
22
23
24 class ApiResource(Resource):
25
26     def _post_args(self):
27         data = request.json if request.json else {}
28         params = common_utils.translate_to_str(data)
29         action = params.get('action', request.form.get('action', ''))
30         args = params.get('args', {})
31
32         try:
33             args['file'] = request.files['file']
34         except KeyError:
35             pass
36
37         args.update({k: v for k, v in request.form.items()})
38
39         return action, args
40
41     def _get_args(self):
42         args = common_utils.translate_to_str(request.args)
43
44         return args
45
46     def _dispatch_post(self, **kwargs):
47         action, args = self._post_args()
48         args.update(kwargs)
49         return self._dispatch(args, action)
50
51     def _dispatch(self, args, action):
52         try:
53             return getattr(self, action)(args)
54         except AttributeError:
55             common_utils.result_handler(consts.API_ERROR, 'No such action')
56
57
58 class Url(object):
59
60     def __init__(self, url, target):
61         super(Url, self).__init__()
62         self.url = url
63         self.target = target
64
65 common_utils.import_modules_from_package("api.resources")