Skip to content

Commit

Permalink
Fix #414 more robust billing currency/plans handling in capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Apr 7, 2023
1 parent 46bae17 commit d505757
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion openeo/capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
18 changes: 12 additions & 6 deletions openeo/rest/rest_capabilities.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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:
Expand All @@ -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})
16 changes: 16 additions & 0 deletions tests/rest/test_capabilities.py
Original file line number Diff line number Diff line change
@@ -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"}]

0 comments on commit d505757

Please sign in to comment.