Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
add legacy properties (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
malmans2 authored Aug 27, 2024
1 parent 37d97ae commit 0ea4ff0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
27 changes: 15 additions & 12 deletions cads_api_client/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,16 +409,23 @@ def get_result_href(self) -> str:
assert isinstance(href, str)
return href

def get_result_size(self) -> Optional[int]:
asset = self.json.get("asset", {}).get("value", {})
size = asset["file:size"]
return int(size)
@property
def asset(self) -> dict[str, Any]:
return dict(self.json["asset"]["value"])

@property
def location(self) -> str:
result_href = self.get_result_href()
return urllib.parse.urljoin(self.response.url, result_href)

@property
def content_length(self) -> int:
return int(self.asset["file:size"])

@property
def content_type(self) -> str:
return str(self.asset["type"])

def download(
self,
target: Optional[str] = None,
Expand All @@ -438,14 +445,10 @@ def download(
multiurl.download(
url, stream=True, target=target, timeout=timeout, **retry_options
)
target_size = os.path.getsize(target)
size = self.get_result_size()
if size:
if target_size != size:
raise DownloadError(
"Download failed: downloaded %s byte(s) out of %s"
% (target_size, size)
)
if (target_size := os.path.getsize(target)) != (size := self.content_length):
raise DownloadError(
"Download failed: downloaded %s byte(s) out of %s" % (target_size, size)
)
return target

def info(self, *args: Any, **kwargs: Any) -> None:
Expand Down
3 changes: 3 additions & 0 deletions tests/integration_test_50_legacy_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def test_retrieve(tmp_path: pathlib.Path, api_root_url: str, api_anon_key: str)
response = requests.head(result.location)
assert response.status_code == 200

assert result.content_length == 1
assert result.content_type == "application/x-grib"


@pytest.mark.parametrize("quiet", [True, False])
def test_quiet(
Expand Down

0 comments on commit 0ea4ff0

Please sign in to comment.