Skip to content

Commit

Permalink
add force option
Browse files Browse the repository at this point in the history
  • Loading branch information
epwalsh committed Mar 31, 2022
1 parent ddb081d commit 5456651
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions beaker/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,24 @@ def get_dataset(self, dataset_id: str) -> Dict[str, Any]:
"""
return self.request(
f"datasets/{dataset_id}", exceptions_for_status={404: DatasetNotFound(dataset_id)}
f"datasets/{urllib.parse.quote(dataset_id, safe='')}",
exceptions_for_status={404: DatasetNotFound(dataset_id)},
).json()

def delete_dataset(self, dataset_id: str):
self.request(
f"datasets/{urllib.parse.quote(dataset_id, safe='')}",
method="DELETE",
exceptions_for_status={404: DatasetNotFound(dataset_id)},
)

def create_dataset(
self,
name: str,
source: PathOrStr,
target: Optional[str] = None,
workspace: Optional[str] = None,
force: bool = False,
) -> Dict[str, Any]:
"""
Create a dataset with the source file(s).
Expand All @@ -219,13 +228,23 @@ def create_dataset(
raise NotImplementedError("'create_dataset()' only works for single files so far")

# Create the dataset.
dataset_info = self.request(
"datasets",
method="POST",
query={"name": name},
data={"workspace": workspace_name, "fileheap": True},
exceptions_for_status={409: DatasetConflict(name)},
).json()
def make_dataset() -> Dict[str, Any]:
return self.request(
"datasets",
method="POST",
query={"name": name},
data={"workspace": workspace_name, "fileheap": True},
exceptions_for_status={409: DatasetConflict(name)},
).json()

try:
dataset_info = make_dataset()
except DatasetConflict:
if force:
self.delete_dataset(f"{self.user}/{name}")
dataset_info = make_dataset()
else:
raise

# Upload the file.
with source.open("rb") as source_file:
Expand Down

0 comments on commit 5456651

Please sign in to comment.