Commit ef890e89 authored by Denis Laxalde's avatar Denis Laxalde
Browse files

[utils] Handle JSON decode error in ckan_post

.json() may fail, see http://docs.python-requests.org/en/latest/user/quickstart/#json-response-content.
parent 0becfca6d171
......@@ -34,11 +34,17 @@ def ckan_post(config, action, data=None):
url = urljoin(base, 'api/3/action/' + action)
headers = {'Authorization': config['ckan-api-key'],
'Content-Type': 'application/json'}
r = requests.post(url, headers=headers, data=json.dumps(data or {}))
if not r.ok:
error = r.json()['error']
raise CKANPostError('action %s failed: %s' % (action, error))
return r.json()['result']
resp = requests.post(url, headers=headers, data=json.dumps(data or {}))
try:
jresp = resp.json()
except ValueError:
error = resp.text
else:
if resp.ok:
return jresp['result']
else:
error = jresp['error']
raise CKANPostError('action %s failed: %s' % (action, error))
@objectify_predicate
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment