Skip to content

Commit

Permalink
Merge pull request #1107 from dandi/gh-980
Browse files Browse the repository at this point in the history
get_content_url(): If a HEAD fails, return the failing URL
  • Loading branch information
yarikoptic committed Aug 24, 2022
2 parents 647b351 + f5a6693 commit 99fa604
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
29 changes: 16 additions & 13 deletions dandi/dandiapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1341,19 +1341,22 @@ def get_content_url(
raise NotFoundError(
"No matching URL found in asset's contentUrl metadata field"
)
if follow_redirects is True:
url = self.client.request(
"HEAD", url, json_resp=False, allow_redirects=True
).url
elif follow_redirects:
for _ in range(follow_redirects):
r = self.client.request(
"HEAD", url, json_resp=False, allow_redirects=False
)
if "Location" in r.headers:
url = r.headers["Location"]
else:
break
try:
if follow_redirects is True:
url = self.client.request(
"HEAD", url, json_resp=False, allow_redirects=True
).url
elif follow_redirects:
for _ in range(follow_redirects):
r = self.client.request(
"HEAD", url, json_resp=False, allow_redirects=False
)
if "Location" in r.headers:
url = r.headers["Location"]
else:
break
except requests.HTTPError as e:
url = e.request.url
if strip_query:
url = urlunparse(urlparse(url)._replace(query=""))
return url
Expand Down
10 changes: 10 additions & 0 deletions dandi/tests/test_dandiapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,16 @@ def test_get_content_url_follow_one_redirects_strip_query() -> None:
)


def test_get_content_url_follow_redirects_zarr(zarr_dandiset: SampleDandiset) -> None:
asset = zarr_dandiset.dandiset.get_asset_by_path("sample.zarr")
assert isinstance(asset, RemoteZarrAsset)
url = asset.get_content_url(follow_redirects=True, strip_query=True)
assert re.fullmatch(
f"http://localhost:9000/dandi-dandisets/zarr/{asset.zarr}/*",
url,
)


def test_remote_asset_json_dict(text_dandiset: SampleDandiset) -> None:
asset = text_dandiset.dandiset.get_asset_by_path("file.txt")
assert asset.json_dict() == {
Expand Down

0 comments on commit 99fa604

Please sign in to comment.