Merge "move flatten dict key to common utils"
[yardstick.git] / api / base.py
index 5270085..0f1e76a 100644 (file)
@@ -15,6 +15,7 @@ from flask import request
 from flask_restful import Resource
 
 from api.utils import common as common_utils
+from yardstick.common import constants as consts
 
 logger = logging.getLogger(__name__)
 logger.setLevel(logging.DEBUG)
@@ -23,9 +24,16 @@ logger.setLevel(logging.DEBUG)
 class ApiResource(Resource):
 
     def _post_args(self):
-        params = common_utils.translate_to_str(request.json)
-        action = params.get('action', '')
+        data = request.json if request.json else {}
+        params = common_utils.translate_to_str(data)
+        action = params.get('action', request.form.get('action', ''))
         args = params.get('args', {})
+
+        try:
+            args['file'] = request.files['file']
+        except KeyError:
+            pass
+
         logger.debug('Input args is: action: %s, args: %s', action, args)
 
         return action, args
@@ -40,8 +48,9 @@ class ApiResource(Resource):
         action, args = self._post_args()
         return self._dispatch(args, action)
 
-    def _dispatch_get(self):
+    def _dispatch_get(self, **kwargs):
         args = self._get_args()
+        args.update(kwargs)
         return self._dispatch(args)
 
     def _dispatch(self, args, action='default'):
@@ -52,5 +61,5 @@ class ApiResource(Resource):
         resources = importlib.import_module(module_name)
         try:
             return getattr(resources, action)(args)
-        except NameError:
-            common_utils.error_handler('Wrong action')
+        except AttributeError:
+            common_utils.result_handler(consts.API_ERROR, 'No such action')