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

Commit

Permalink
add form and constraints properties to Collection (#114)
Browse files Browse the repository at this point in the history
* add form and constraints properties to Collection

* cleanup

* use private properties
  • Loading branch information
malmans2 authored Nov 7, 2024
1 parent 5d99da5 commit d2aef91
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 26 deletions.
40 changes: 34 additions & 6 deletions cads_api_client/catalogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def collection_ids(self) -> list[str]:
------
list[str]
"""
return [collection["id"] for collection in self.json["collections"]]
return [collection["id"] for collection in self._json_dict["collections"]]


@attrs.define
Expand All @@ -54,7 +54,7 @@ def begin_datetime(self) -> datetime.datetime | None:
-------
datetime.datetime or None
"""
if (value := self.json["extent"]["temporal"]["interval"][0][0]) is None:
if (value := self._json_dict["extent"]["temporal"]["interval"][0][0]) is None:
return value
return datetime.datetime.fromisoformat(value.replace("Z", "+00:00"))

Expand All @@ -66,7 +66,7 @@ def end_datetime(self) -> datetime.datetime | None:
-------
datetime.datetime or None
"""
if (value := self.json["extent"]["temporal"]["interval"][0][1]) is None:
if (value := self._json_dict["extent"]["temporal"]["interval"][0][1]) is None:
return value
return datetime.datetime.fromisoformat(value.replace("Z", "+00:00"))

Expand All @@ -78,7 +78,7 @@ def bbox(self) -> tuple[float, float, float, float]:
-------
tuple[float,float,float,float]
"""
return tuple(self.json["extent"]["spatial"]["bbox"][0])
return tuple(self._json_dict["extent"]["spatial"]["bbox"][0])

@property
def id(self) -> str:
Expand All @@ -88,7 +88,7 @@ def id(self) -> str:
-------
str
"""
return str(self.json["id"])
return str(self._json_dict["id"])

@property
def process(self) -> cads_api_client.Process:
Expand All @@ -102,6 +102,34 @@ def process(self) -> cads_api_client.Process:
url = self._get_link_href(rel="retrieve")
return cads_api_client.Process.from_request("get", url, **self._request_kwargs)

@property
def form(self) -> list[dict[str, Any]]:
"""
Form JSON.
Returns
-------
list[dict[str,Any]]
"""
url = f"{self.url}/form.json"
return ApiResponse.from_request(
"get", url, log_messages=False, **self._request_kwargs
)._json_list

@property
def constraints(self) -> list[dict[str, Any]]:
"""
Constraints JSON.
Returns
-------
list[dict[str,Any]]
"""
url = f"{self.url}/constraints.json"
return ApiResponse.from_request(
"get", url, log_messages=False, **self._request_kwargs
)._json_list

def submit(self, **request: Any) -> cads_api_client.Remote:
warnings.warn(
"`.submit` has been deprecated, and in the future will raise an error."
Expand Down Expand Up @@ -156,7 +184,7 @@ def get_licenses(self, **params: Any) -> dict[str, Any]:
response = ApiResponse.from_request(
"get", url, params=params, **self._request_kwargs
)
return response.json
return response._json_dict

@property
def messages(self) -> ApiResponse:
Expand Down
3 changes: 2 additions & 1 deletion cads_api_client/legacy_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ def workflow(self, code, *args, **kwargs):

def status(self, context: Any = None) -> dict[str, list[str]]:
status = collections.defaultdict(list)
for message in self.client._catalogue_api.messages.json.get("messages", []):
messages = self.client._catalogue_api.messages._json_dict.get("messages", [])
for message in messages:
status[message["severity"]].append(message["content"])
return dict(status)

Expand Down
44 changes: 28 additions & 16 deletions cads_api_client/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,35 @@ def url(self) -> str:
return str(self.response.request.url)

@property
def json(self) -> dict[str, Any]:
def json(self) -> Any:
"""Content of the response.
Returns
-------
dict[str,Any]
Any
"""
return dict(self.response.json())
return self.response.json()

@property
def _json_dict(self) -> dict[str, Any]:
assert isinstance(content := self.json, dict)
return content

@property
def _json_list(self) -> list[dict[str, Any]]:
assert isinstance(content := self.json, list)
return content

def log_messages(self) -> None:
if message_str := self.json.get("message"):
if message_str := self._json_dict.get("message"):
level, message_str = get_level_and_message(message_str)
self.log(level, message_str)

messages = self.json.get("messages", [])
messages = self._json_dict.get("messages", [])
dataset_messages = (
self.json.get("metadata", {}).get("datasetMetadata", {}).get("messages", [])
self._json_dict.get("metadata", {})
.get("datasetMetadata", {})
.get("messages", [])
)
for message_dict in messages + dataset_messages:
if not (content := message_dict.get("content")):
Expand All @@ -231,7 +243,7 @@ def log_messages(self) -> None:

def _get_links(self, rel: str | None = None) -> list[dict[str, str]]:
links = []
for link in self.json.get("links", []):
for link in self._json_dict.get("links", []):
if rel is not None and link.get("rel") == rel:
links.append(link)
return links
Expand Down Expand Up @@ -304,7 +316,7 @@ def process_ids(self) -> list[str]:
-------
list[str]
"""
return [proc["id"] for proc in self.json["processes"]]
return [proc["id"] for proc in self._json_dict["processes"]]


@attrs.define
Expand All @@ -319,7 +331,7 @@ def id(self) -> str:
-------
str
"""
process_id: str = self.json["id"]
process_id: str = self._json_dict["id"]
return process_id

def submit(self, **request: Any) -> cads_api_client.Remote:
Expand Down Expand Up @@ -361,7 +373,7 @@ def apply_constraints(self, **request: Any) -> dict[str, Any]:
json={"inputs": request},
**self._request_kwargs,
)
return response.json
return response._json_dict

def estimate_costs(self, **request: Any) -> dict[str, Any]:
"""Estimate costs of the parameters in a request.
Expand All @@ -382,7 +394,7 @@ def estimate_costs(self, **request: Any) -> dict[str, Any]:
json={"inputs": request},
**self._request_kwargs,
)
return response.json
return response._json_dict


