-
Notifications
You must be signed in to change notification settings - Fork 2k
/
flapi.py
52 lines (35 loc) · 1.29 KB
/
flapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import logging
import json
import ckan.model as model
import ckan.logic as logic
from flask import abort, jsonify, request, Response
from flask.views import MethodView
log = logging.getLogger(__name__)
class ApiView(MethodView):
def get(self, func_name):
# TODO: Identify user - check_access
try:
fn = logic.get_action(func_name)
except Exception, e:
abort(404)
params = request.args
# TODO: Check and pop callback
context = {'model':model, 'session': model.Session, 'user': ''}
try:
response = fn(context, params)
except logic.ValidationError, e:
error_dict = e.error_dict
error_dict['__type'] = 'Validation Error'
return_dict = {
'error': error_dict,
'success': False
}
log.info('Validation error (Action API): %r' % str(e.error_dict))
return jsonify(return_dict), 409
if isinstance(response, list):
# Flask won't allow jsonifying lists because it's unsafe. Apparently.
return Response(json.dumps(response), mimetype='application/json')
return jsonify(response)
def post(self, func_name):
print "A POST request to", func_name
return "!"