Commit 97c20ae3 authored by Denis Laxalde's avatar Denis Laxalde
Browse files

[hooks] Abstract error handling during CKAN POST requests in a function

parent 6e1d41bacacf
......@@ -26,33 +26,32 @@ from cubes.ckanpublish.utils import (ckan_post, CKANPostError,
ckan_instance_configured)
def _ckan_action(config, eid, action, data=None):
"""Run `ckan_post` and eventually raise ValidationError."""
try:
return ckan_post(config, action, data=data)
except (CKANPostError, RequestException) as exc:
raise ValidationError(eid, {'ckan_dataset_id': unicode(exc)})
def create_dataset(config, eid, data):
"""Create a CKAN dataset and set `ckan_dataset_id` attribute or
respective entity. Return the dataset id.
"""
try:
res = ckan_post(config, 'package_create', data)
return res['id']
except (CKANPostError, RequestException) as exc:
raise ValidationError(eid, {'ckan_dataset_id': unicode(exc)})
res = _ckan_action(config, eid, 'package_create', data)
return res['id']
def update_dataset(config, eid, datasetid, udata):
"""Update an existing CKAN dataset"""
try:
data = ckan_post(config, 'package_show', {'id': datasetid})
data.update(udata)
ckan_post(config, 'package_update', data)
except (CKANPostError, RequestException) as exc:
raise ValidationError(eid, {'ckan_dataset_id': unicode(exc)})
data = _ckan_action(config, eid, 'package_show', data={'id': datasetid})
data.update(udata)
_ckan_action(config, eid, 'package_update', data=data)
def delete_dataset(config, eid, datasetid):
"""Delete a CKAN dataset"""
try:
ckan_post(config, 'package_delete', {'id': datasetid})
except (CKANPostError, RequestException) as exc:
raise ValidationError(eid, {'ckan_dataset_id': unicode(exc)})
_ckan_action(config, eid, 'package_delete', data={'id': datasetid})
class DeleteCKANDataSetHook(hook.Hook):
......
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