From a337060067adf5bcb54fb88ca9674e85349a1501 Mon Sep 17 00:00:00 2001 From: "John T. Wodder II" Date: Tue, 13 Apr 2021 09:45:17 -0400 Subject: [PATCH] Fix & test for downloading by asset ID URL --- dandi/download.py | 25 +++++++++++++++++++------ dandi/tests/test_download.py | 17 +++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/dandi/download.py b/dandi/download.py index bff6dfe87..be1c09cd1 100644 --- a/dandi/download.py +++ b/dandi/download.py @@ -163,12 +163,25 @@ def download_generator( elif isinstance(client, DandiAPIClient): # At least according to our 0.1.0 state of v1 # https://github.com/dandi/dandi-publish/issues/79 - down_args = ( - dandiset["dandiset"]["identifier"], - dandiset["version"], - asset["asset_id"], - ) - metadata = client.get_asset(*down_args) + if "asset_id" in asset_id: + down_args = ( + dandiset["dandiset"]["identifier"], + dandiset["version"], + asset_id["asset_id"], + ) + metadata = asset + asset = { + "path": metadata["path"], + "size": metadata["contentSize"], + "metadata": metadata, + } + else: + down_args = ( + dandiset["dandiset"]["identifier"], + dandiset["version"], + asset["asset_id"], + ) + metadata = client.get_asset(*down_args) d = metadata.get("digest", {}) if "dandi:dandi-etag" in d: digests = {"dandi-etag": d["dandi:dandi-etag"]} diff --git a/dandi/tests/test_download.py b/dandi/tests/test_download.py index 8c34631c0..3198ab10e 100644 --- a/dandi/tests/test_download.py +++ b/dandi/tests/test_download.py @@ -176,3 +176,20 @@ def test_download_item(local_dandi_api, text_dandiset, tmp_path): tmp_path / "coconut.txt" ] assert (tmp_path / "coconut.txt").read_text() == "Coconut\n" + + +def test_download_asset_id(local_dandi_api, text_dandiset, tmp_path): + dandiset_id = text_dandiset["dandiset_id"] + asset = local_dandi_api["client"].get_asset_bypath( + dandiset_id, "draft", "subdir2/coconut.txt" + ) + assert asset is not None + download( + f"{local_dandi_api['instance'].api}/dandisets/{dandiset_id}/versions" + f"/draft/assets/{asset['asset_id']}/download/", + tmp_path, + ) + assert list(map(Path, find_files(r".*", paths=[tmp_path], dirs=True))) == [ + tmp_path / "coconut.txt" + ] + assert (tmp_path / "coconut.txt").read_text() == "Coconut\n"