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

Commit

Permalink
add delete option (#60)
Browse files Browse the repository at this point in the history
* add delete option

* add tests
  • Loading branch information
malmans2 authored Aug 27, 2024
1 parent 65c7f8f commit 37d97ae
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
2 changes: 2 additions & 0 deletions cads_api_client/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ApiClient:
url: Optional[str] = None
session: requests.Session = attrs.field(factory=requests.Session)
sleep_max: int = 120
cleanup: bool = False

def get_url(self) -> str:
return self.url or config.get_config("url")
Expand All @@ -39,6 +40,7 @@ def retrieve_api(self) -> processing.Processing:
headers=self._headers(),
session=self.session,
sleep_max=self.sleep_max,
cleanup=self.cleanup,
)

@functools.cached_property
Expand Down
3 changes: 3 additions & 0 deletions cads_api_client/legacy_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ 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.delete = kwargs.pop("delete", True)
self.client = api_client.ApiClient(
url=self.url,
key=self.key,
session=self.session,
sleep_max=self.sleep_max,
cleanup=self.delete,
)

self.timeout = kwargs.pop("timeout", 60)
Expand Down Expand Up @@ -115,6 +117,7 @@ def __init__(
"timeout": self.timeout,
"sleep_max": self.sleep_max,
"retry_max": self.retry_max,
"delete": self.delete,
},
)

Expand Down
41 changes: 32 additions & 9 deletions cads_api_client/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,15 @@ def __init__(
sleep_max: int = 120,
headers: Dict[str, Any] = {},
session: requests.Session = requests.api, # type: ignore
cleanup: bool = False,
):
self.url = url
self.sleep_max = sleep_max
self.headers = headers
self.session = session
self.cleanup = cleanup
self.log_start_time = None
logger.info(f"Request ID is {self.request_uid}")
self.info(f"Request ID is {self.request_uid}")

def log_metadata(self, metadata: dict[str, Any]) -> None:
logs = metadata.get("log", [])
Expand All @@ -213,9 +215,9 @@ def _get_reply(self, robust: bool, **retry_options: Any) -> dict[str, Any]:
if self.log_start_time:
params["logStartTime"] = self.log_start_time

logger.debug(f"GET {self.url}")
self.debug(f"GET {self.url}")
requests_response = get(url=self.url, headers=self.headers, params=params)
logger.debug(f"REPLY {requests_response.text}")
self.debug(f"REPLY {requests_response.text}")
requests_response.raise_for_status()
return dict(requests_response.json())

Expand All @@ -236,7 +238,7 @@ def wait_on_result(self, retry_options: Dict[str, Any] = {}) -> None:
status = None
while True:
if status != (status := self._robust_status(retry_options=retry_options)):
logger.info(f"status has been updated to {status}")
self.info(f"status has been updated to {status}")
if status == "successful":
break
elif status == "failed":
Expand All @@ -248,7 +250,7 @@ def wait_on_result(self, retry_options: Dict[str, Any] = {}) -> None:
sleep = self.sleep_max
else:
raise ProcessingFailedError(f"Unknown API state {status!r}")
logger.debug(f"result not ready, waiting for {sleep} seconds")
self.debug(f"result not ready, waiting for {sleep} seconds")
time.sleep(sleep)

def build_status_info(self) -> StatusInfo:
Expand All @@ -263,9 +265,9 @@ def make_results(self, url: Optional[str] = None) -> Results:
if status not in ("successful", "failed"):
raise ValueError(f"Result not ready, job is {status}")

logger.debug(f"GET {url}")
self.debug(f"GET {url}")
request_response = self.session.get(url, headers=self.headers)
logger.debug(f"REPLY {request_response.text}")
self.debug(f"REPLY {request_response.text}")

response = ApiResponse(request_response, session=self.session)
try:
Expand Down Expand Up @@ -301,6 +303,14 @@ def download(
target, timeout=timeout, retry_options=retry_options
)

def delete(self) -> dict[str, Any]:
self.debug(f"DELETE {self.url}")
requests_response = self.session.delete(url=self.url, headers=self.headers)
self.debug(f"REPLY {requests_response.text}")
requests_response.raise_for_status()
self.cleanup = False
return dict(requests_response.json())

def _warn(self) -> None:
message = (
".update and .reply are available for backward compatibility."
Expand Down Expand Up @@ -350,6 +360,13 @@ def error(self, *args: Any, **kwargs: Any) -> None:
def debug(self, *args: Any, **kwargs: Any) -> None:
logger.debug(*args, **kwargs)

def __del__(self) -> None:
if self.cleanup:
try:
self.delete()
except Exception as exc:
warnings.warn(str(exc), UserWarning)


@attrs.define
class StatusInfo(ApiResponse):
Expand Down Expand Up @@ -454,13 +471,15 @@ def __init__(
headers: Dict[str, Any] = {},
session: requests.Session = requests.api, # type: ignore
sleep_max: int = 120,
cleanup: bool = False,
) -> None:
if not force_exact_url:
url = f"{url}/{self.supported_api_version}"
self.url = url
self.headers = headers
self.session = session
self.sleep_max = sleep_max
self.cleanup = cleanup

def processes(self, params: Dict[str, Any] = {}) -> ProcessList:
url = f"{self.url}/processes"
Expand Down Expand Up @@ -518,7 +537,7 @@ def submit(
status_info = self.process_execute(
collection_id, request, retry_options=retry_options
)
return status_info.make_remote(sleep_max=self.sleep_max)
return status_info.make_remote(sleep_max=self.sleep_max, cleanup=self.cleanup)

def submit_and_wait_on_result(
self, collection_id: str, retry_options: Dict[str, Any] = {}, **request: Any
Expand All @@ -530,7 +549,11 @@ def submit_and_wait_on_result(
def make_remote(self, job_id: str) -> Remote:
url = f"{self.url}/jobs/{job_id}"
return Remote(
url, headers=self.headers, session=self.session, sleep_max=self.sleep_max
url,
headers=self.headers,
session=self.session,
sleep_max=self.sleep_max,
cleanup=self.cleanup,
)

def download_result(
Expand Down
15 changes: 15 additions & 0 deletions tests/integration_test_40_api_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import datetime

import pytest
import requests

from cads_api_client import ApiClient


Expand All @@ -16,3 +21,13 @@ def test_accept_licence() -> None:
licence["id"] == licence_id and licence["revision"] == licence_revision
for licence in client.accepted_licences["licences"]
)


def test_delete_request(api_anon_client: ApiClient) -> None:
remote = api_anon_client.submit(
"test-adaptor-dummy", _timestamp=datetime.datetime.now().isoformat()
)
reply = remote.delete()
assert reply["status"] == "dismissed"
with pytest.raises(requests.exceptions.HTTPError):
remote.status

0 comments on commit 37d97ae

Please sign in to comment.