Skip to content

Commit

Permalink
Some more cleanup of the API controller
Browse files Browse the repository at this point in the history
  • Loading branch information
rossjones committed Sep 5, 2015
1 parent b750953 commit 79c87d3
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions ckan/controllers/flapi.py
Expand Up @@ -4,7 +4,7 @@
import ckan.logic as logic
from ckan.common import json, _, ungettext, c

from flask import abort, jsonify, request, Response
from flask import abort, jsonify, request, Response, url_for
from flask.views import MethodView

log = logging.getLogger(__name__)
Expand All @@ -24,10 +24,37 @@ def get(self, func_name):
params = request.args.copy()

# TODO: Check and pop callback
print c.user

# 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'
Expand All @@ -40,8 +67,19 @@ def get(self, func_name):
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')
# 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)

Expand Down

0 comments on commit 79c87d3

Please sign in to comment.