-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #668 support federation extension on
list_collections
- Loading branch information
Showing
8 changed files
with
174 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typing import List, Union | ||
|
||
|
||
class FederationExtension: | ||
""" | ||
Wrapper the openEO Federation extension as defined by | ||
https://github.com/Open-EO/openeo-api/tree/draft/extensions/federation | ||
""" | ||
|
||
__slots__ = ["_data"] | ||
|
||
def __init__(self, data: dict): | ||
self._data = data | ||
|
||
@property | ||
def missing(self) -> Union[List[str], None]: | ||
""" | ||
Get the ``federation:missing`` property (if any) of the resource, | ||
which lists back-ends that were not available during the request. | ||
:return: list of back-end IDs that were not available. | ||
Or None, when ``federation:missing`` is not present in response. | ||
""" | ||
return self._data.get("federation:missing", None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from dataclasses import dataclass | ||
from typing import List, Optional, Union | ||
|
||
from openeo.internal.jupyter import render_component | ||
from openeo.rest.models.federation_extension import FederationExtension | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Link: | ||
""" | ||
Container for (web) link data, used throughout the openEO API, | ||
to point to alternate representations, a license, extra detailed information, and more. | ||
""" | ||
|
||
rel: str | ||
href: str | ||
type: Optional[str] = None | ||
title: Optional[str] = None | ||
|
||
@classmethod | ||
def from_dict(cls, data: dict): | ||
return cls(rel=data["rel"], href=data["href"], type=data.get("type"), title=data.get("title")) | ||
|
||
# TODO: add _html_repr_ for Jupyter integration | ||
|
||
|
||
class CollectionListingResponse(list): | ||
""" | ||
Container for collection metadata listing received from a ``GET /collections`` request. | ||
This object mimics a list of collection metadata dictionaries, | ||
which was the original return API of :py:meth:`~openeo.rest.connection.Connection.list_collections()`, | ||
but now also includes additional metadata like links and extensions. | ||
:param data: response data from a ``GET /collections`` request | ||
""" | ||
|
||
__slots__ = ["_data"] | ||
|
||
def __init__(self, data: dict): | ||
self._data = data | ||
# Mimic original list of collection metadata dictionaries | ||
super().__init__(data["collections"]) | ||
|
||
def _repr_html_(self): | ||
return render_component(component="collections", data=self) | ||
|
||
@property | ||
def links(self) -> List[Link]: | ||
"""Get links from collections response.""" | ||
return [Link.from_dict(d) for d in self._data.get("links", [])] | ||
|
||
@property | ||
def ext_federation(self) -> FederationExtension: | ||
"""Accessor for federation extension data.""" | ||
return FederationExtension(self._data) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import pytest | ||
|
||
from openeo.rest.models.general import CollectionListingResponse, Link | ||
|
||
|
||
class TestLink: | ||
def test_basic(self): | ||
link = Link(rel="about", href="https://example.com/about") | ||
assert link.rel == "about" | ||
assert link.href == "https://example.com/about" | ||
assert link.title is None | ||
assert link.type is None | ||
|
||
def test_full(self): | ||
link = Link(rel="about", href="https://example.com/about", type="text/html", title="About example") | ||
assert link.rel == "about" | ||
assert link.href == "https://example.com/about" | ||
assert link.title == "About example" | ||
assert link.type == "text/html" | ||
|
||
def test_repr(self): | ||
link = Link(rel="about", href="https://example.com/about") | ||
assert repr(link) == "Link(rel='about', href='https://example.com/about', type=None, title=None)" | ||
|
||
|
||
class TestCollectionListingResponse: | ||
def test_basic(self): | ||
data = {"collections": [{"id": "S2"}, {"id": "S3"}]} | ||
collections = CollectionListingResponse(data) | ||
assert collections == [{"id": "S2"}, {"id": "S3"}] | ||
assert repr(collections) == "[{'id': 'S2'}, {'id': 'S3'}]" | ||
|
||
def test_links(self): | ||
data = { | ||
"collections": [{"id": "S2"}, {"id": "S3"}], | ||
"links": [ | ||
{"rel": "self", "href": "https://openeo.test/collections"}, | ||
{"rel": "next", "href": "https://openeo.test/collections?page=2"}, | ||
], | ||
} | ||
collections = CollectionListingResponse(data) | ||
assert collections.links == [ | ||
Link(rel="self", href="https://openeo.test/collections"), | ||
Link(rel="next", href="https://openeo.test/collections?page=2"), | ||
] | ||
|
||
@pytest.mark.parametrize( | ||
["data", "expected"], | ||
[ | ||
( | ||
{"collections": [{"id": "S2"}], "federation:missing": ["wwu"]}, | ||
["wwu"], | ||
), | ||
( | ||
{"collections": [{"id": "S2"}]}, | ||
None, | ||
), | ||
], | ||
) | ||
def test_federation_missing(self, data, expected): | ||
collections = CollectionListingResponse(data) | ||
assert collections.ext_federation.missing == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters