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

Commit

Permalink
Merge pull request #39 from ecmwf-projects/legacy-retry-options
Browse files Browse the repository at this point in the history
implement legacy retry options
  • Loading branch information
malmans2 authored Mar 6, 2024
2 parents 3a5e33f + 4c7e390 commit f73b356
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
36 changes: 28 additions & 8 deletions cads_api_client/legacy_api_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

import functools
import warnings
from typing import Any, overload
from typing import Any, Callable, overload

import cdsapi.api
import requests
Expand Down Expand Up @@ -45,10 +46,21 @@ def __init__(

self.url, self.key, _ = cdsapi.api.get_url_key_verify(url, key, None)
self.session = kwargs.pop("session", requests.Session())
self.sleep_max = kwargs.pop("sleep_max", 120)
self.client = api_client.ApiClient(
url=self.url, key=self.key, session=self.session
url=self.url,
key=self.key,
session=self.session,
sleep_max=self.sleep_max,
)

self.timeout = kwargs.pop("timeout", 60)
self.retry_max = kwargs.pop("retry_max", 500)
self.retry_options = {
"maximum_tries": self.retry_max,
"retry_after": self.sleep_max,
}

if kwargs:
warnings.warn(
"This is a beta version."
Expand All @@ -68,15 +80,23 @@ def retrieve(self, name: str, request: dict[str, Any], target: str) -> str: ...
@overload
def retrieve(
self, name: str, request: dict[str, Any], target: None = ...
) -> processing.Remote: ...
) -> processing.Results: ...

def retrieve(
self, name: str, request: dict[str, Any], target: str | None = None
) -> str | processing.Remote:
if target is None:
collection = self.client.collection(name)
return collection.submit(**request)
return self.client.retrieve(name, target, **request)
) -> str | processing.Results:
result = self.client.submit_and_wait_on_result(
collection_id=name,
retry_options=self.retry_options,
**request,
)
partial_download: Callable[..., str] = functools.partial(
result.download,
timeout=self.timeout,
retry_options=self.retry_options,
)
result.download = partial_download # type: ignore[method-assign]
return result if target is None else result.download(target)

def service(self, name, *args, **kwargs): # type: ignore
self.raise_not_implemented_error()
Expand Down
2 changes: 2 additions & 0 deletions ci/environment-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ dependencies:
- types-requests
- pip:
- responses
# FIXME: install from B-Open fork until the PR gets merged
- git+https://github.com/bopen/cdsapi.git@COPDS-1531-beta-api
21 changes: 21 additions & 0 deletions tests/integration_test_40_legacy_api_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pathlib

from cads_api_client import legacy_api_client


def test_retrieve(tmp_path: pathlib.Path, api_root_url: str, api_key: str) -> None:
client = legacy_api_client.LegacyApiClient(url=api_root_url, key=api_key)

collection_id = "test-adaptor-dummy"
request = {"size": 1}

target = tmp_path / "test-retrieve-with-target.grib"
actual_target = client.retrieve(collection_id, request, str(target))
assert str(target) == actual_target
assert target.stat().st_size == 1

result = client.retrieve(collection_id, request)
target = tmp_path / "test-retrieve-no-target.grib"
actual_target = result.download(str(target))
assert str(target) == actual_target
assert target.stat().st_size == 1

0 comments on commit f73b356

Please sign in to comment.