From d505757780b717ba7c6d3032c3b06b025f00aa99 Mon Sep 17 00:00:00 2001 From: Stefaan Lippens Date: Fri, 7 Apr 2023 12:01:46 +0200 Subject: [PATCH] Fix #414 more robust billing currency/plans handling in capabilities --- CHANGELOG.md | 2 ++ openeo/capabilities.py | 2 +- openeo/rest/rest_capabilities.py | 18 ++++++++++++------ tests/rest/test_capabilities.py | 16 ++++++++++++++++ 4 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 tests/rest/test_capabilities.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 8732a31bd..73d99d009 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `VectorCube.create_job()` and `MlModel.create_job()` are properly aligned with `DataCube.create_job()` regarding setting job title, description, etc. ([#412](https://github.com/Open-EO/openeo-python-client/issues/412)). +- More robust handling of billing currency/plans in capabilities + ([#414](https://github.com/Open-EO/openeo-python-client/issues/414)) ## [0.15.0] - 2023-03-03 diff --git a/openeo/capabilities.py b/openeo/capabilities.py index bc3454b63..0bc4a1270 100644 --- a/openeo/capabilities.py +++ b/openeo/capabilities.py @@ -4,7 +4,7 @@ from typing import Union, Tuple -# Is this base class (still) useful? +# TODO Is this base class (still) useful? class Capabilities(ABC): diff --git a/openeo/rest/rest_capabilities.py b/openeo/rest/rest_capabilities.py index 21d54b570..00922c261 100644 --- a/openeo/rest/rest_capabilities.py +++ b/openeo/rest/rest_capabilities.py @@ -1,5 +1,8 @@ +from typing import List, Optional + from openeo.capabilities import Capabilities from openeo.internal.jupyter import render_component +from openeo.util import deep_get class RESTCapabilities(Capabilities): @@ -13,6 +16,9 @@ def __init__(self, data: dict, url: str = None): def get(self, key: str, default=None): return self.capabilities.get(key, default) + def deep_get(self, *keys, default=None): + return deep_get(self.capabilities, *keys, default=default) + def api_version(self) -> str: """ Get openEO version.""" if 'api_version' in self.capabilities: @@ -36,13 +42,13 @@ def supports_endpoint(self, path: str, method="GET"): for endpoint in self.capabilities.get("endpoints", []) ) - def currency(self): - """ Get default billing currency.""" - return self.capabilities.get('billing', {}).get('currency') + def currency(self) -> Optional[str]: + """Get default billing currency.""" + return self.deep_get("billing", "currency", default=None) - def list_plans(self): - """ List all billing plans.""" - return self.capabilities.get('billing', {}).get('plans') + def list_plans(self) -> List[dict]: + """List all billing plans.""" + return self.deep_get("billing", "plans", default=[]) def _repr_html_(self): return render_component("capabilities", data = self.capabilities, parameters = {"url": self.url}) diff --git a/tests/rest/test_capabilities.py b/tests/rest/test_capabilities.py new file mode 100644 index 000000000..c5b8a3ae3 --- /dev/null +++ b/tests/rest/test_capabilities.py @@ -0,0 +1,16 @@ +from openeo.rest.rest_capabilities import RESTCapabilities + + +class TestCapabilities: + def test_currency(self): + assert RESTCapabilities({}).currency() is None + assert RESTCapabilities({"billing": None}).currency() is None + assert RESTCapabilities({"billing": {"currency": "EUR"}}).currency() == "EUR" + + def test_list_plans(self): + assert RESTCapabilities({}).list_plans() == [] + assert RESTCapabilities({"billing": None}).list_plans() == [] + assert RESTCapabilities({"billing": {"plans": []}}).list_plans() == [] + assert RESTCapabilities( + {"billing": {"plans": [{"name": "free"}]}} + ).list_plans() == [{"name": "free"}]