Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ The `download` command retrieves Databus files, versions, artifacts, groups, col
<a id="cli-deploy"></a>
### Deploy

The `deploy` command publishes datasets using distribution arguments, metadata JSON files, or WebDAV/Nextcloud uploads. See the [deploy docs](doc/cli-usage.md#cli-deploy).
The `deploy` command publishes datasets using distribution arguments, metadata JSON files, or WebDAV/Nextcloud uploads, with configurable HTTP retries (`--max-retries`, `--request-timeout`). See the [deploy docs](doc/cli-usage.md#cli-deploy).

<a id="cli-delete"></a>
### Delete

The `delete` command removes Databus versions, artifacts, groups, or collections and provides dry-run and confirmation safeguards. See the [delete docs](doc/cli-usage.md#cli-delete).
The `delete` command removes Databus versions, artifacts, groups, or collections and provides dry-run, confirmation safeguards, and configurable HTTP retries (`--max-retries`, `--request-timeout`). See the [delete docs](doc/cli-usage.md#cli-delete).

<a id="cli-manifest"></a>
### Manifest
Expand Down
152 changes: 135 additions & 17 deletions databusclient/api/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from databusclient.api.utils import (
fetch_databus_jsonld,
get_databus_id_parts_from_file_url,
get_http_session,
)


Expand All @@ -23,16 +24,26 @@ class DeleteQueue:
Allows adding multiple databus URIs to a queue and executing their deletion in batch.
"""

def __init__(self, databus_key: str, manifest_context=None):
def __init__(
self,
databus_key: str,
manifest_context=None,
session: requests.Session | None = None,
timeout: int = 30,
):
"""Create a DeleteQueue bound to a given Databus API key.

Args:
databus_key: API key used to authenticate deletion requests.
manifest_context: Optional ManifestContext to record deletion
outcomes into. Passed through to _delete_list on execute().
session: Optional HTTP session with retry strategy.
timeout: Request timeout in seconds.
"""
self.databus_key = databus_key
self.manifest_context = manifest_context
self.session = session
self.timeout = timeout
self.queue: set[str] = set()

def add_uri(self, databusURI: str):
Expand Down Expand Up @@ -74,11 +85,18 @@ def execute(self):
Each queued URI will be deleted using `_delete_resource`.
Passes manifest_context through so deletions are recorded.
"""
del_kwargs = {
"force": True,
"manifest_context": self.manifest_context,
}
import inspect
if "session" in inspect.signature(_delete_list).parameters:
del_kwargs["session"] = self.session
del_kwargs["timeout"] = self.timeout
_delete_list(
list(self.sorted_queue()),
self.databus_key,
force=True,
manifest_context=self.manifest_context,
**del_kwargs,
)


Expand Down Expand Up @@ -122,6 +140,8 @@ def _delete_resource(
force: bool = False,
queue: DeleteQueue = None,
manifest_context=None,
session: requests.Session | None = None,
timeout: int = 30,
):
"""Delete a single Databus resource (version, artifact, group).

Expand All @@ -134,6 +154,8 @@ def _delete_resource(
dry_run: If True, do not perform the deletion but only print what would be deleted.
force: If True, skip confirmation prompt and proceed with deletion.
queue: If queue is provided, add the URI to the queue instead of deleting immediately.
session: Optional HTTP session to use for requests.
timeout: Request timeout in seconds.
"""

# Confirm the deletion request, skip the request or cancel deletion process
Expand All @@ -158,9 +180,12 @@ def _delete_resource(
queue.add_uri(databusURI)
return

if session is None:
session = get_http_session()

print(f"[DELETE] {databusURI}")
headers = {"accept": "*/*", "X-API-KEY": databus_key}
response = requests.delete(databusURI, headers=headers, timeout=30)
response = session.delete(databusURI, headers=headers, timeout=timeout)

if response.status_code in (200, 204):
print(f"Successfully deleted: {databusURI}")
Expand All @@ -179,6 +204,8 @@ def _delete_list(
force: bool = False,
queue: DeleteQueue = None,
manifest_context=None,
session: requests.Session | None = None,
timeout: int = 30,
):
"""Delete a list of Databus resources.

Expand All @@ -188,10 +215,19 @@ def _delete_list(
dry_run: If True, do not perform the deletion but only print what would be deleted.
force: If True, skip confirmation prompt and proceed with deletion.
queue: If queue is provided, add the URIs to the queue instead of deleting immediately.
session: Optional HTTP session with retry strategy.
timeout: Request timeout in seconds.
"""
for databusURI in databusURIs:
_delete_resource(
databusURI, databus_key, dry_run=dry_run, force=force, queue=queue, manifest_context=manifest_context
databusURI,
databus_key,
dry_run=dry_run,
force=force,
queue=queue,
manifest_context=manifest_context,
session=session,
timeout=timeout,
)


Expand All @@ -202,6 +238,8 @@ def _delete_artifact(
force: bool = False,
queue: DeleteQueue = None,
manifest_context=None,
session: requests.Session | None = None,
timeout: int = 30,
):
"""Delete an artifact and all its versions.

Expand All @@ -214,8 +252,10 @@ def _delete_artifact(
dry_run: If True, do not perform the deletion but only print what would be deleted.
force: If True, skip confirmation prompt and proceed with deletion.
queue: If queue is provided, add the URI to the queue instead of deleting immediately.
session: Optional HTTP session with retry strategy.
timeout: Request timeout in seconds.
"""
artifact_body = fetch_databus_jsonld(databusURI, databus_key)
artifact_body = fetch_databus_jsonld(databusURI, databus_key, session=session, timeout=timeout)

json_dict = json.loads(artifact_body)
versions = json_dict.get("databus:hasVersion")
Expand All @@ -235,11 +275,27 @@ def _delete_artifact(
else:
# Delete all versions
_delete_list(
version_uris, databus_key, dry_run=dry_run, force=force, queue=queue, manifest_context=manifest_context
version_uris,
databus_key,
dry_run=dry_run,
force=force,
queue=queue,
manifest_context=manifest_context,
session=session,
timeout=timeout,
)

# Finally, delete the artifact itself
_delete_resource(databusURI, databus_key, dry_run=dry_run, force=force, queue=queue,manifest_context=manifest_context)
_delete_resource(
databusURI,
databus_key,
dry_run=dry_run,
force=force,
queue=queue,
manifest_context=manifest_context,
session=session,
timeout=timeout,
)


def _delete_group(
Expand All @@ -249,6 +305,8 @@ def _delete_group(
force: bool = False,
queue: DeleteQueue = None,
manifest_context=None,
session: requests.Session | None = None,
timeout: int = 30,
):
"""Delete a group and all its artifacts and versions.

Expand All @@ -261,8 +319,10 @@ def _delete_group(
dry_run: If True, do not perform the deletion but only print what would be deleted.
force: If True, skip confirmation prompt and proceed with deletion.
queue: If queue is provided, add the URI to the queue instead of deleting immediately.
session: Optional HTTP session with retry strategy.
timeout: Request timeout in seconds.
"""
group_body = fetch_databus_jsonld(databusURI, databus_key)
group_body = fetch_databus_jsonld(databusURI, databus_key, session=session, timeout=timeout)

json_dict = json.loads(group_body)
artifacts = json_dict.get("databus:hasArtifact")
Expand Down Expand Up @@ -292,14 +352,38 @@ def _delete_group(
# Delete all artifacts (which deletes their versions)
for artifact_uri in artifact_uris:
_delete_artifact(
artifact_uri, databus_key, dry_run=dry_run, force=force, queue=queue, manifest_context=manifest_context
artifact_uri,
databus_key,
dry_run=dry_run,
force=force,
queue=queue,
manifest_context=manifest_context,
session=session,
timeout=timeout,
)

# Finally, delete the group itself
_delete_resource(databusURI, databus_key, dry_run=dry_run, force=force, queue=queue,manifest_context=manifest_context)
_delete_resource(
databusURI,
databus_key,
dry_run=dry_run,
force=force,
queue=queue,
manifest_context=manifest_context,
session=session,
timeout=timeout,
)


def delete(databusURIs: List[str], databus_key: str, dry_run: bool, force: bool, manifest_context=None):
def delete(
databusURIs: List[str],
databus_key: str,
dry_run: bool = False,
force: bool = False,
manifest_context=None,
session: requests.Session | None = None,
timeout: int = 30,
):
"""Delete a dataset from the databus.

Delete a group, artifact, or version identified by the given databus URI.
Expand All @@ -310,9 +394,15 @@ def delete(databusURIs: List[str], databus_key: str, dry_run: bool, force: bool,
databus_key: Databus API key to authenticate the deletion requests.
dry_run: If True, will only print what would be deleted without performing actual deletions.
force: If True, skip confirmation prompt and proceed with deletion.
session: Optional HTTP session with retry strategy.
timeout: Request timeout in seconds.
"""
if session is None:
session = get_http_session()

queue = DeleteQueue(databus_key, manifest_context=manifest_context)
queue = DeleteQueue(
databus_key, manifest_context=manifest_context, session=session, timeout=timeout
)

for databusURI in databusURIs:
_host, _account, group, artifact, version, file = (
Expand All @@ -322,24 +412,52 @@ def delete(databusURIs: List[str], databus_key: str, dry_run: bool, force: bool,
if group == "collections" and artifact is not None:
print(f"Deleting collection: {databusURI}")
_delete_resource(
databusURI, databus_key, dry_run=dry_run, force=force, queue=queue, manifest_context=manifest_context
databusURI,
databus_key,
dry_run=dry_run,
force=force,
queue=queue,
manifest_context=manifest_context,
session=session,
timeout=timeout,
)
elif file is not None:
print(f"Deleting file is not supported via API: {databusURI}")
elif version is not None:
print(f"Deleting version: {databusURI}")
_delete_resource(
databusURI, databus_key, dry_run=dry_run, force=force, queue=queue, manifest_context=manifest_context
databusURI,
databus_key,
dry_run=dry_run,
force=force,
queue=queue,
manifest_context=manifest_context,
session=session,
timeout=timeout,
)
elif artifact is not None:
print(f"Deleting artifact and all its versions: {databusURI}")
_delete_artifact(
databusURI, databus_key, dry_run=dry_run, force=force, queue=queue, manifest_context=manifest_context
databusURI,
databus_key,
dry_run=dry_run,
force=force,
queue=queue,
manifest_context=manifest_context,
session=session,
timeout=timeout,
)
elif group is not None and group != "collections":
print(f"Deleting group and all its artifacts and versions: {databusURI}")
_delete_group(
databusURI, databus_key, dry_run=dry_run, force=force, queue=queue, manifest_context=manifest_context
databusURI,
databus_key,
dry_run=dry_run,
force=force,
queue=queue,
manifest_context=manifest_context,
session=session,
timeout=timeout,
)
else:
print(f"Deleting {databusURI} is not supported.")
Expand Down
Loading