Skip to content

Commit

Permalink
Merge pull request #781 from dandi/asset-created
Browse files Browse the repository at this point in the history
Give `RemoteAsset` a `created` attribute
  • Loading branch information
yarikoptic authored Oct 6, 2021
2 parents 8313173 + 4eee910 commit c9f00c7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
25 changes: 8 additions & 17 deletions dandi/dandiapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,18 +552,6 @@ def _mkasset(self, data: Dict[str, Any]) -> "RemoteAsset":
**data,
)

def _mkasset_from_metadata(self, metadata: Dict[str, Any]) -> "RemoteAsset":
return RemoteAsset(
client=self.client,
dandiset_id=self.identifier,
version_id=self.version_id,
identifier=metadata["identifier"],
path=metadata["path"],
size=metadata["contentSize"],
modified=metadata["dateModified"],
_metadata=metadata,
)

def json_dict(self) -> Dict[str, Any]:
"""
Convert to a JSONable `dict`, omitting the ``client`` attribute and
Expand Down Expand Up @@ -706,9 +694,10 @@ def get_asset(self, asset_id: str) -> "RemoteAsset":
ID. If the given asset does not exist, a `requests.HTTPError` is
raised with a 404 status code.
"""
return self._mkasset_from_metadata(
self.client.get(f"{self.version_api_path}assets/{asset_id}/")
)
metadata = self.client.get(f"{self.version_api_path}assets/{asset_id}/")
asset = self.get_asset_by_path(metadata["path"])
asset._metadata = metadata
return asset

def get_assets_with_path_prefix(self, path: str) -> Iterator["RemoteAsset"]:
"""
Expand Down Expand Up @@ -951,7 +940,6 @@ class BaseRemoteAsset(APIBase):
identifier: str = Field(alias="asset_id")
path: str
size: int
modified: datetime
#: Metadata supplied at initialization; returned when metadata is requested
#: instead of performing an API call
_metadata: Optional[Dict[str, Any]] = PrivateAttr(default_factory=None)
Expand All @@ -974,7 +962,6 @@ def _from_metadata(
identifier=metadata["identifier"],
path=metadata["path"],
size=metadata["contentSize"],
modified=metadata["dateModified"],
_metadata=metadata,
)

Expand Down Expand Up @@ -1116,6 +1103,10 @@ class RemoteAsset(BaseRemoteAsset):
#: The identifier for the version of the Dandiset to which the asset
#: belongs
version_id: str
#: The date at which the asset was created
created: datetime
#: The date at which the asset was last modified
modified: datetime

@property
def api_path(self) -> str:
Expand Down
15 changes: 15 additions & 0 deletions dandi/tests/test_dandiapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ def test_remote_asset_json_dict(text_dandiset):
assert asset.json_dict() == {
"asset_id": anys.ANY_STR,
"modified": anys.ANY_AWARE_DATETIME_STR,
"created": anys.ANY_AWARE_DATETIME_STR,
"path": anys.ANY_STR,
"size": anys.ANY_INT,
}
Expand Down Expand Up @@ -587,3 +588,17 @@ def test_refresh(text_dandiset):
assert dandiset.version_id == DRAFT
assert dandiset.version.modified > mtime
assert dandiset.most_recent_published_version is not None


def test_get_asset_with_and_without_metadata(mocker, text_dandiset):
path_asset = text_dandiset["dandiset"].get_asset_by_path("file.txt")
id_asset = text_dandiset["dandiset"].get_asset(path_asset.identifier)
assert path_asset == id_asset
assert path_asset._metadata is None
assert id_asset._metadata is not None
get_spy = mocker.spy(text_dandiset["client"], "get")
id_metadata = id_asset.get_raw_metadata()
get_spy.assert_not_called()
path_metadata = path_asset.get_raw_metadata()
get_spy.assert_called_once()
assert path_metadata == id_metadata

0 comments on commit c9f00c7

Please sign in to comment.