Merge "Add API(v2) to add environment to task"
[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         LOG.debug('Input args is: action: %s, args: %s', action, args)
39
40         return action, args
41
42     def _get_args(self):
43         args = common_utils.translate_to_str(request.args)
44         LOG.debug('Input args is: args: %s', args)
45
46         return args
47
48     def _dispatch_post(self, **kwargs):
49         action, args = self._post_args()
50         args.update(kwargs)
51         return self._dispatch(args, action)
52
53     def _dispatch(self, args, action):
54         try:
55             return getattr(self, action)(args)
56         except AttributeError:
57             common_utils.result_handler(consts.API_ERROR, 'No such action')
58
59
60 class Url(object):
61
62     def __init__(self, url, target):
63         super(Url, self).__init__()
64         self.url = url
65         self.target = target
66
67 common_utils.import_modules_from_package("api.resources")