From 1c91d7bd3190c32462c29905149587debb0da01b Mon Sep 17 00:00:00 2001 From: Mattia Almansi Date: Tue, 27 Feb 2024 11:33:45 +0100 Subject: [PATCH 01/15] add legacy client for beta release --- cads_api_client/legacy_client.py | 62 ++++++++++++++++++++++++++++++++ pyproject.toml | 2 +- 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 cads_api_client/legacy_client.py diff --git a/cads_api_client/legacy_client.py b/cads_api_client/legacy_client.py new file mode 100644 index 0000000..1e4b479 --- /dev/null +++ b/cads_api_client/legacy_client.py @@ -0,0 +1,62 @@ +from __future__ import annotations + +import functools +import warnings +from typing import Any, overload + +import cdsapi.legacy_client + +from . import api_client, processing + + +class LegacyClient(cdsapi.legacy_client.LegacyClient): # type: ignore[misc] + @classmethod + def raise_not_implemented_error(self) -> None: + raise NotImplementedError( + "Thi is a beta version. This functionality has not been implemented yet." + ) + + @functools.cached_property + def client(self) -> api_client.ApiClient: + warnings.warn( + "This is a beta version." + " `url`, `key`, and `session` are the only legacy parameters currently implemented." + " Any other parameter is silently ignored.", + UserWarning, + ) + return api_client.ApiClient(url=self.url, key=self.key, session=self.session) + + @overload + 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: + ... + + def retrieve( + self, name: str, request: dict[str, Any], target: str | None = None + ) -> str | processing.Remote: + collection = self.client.collection(name) + remote = collection.submit(**request) + return remote if target is None else remote.download(target) + + def service(self, name, *args, **kwargs): # type: ignore + self.raise_not_implemented_error() + + def workflow(self, code, *args, **kwargs): # type: ignore + self.raise_not_implemented_error() + + def status(self, context=None): # type: ignore + self.raise_not_implemented_error() + + def download(self, results, targets=None): # type: ignore + self.raise_not_implemented_error() + + def remote(self, url): # type: ignore + self.raise_not_implemented_error() + + def robust(self, call): # type: ignore + self.raise_not_implemented_error() diff --git a/pyproject.toml b/pyproject.toml index cd3805a..c8cacc4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,7 +30,7 @@ strict = true [[tool.mypy.overrides]] ignore_missing_imports = true -module = ["multiurl.*", "py.*"] +module = ["cdsapi.*", "multiurl.*", "py.*"] [tool.ruff] # Same as Black. From 6e29e9d7bd5ea9d1e6bfaca2b64a3593c18cf4c6 Mon Sep 17 00:00:00 2001 From: Mattia Almansi Date: Tue, 27 Feb 2024 11:45:16 +0100 Subject: [PATCH 02/15] typo --- cads_api_client/legacy_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cads_api_client/legacy_client.py b/cads_api_client/legacy_client.py index 1e4b479..cff0357 100644 --- a/cads_api_client/legacy_client.py +++ b/cads_api_client/legacy_client.py @@ -13,7 +13,7 @@ class LegacyClient(cdsapi.legacy_client.LegacyClient): # type: ignore[misc] @classmethod def raise_not_implemented_error(self) -> None: raise NotImplementedError( - "Thi is a beta version. This functionality has not been implemented yet." + "This is a beta version. This functionality has not been implemented yet." ) @functools.cached_property From e752224d7b65c3957fa0a1a462e0ae0c60417190 Mon Sep 17 00:00:00 2001 From: Mattia Almansi Date: Tue, 27 Feb 2024 13:24:47 +0100 Subject: [PATCH 03/15] renaming --- cads_api_client/{legacy_client.py => legacy_api_client.py} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename cads_api_client/{legacy_client.py => legacy_api_client.py} (93%) diff --git a/cads_api_client/legacy_client.py b/cads_api_client/legacy_api_client.py similarity index 93% rename from cads_api_client/legacy_client.py rename to cads_api_client/legacy_api_client.py index cff0357..cb9e614 100644 --- a/cads_api_client/legacy_client.py +++ b/cads_api_client/legacy_api_client.py @@ -4,12 +4,12 @@ import warnings from typing import Any, overload -import cdsapi.legacy_client +import cdsapi.abstract_legacy_client from . import api_client, processing -class LegacyClient(cdsapi.legacy_client.LegacyClient): # type: ignore[misc] +class LegacyApiClient(cdsapi.abstract_legacy_client.AbstractLegacyClient): # type: ignore[misc] @classmethod def raise_not_implemented_error(self) -> None: raise NotImplementedError( From 6bfbb336948a89255d035a1dbbf413fa62a6f7ce Mon Sep 17 00:00:00 2001 From: Mattia Almansi Date: Thu, 29 Feb 2024 12:58:13 +0100 Subject: [PATCH 04/15] import from cdsapi --- cads_api_client/legacy_api_client.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cads_api_client/legacy_api_client.py b/cads_api_client/legacy_api_client.py index cb9e614..cb79b93 100644 --- a/cads_api_client/legacy_api_client.py +++ b/cads_api_client/legacy_api_client.py @@ -4,12 +4,16 @@ import warnings from typing import Any, overload -import cdsapi.abstract_legacy_client +import cdsapi +import requests from . import api_client, processing -class LegacyApiClient(cdsapi.abstract_legacy_client.AbstractLegacyClient): # type: ignore[misc] +class LegacyApiClient(cdsapi.Client): # type: ignore[misc] + def _initialize_session(self, session: requests.Session) -> requests.Session: + return session + @classmethod def raise_not_implemented_error(self) -> None: raise NotImplementedError( From 8347c64f293a5fcab521703e995b337d0fb395bf Mon Sep 17 00:00:00 2001 From: Mattia Almansi Date: Thu, 29 Feb 2024 13:06:59 +0100 Subject: [PATCH 05/15] override __new__ --- cads_api_client/legacy_api_client.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cads_api_client/legacy_api_client.py b/cads_api_client/legacy_api_client.py index cb79b93..7c2fe5f 100644 --- a/cads_api_client/legacy_api_client.py +++ b/cads_api_client/legacy_api_client.py @@ -11,6 +11,10 @@ class LegacyApiClient(cdsapi.Client): # type: ignore[misc] + def __new__(cls, *args: Any, **kwargs: Any) -> Any: + instantiated: LegacyApiClient = super().__new__(cls) + return instantiated + def _initialize_session(self, session: requests.Session) -> requests.Session: return session From 054073fb9c3271a6ff24f9e737fc57c3f5c372de Mon Sep 17 00:00:00 2001 From: Mattia Almansi Date: Thu, 29 Feb 2024 13:10:34 +0100 Subject: [PATCH 06/15] no need to override __new__ --- cads_api_client/legacy_api_client.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/cads_api_client/legacy_api_client.py b/cads_api_client/legacy_api_client.py index 7c2fe5f..cb79b93 100644 --- a/cads_api_client/legacy_api_client.py +++ b/cads_api_client/legacy_api_client.py @@ -11,10 +11,6 @@ class LegacyApiClient(cdsapi.Client): # type: ignore[misc] - def __new__(cls, *args: Any, **kwargs: Any) -> Any: - instantiated: LegacyApiClient = super().__new__(cls) - return instantiated - def _initialize_session(self, session: requests.Session) -> requests.Session: return session From bee3dd7994e5d18299e415e284e4e2d5b3324037 Mon Sep 17 00:00:00 2001 From: Mattia Almansi Date: Thu, 29 Feb 2024 13:19:49 +0100 Subject: [PATCH 07/15] avoid recursion error --- cads_api_client/legacy_api_client.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cads_api_client/legacy_api_client.py b/cads_api_client/legacy_api_client.py index cb79b93..6e21262 100644 --- a/cads_api_client/legacy_api_client.py +++ b/cads_api_client/legacy_api_client.py @@ -11,6 +11,10 @@ class LegacyApiClient(cdsapi.Client): # type: ignore[misc] + def __new__(cls, *args: Any, **kwargs: Any) -> LegacyApiClient: + instantiated: LegacyApiClient = super().__new__(cls) + return instantiated + def _initialize_session(self, session: requests.Session) -> requests.Session: return session From 43865990cf766d1aef85bb6c700110256cb0ff33 Mon Sep 17 00:00:00 2001 From: Mattia Almansi Date: Thu, 29 Feb 2024 15:56:36 +0100 Subject: [PATCH 08/15] override __init__ --- cads_api_client/legacy_api_client.py | 46 ++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/cads_api_client/legacy_api_client.py b/cads_api_client/legacy_api_client.py index 6e21262..f6313b4 100644 --- a/cads_api_client/legacy_api_client.py +++ b/cads_api_client/legacy_api_client.py @@ -4,17 +4,51 @@ import warnings from typing import Any, overload -import cdsapi +import cdsapi.api import requests from . import api_client, processing - -class LegacyApiClient(cdsapi.Client): # type: ignore[misc] +LEGACY_KWARGS = [ + "quiet", + "debug", + "verify", + "timeout", + "progress", + "full_stack", + "delete", + "retry_max", + "sleep_max", + "wait_until_complete", + "info_callback", + "warning_callback", + "error_callback", + "debug_callback", + "metadata", + "forget", + "session", +] + + +class LegacyApiClient(cdsapi.api.Client): # type: ignore[misc] def __new__(cls, *args: Any, **kwargs: Any) -> LegacyApiClient: instantiated: LegacyApiClient = super().__new__(cls) return instantiated + def __init__( + self, url: str | None, key: str | None, *args: Any, **kwargs: Any + ) -> None: + kwargs.update(zip(LEGACY_KWARGS, args)) + + self.session = kwargs.pop("session", requests.Session()) + self.url, self.key, _ = cdsapi.api.get_url_key_verify(url, key, None) + + if kwargs: + warnings.warn( + f"This is a beta version, the following parameters are not implemented yet: {kwargs}", + UserWarning, + ) + def _initialize_session(self, session: requests.Session) -> requests.Session: return session @@ -26,12 +60,6 @@ def raise_not_implemented_error(self) -> None: @functools.cached_property def client(self) -> api_client.ApiClient: - warnings.warn( - "This is a beta version." - " `url`, `key`, and `session` are the only legacy parameters currently implemented." - " Any other parameter is silently ignored.", - UserWarning, - ) return api_client.ApiClient(url=self.url, key=self.key, session=self.session) @overload From 6b459ee0c412f8f3a52bfc569286053371d1410e Mon Sep 17 00:00:00 2001 From: Mattia Almansi Date: Thu, 29 Feb 2024 15:57:49 +0100 Subject: [PATCH 09/15] reorder --- cads_api_client/legacy_api_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cads_api_client/legacy_api_client.py b/cads_api_client/legacy_api_client.py index f6313b4..cd9d75f 100644 --- a/cads_api_client/legacy_api_client.py +++ b/cads_api_client/legacy_api_client.py @@ -40,8 +40,8 @@ def __init__( ) -> None: kwargs.update(zip(LEGACY_KWARGS, args)) - self.session = kwargs.pop("session", requests.Session()) self.url, self.key, _ = cdsapi.api.get_url_key_verify(url, key, None) + self.session = kwargs.pop("session", requests.Session()) if kwargs: warnings.warn( From 43bc8e8b57776816599d4f7ea0b5af5f8542b36f Mon Sep 17 00:00:00 2001 From: Mattia Almansi Date: Thu, 29 Feb 2024 16:07:16 +0100 Subject: [PATCH 10/15] better message --- cads_api_client/legacy_api_client.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cads_api_client/legacy_api_client.py b/cads_api_client/legacy_api_client.py index cd9d75f..c167809 100644 --- a/cads_api_client/legacy_api_client.py +++ b/cads_api_client/legacy_api_client.py @@ -45,7 +45,8 @@ def __init__( if kwargs: warnings.warn( - f"This is a beta version, the following parameters are not implemented yet: {kwargs}", + "This is a beta version." + f" The following parameters have not been implemented yet: {kwargs}.", UserWarning, ) From b96a3b03d442dcccfee7bd2df9f6e3f9a0491d02 Mon Sep 17 00:00:00 2001 From: Mattia Almansi Date: Thu, 29 Feb 2024 16:08:31 +0100 Subject: [PATCH 11/15] add default --- cads_api_client/legacy_api_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cads_api_client/legacy_api_client.py b/cads_api_client/legacy_api_client.py index c167809..f086c1d 100644 --- a/cads_api_client/legacy_api_client.py +++ b/cads_api_client/legacy_api_client.py @@ -36,7 +36,7 @@ def __new__(cls, *args: Any, **kwargs: Any) -> LegacyApiClient: return instantiated def __init__( - self, url: str | None, key: str | None, *args: Any, **kwargs: Any + self, url: str | None = None, key: str | None = None, *args: Any, **kwargs: Any ) -> None: kwargs.update(zip(LEGACY_KWARGS, args)) From 9a8a43d4e75a28fc01ad75909471b89292cb8336 Mon Sep 17 00:00:00 2001 From: Mattia Almansi Date: Thu, 29 Feb 2024 16:09:16 +0100 Subject: [PATCH 12/15] qa --- cads_api_client/legacy_api_client.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cads_api_client/legacy_api_client.py b/cads_api_client/legacy_api_client.py index f086c1d..196005a 100644 --- a/cads_api_client/legacy_api_client.py +++ b/cads_api_client/legacy_api_client.py @@ -36,7 +36,11 @@ def __new__(cls, *args: Any, **kwargs: Any) -> LegacyApiClient: return instantiated def __init__( - self, url: str | None = None, key: str | None = None, *args: Any, **kwargs: Any + self, + url: str | None = None, + key: str | None = None, + *args: Any, + **kwargs: Any, ) -> None: kwargs.update(zip(LEGACY_KWARGS, args)) From b365ec2ec234efb75cc058fe1e1e6109172333ee Mon Sep 17 00:00:00 2001 From: Mattia Almansi Date: Thu, 29 Feb 2024 16:10:44 +0100 Subject: [PATCH 13/15] cleanup --- cads_api_client/legacy_api_client.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/cads_api_client/legacy_api_client.py b/cads_api_client/legacy_api_client.py index 196005a..df039fc 100644 --- a/cads_api_client/legacy_api_client.py +++ b/cads_api_client/legacy_api_client.py @@ -54,9 +54,6 @@ def __init__( UserWarning, ) - def _initialize_session(self, session: requests.Session) -> requests.Session: - return session - @classmethod def raise_not_implemented_error(self) -> None: raise NotImplementedError( From 10eb3d48714d1d3a1b8015a9dd2a724ea371da33 Mon Sep 17 00:00:00 2001 From: Mattia Almansi Date: Thu, 29 Feb 2024 16:15:06 +0100 Subject: [PATCH 14/15] add logging methods --- cads_api_client/legacy_api_client.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/cads_api_client/legacy_api_client.py b/cads_api_client/legacy_api_client.py index df039fc..ee4c6a0 100644 --- a/cads_api_client/legacy_api_client.py +++ b/cads_api_client/legacy_api_client.py @@ -90,6 +90,18 @@ def workflow(self, code, *args, **kwargs): # type: ignore def status(self, context=None): # type: ignore self.raise_not_implemented_error() + def info(self, *args, **kwargs): # type: ignore + self.raise_not_implemented_error() + + def warning(self, *args, **kwargs): # type: ignore + self.raise_not_implemented_error() + + def error(self, *args, **kwargs): # type: ignore + self.raise_not_implemented_error() + + def debug(self, *args, **kwargs): # type: ignore + self.raise_not_implemented_error() + def download(self, results, targets=None): # type: ignore self.raise_not_implemented_error() From c59f6ba7befed795dc08411657ec2896a4e50a0c Mon Sep 17 00:00:00 2001 From: Mattia Almansi Date: Thu, 29 Feb 2024 16:39:54 +0100 Subject: [PATCH 15/15] use client retrieve --- cads_api_client/legacy_api_client.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/cads_api_client/legacy_api_client.py b/cads_api_client/legacy_api_client.py index ee4c6a0..fb6ad9e 100644 --- a/cads_api_client/legacy_api_client.py +++ b/cads_api_client/legacy_api_client.py @@ -1,6 +1,5 @@ from __future__ import annotations -import functools import warnings from typing import Any, overload @@ -46,6 +45,9 @@ def __init__( self.url, self.key, _ = cdsapi.api.get_url_key_verify(url, key, None) self.session = kwargs.pop("session", requests.Session()) + self.client = api_client.ApiClient( + url=self.url, key=self.key, session=self.session + ) if kwargs: warnings.warn( @@ -60,10 +62,6 @@ def raise_not_implemented_error(self) -> None: "This is a beta version. This functionality has not been implemented yet." ) - @functools.cached_property - def client(self) -> api_client.ApiClient: - return api_client.ApiClient(url=self.url, key=self.key, session=self.session) - @overload def retrieve(self, name: str, request: dict[str, Any], target: str) -> str: ... @@ -77,9 +75,10 @@ def retrieve( def retrieve( self, name: str, request: dict[str, Any], target: str | None = None ) -> str | processing.Remote: - collection = self.client.collection(name) - remote = collection.submit(**request) - return remote if target is None else remote.download(target) + if target is None: + collection = self.client.collection(name) + return collection.submit(**request) + return self.client.retrieve(name, target, **request) def service(self, name, *args, **kwargs): # type: ignore self.raise_not_implemented_error()