Skip to content

Commit

Permalink
minor fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Aug 22, 2020
1 parent 59bdc53 commit 1677069
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
0.0.6
- enh: allow to create the circle if it does not exist during dataset
creation
- fix: set "state" to "active" in dictionary returned by `create_dataset`
- fix: catch error when user uses `CKANAPI.get` when he should use
`CKANAPI.post`
0.0.5
- basic upload functional
0.0.4
Expand Down
5 changes: 4 additions & 1 deletion dcoraid/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ def get(self, api_call, **kwargs):
url_call = self.api_url + api_call
req = requests.get(url_call, headers=self.headers)
data = req.json()
if not data["success"]:
if isinstance(data, str):
raise ValueError(
"Command did not succeed, reason: '{}'".format(data))
elif not data["success"]:
raise ConnectionError(
"Could not run API call '{}'! ".format(url_call)
+ "Reason: {} ({})".format(req.reason,
Expand Down
11 changes: 10 additions & 1 deletion dcoraid/upload/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def activate_dataset(dataset_id, server, api_key):


def create_dataset(dataset_dict, server, api_key, resources=[],
activate=False):
create_circle=False, activate=False):
"""Create a draft dataset
Parameters
Expand All @@ -45,13 +45,21 @@ def create_dataset(dataset_dict, server, api_key, resources=[],
API key of the CKAN/DCOR user
resources: list of str or pathlib.Path
Paths to dataset resources
create_circle: bool
Create the circle if it does not already exist
activate: bool
If True, then the dataset state is changed to "active"
after uploading of the resources is complete. For DCOR,
this implies that no other resources can be added to the
dataset.
"""
api = CKANAPI(server=server, api_key=api_key)
if create_circle:
circles = [c["name"] for c in api.get("organization_list_for_user")]
if dataset_dict["owner_org"] not in circles:
# Create the circle before creating the dataset
api.post("organization_create",
data={"name": dataset_dict["owner_org"]})
dataset_dict = copy.deepcopy(dataset_dict)
dataset_dict["state"] = "draft"
data = api.post("package_create", dataset_dict)
Expand All @@ -63,6 +71,7 @@ def create_dataset(dataset_dict, server, api_key, resources=[],
api_key=api_key)
if activate:
activate_dataset(dataset_id=data["id"], server=server, api_key=api_key)
data["state"] = "active"
return data


Expand Down

0 comments on commit 1677069

Please sign in to comment.