-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathcapabilities.py
More file actions
77 lines (60 loc) · 2.92 KB
/
capabilities.py
File metadata and controls
77 lines (60 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import re
from typing import Dict, List, Optional, Union
from openeo.internal.jupyter import render_component
from openeo.rest.models import federation_extension
from openeo.util import deep_get
from openeo.utils.version import ApiVersionException, ComparableVersion
__all__ = ["OpenEoCapabilities"]
CONFORMANCE_JWT_BEARER = re.compile(r"https://api\.openeo\.org/[^/]+/authentication/jwt")
class OpenEoCapabilities:
"""Container of the openEO capabilities document of an openEO backend."""
def __init__(self, data: dict, url: Optional[str] = None):
self.capabilities = data
self.url = url
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) -> Union[str, None]:
"""Version number of the openEO API specification this back-end implements."""
if "api_version" in self.capabilities:
return self.capabilities.get("api_version")
else:
# Legacy/deprecated
return self.capabilities.get("version")
@property
def api_version_check(self) -> ComparableVersion:
"""Helper to easily check if the API version is at least or below some threshold version."""
api_version = self.api_version()
if not api_version:
raise ApiVersionException("No API version found")
return ComparableVersion(api_version)
def has_conformance(self, uri: str) -> bool:
"""Check if backend provides a given conformance string"""
for conformance_uri in self.capabilities.get("conformsTo", []):
if re.fullmatch(uri, conformance_uri):
return True
return False
def supports_endpoint(self, path: str, method="GET") -> bool:
"""Check if backend supports given endpoint"""
return any(
endpoint.get("path") == path and method.upper() in endpoint.get("methods", [])
for endpoint in self.capabilities.get("endpoints", [])
)
def currency(self) -> Union[str, None]:
"""Get default billing currency."""
return self.deep_get("billing", "currency", default=None)
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})
def ext_federation_backend_details(self) -> Union[Dict[str, dict], None]:
"""
Lists all back-ends (with details, such as URL) that are part of the federation
if this backend acts as a federated backend,
as specified in the openEO Federation Extension.
Returns ``None`` otherwise.
.. versionadded:: 0.38.0
"""
return federation_extension.get_backend_details(data=self.capabilities)