@attrs.define(slots=False)
Expand Down Expand Up @@ -449,7 +461,7 @@ def json(self) -> dict[str, Any]:
params = {"log": True, "request": True}
if self.log_start_time:
params["logStartTime"] = self.log_start_time
return self._get_api_response("get", params=params).json
return self._get_api_response("get", params=params)._json_dict

@property
def collection_id(self) -> str:
Expand Down Expand Up @@ -542,7 +554,7 @@ def results_ready(self) -> bool:
return False
if status == "failed":
results = self.make_results(wait=False)
raise ProcessingFailedError(error_json_to_message(results.json))
raise ProcessingFailedError(error_json_to_message(results._json_dict))
if status in ("dismissed", "deleted"):
raise ProcessingFailedError(f"API state {status!r}")
raise ProcessingFailedError(f"Unknown API state {status!r}")
Expand Down Expand Up @@ -584,7 +596,7 @@ def delete(self) -> dict[str, Any]:
"""
response = self._get_api_response("delete")
self.cleanup = False
return response.json
return response._json_dict

def _warn(self) -> None:
message = (
Expand Down Expand Up @@ -669,7 +681,7 @@ def request_uids(self) -> list[str]:
-------
list[str]
"""
return [job["jobID"] for job in self.json["jobs"]]
return [job["jobID"] for job in self._json_dict["jobs"]]

@property
def job_ids(self) -> list[str]:
Expand Down Expand Up @@ -699,7 +711,7 @@ def asset(self) -> dict[str, Any]:
-------
dict[str,Any]
"""
return dict(self.json["asset"]["value"])
return dict(self._json_dict["asset"]["value"])

def _download(self, url: str, target: str) -> requests.Response:
download_options = {"stream": True, "resume_transfers": True}
Expand Down
8 changes: 5 additions & 3 deletions cads_api_client/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ def _get_api_response(

def accept_licence(self, licence_id: str, revision: int) -> dict[str, Any]:
url = f"{self.url}/account/licences/{licence_id}"
return self._get_api_response("put", url, json={"revision": revision}).json
return self._get_api_response(
"put", url, json={"revision": revision}
)._json_dict

def accepted_licences(self, **params: Any) -> dict[str, Any]:
url = f"{self.url}/account/licences"
return self._get_api_response("get", url, params=params).json
return self._get_api_response("get", url, params=params)._json_dict

def check_authentication(self) -> dict[str, Any]:
url = f"{self.url}/account/verification/pat"
return self._get_api_response("post", url).json
return self._get_api_response("post", url)._json_dict
25 changes: 25 additions & 0 deletions tests/integration_test_10_catalogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,28 @@ def test_catalogue_get_licences(api_anon_client: ApiClient) -> None:
assert portal_licences
for portal_licence in portal_licences:
assert portal_licence in all_licences


def test_catalogue_form(collection: Collection) -> None:
assert set(collection.form[0]) == {
"css",
"default",
"details",
"help",
"id",
"label",
"name",
"required",
"type",
}


def test_catalogue_constraints(collection: Collection) -> None:
assert set(collection.constraints[0]) == {
"day",
"month",
"product_type",
"time",
"variable",
"year",
}

0 comments on commit d2aef91

Please sign in to comment.