-
Notifications
You must be signed in to change notification settings - Fork 2k
/
flapi.py
90 lines (68 loc) · 2.47 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import logging
import ckan.model as model
import ckan.logic as logic
from ckan.common import json, _, ungettext, c
from flask import abort, jsonify, request, Response, url_for
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.copy()
# TODO: Check and pop callback
# TODO: Switch to flask based route lookup ....
help = url_for('api',func_name="help_show", _external=True)
help = help + "?name={0}".format(func_name)
context = {'model':model, 'session': model.Session, 'user': c.user}
try:
response = fn(context, params)
except logic.NotFound, nfe:
error_dict = {
'__type': 'Not Found Error',
'message': 'Not found'
}
return_dict = {
'error': error_dict,
'success': False
}
log.info('Not Found error (Action API): %r' % str(nfe))
return jsonify(return_dict), 404
except logic.NotAuthorized, not_auth:
error_dict = not_auth.error_dict
error_dict['__type'] = 'Not Authorized Error',
return_dict = {
'error': error_dict,
'success': False
}
log.info('Not Authorized error (Action API): %r' % str(not_auth.error_dict))
return jsonify(return_dict), 403
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):
# Add help url
response = {
"success": True,
"result": response
}
if isinstance(response, basestring):
# Add help url
response = {
"success": True,
"result": response
}
response['help'] = help
return jsonify(response)
def post(self, func_name):
print "A POST request to", func_name
return "!"