-
Notifications
You must be signed in to change notification settings - Fork 115
Feature/add collection search extension V2 #736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
5 changes: 5 additions & 0 deletions
5
stac_fastapi/extensions/stac_fastapi/extensions/core/collection_search/__init__.py
This file contains hidden or 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,5 @@ | ||
"""Collection-Search extension module.""" | ||
|
||
from .collection_search import CollectionSearchExtension, ConformanceClasses | ||
|
||
__all__ = ["CollectionSearchExtension", "ConformanceClasses"] |
66 changes: 66 additions & 0 deletions
66
stac_fastapi/extensions/stac_fastapi/extensions/core/collection_search/collection_search.py
This file contains hidden or 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,66 @@ | ||
"""Collection-Search extension.""" | ||
|
||
from enum import Enum | ||
from typing import List, Optional | ||
|
||
import attr | ||
from fastapi import FastAPI | ||
|
||
from stac_fastapi.types.extension import ApiExtension | ||
|
||
from .request import CollectionSearchExtensionGetRequest | ||
|
||
|
||
class ConformanceClasses(str, Enum): | ||
"""Conformance classes for the Collection-Search extension. | ||
|
||
See | ||
https://github.com/stac-api-extensions/collection-search | ||
""" | ||
|
||
COLLECTIONSEARCH = "https://api.stacspec.org/v1.0.0-rc.1/collection-search" | ||
BASIS = "http://www.opengis.net/spec/ogcapi-common-2/1.0/conf/simple-query" | ||
FREETEXT = "https://api.stacspec.org/v1.0.0-rc.1/collection-search#free-text" | ||
FILTER = "https://api.stacspec.org/v1.0.0-rc.1/collection-search#filter" | ||
QUERY = "https://api.stacspec.org/v1.0.0-rc.1/collection-search#query" | ||
SORT = "https://api.stacspec.org/v1.0.0-rc.1/collection-search#sort" | ||
FIELDS = "https://api.stacspec.org/v1.0.0-rc.1/collection-search#fields" | ||
|
||
|
||
@attr.s | ||
class CollectionSearchExtension(ApiExtension): | ||
"""Collection-Search Extension. | ||
|
||
The Collection-Search extension adds functionality to the `GET - /collections` | ||
endpoint which allows the caller to include or exclude specific from the API | ||
response. | ||
Registering this extension with the application has the added effect of | ||
removing the `ItemCollection` response model from the `/search` endpoint, as | ||
the Fields extension allows the API to return potentially invalid responses | ||
by excluding fields which are required by the STAC spec, such as geometry. | ||
|
||
https://github.com/stac-api-extensions/collection-search | ||
|
||
Attributes: | ||
conformance_classes (list): Defines the list of conformance classes for | ||
the extension | ||
""" | ||
|
||
GET = CollectionSearchExtensionGetRequest | ||
POST = None | ||
|
||
conformance_classes: List[str] = attr.ib( | ||
default=[ConformanceClasses.COLLECTIONSEARCH, ConformanceClasses.BASIS] | ||
) | ||
schema_href: Optional[str] = attr.ib(default=None) | ||
|
||
def register(self, app: FastAPI) -> None: | ||
"""Register the extension with a FastAPI application. | ||
|
||
Args: | ||
app (fastapi.FastAPI): target FastAPI application. | ||
|
||
Returns: | ||
None | ||
""" | ||
pass |
27 changes: 27 additions & 0 deletions
27
stac_fastapi/extensions/stac_fastapi/extensions/core/collection_search/request.py
This file contains hidden or 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,27 @@ | ||
"""Request models for the Collection-Search extension.""" | ||
|
||
from typing import Optional | ||
|
||
import attr | ||
from fastapi import Query | ||
from stac_pydantic.shared import BBox | ||
from typing_extensions import Annotated | ||
|
||
from stac_fastapi.types.rfc3339 import DateTimeType | ||
from stac_fastapi.types.search import APIRequest, _bbox_converter, _datetime_converter | ||
|
||
|
||
@attr.s | ||
class CollectionSearchExtensionGetRequest(APIRequest): | ||
"""Basics additional Collection-Search parameters for the GET request.""" | ||
|
||
bbox: Optional[BBox] = attr.ib(default=None, converter=_bbox_converter) | ||
datetime: Optional[DateTimeType] = attr.ib( | ||
default=None, converter=_datetime_converter | ||
) | ||
limit: Annotated[ | ||
Optional[int], | ||
Query( | ||
description="Limits the number of results that are included in each page of the response." # noqa: E501 | ||
), | ||
] = attr.ib(default=10) |
181 changes: 181 additions & 0 deletions
181
stac_fastapi/extensions/tests/test_collection_search.py
This file contains hidden or 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,181 @@ | ||
import json | ||
from urllib.parse import quote_plus | ||
|
||
from starlette.testclient import TestClient | ||
|
||
from stac_fastapi.api.app import StacApi | ||
from stac_fastapi.api.models import create_request_model | ||
from stac_fastapi.extensions.core import CollectionSearchExtension | ||
from stac_fastapi.extensions.core.collection_search import ConformanceClasses | ||
from stac_fastapi.extensions.core.collection_search.request import ( | ||
CollectionSearchExtensionGetRequest, | ||
) | ||
from stac_fastapi.extensions.core.fields.request import FieldsExtensionGetRequest | ||
from stac_fastapi.extensions.core.filter.request import FilterExtensionGetRequest | ||
from stac_fastapi.extensions.core.free_text.request import FreeTextExtensionGetRequest | ||
from stac_fastapi.extensions.core.query.request import QueryExtensionGetRequest | ||
from stac_fastapi.extensions.core.sort.request import SortExtensionGetRequest | ||
from stac_fastapi.types.config import ApiSettings | ||
from stac_fastapi.types.core import BaseCoreClient | ||
|
||
|
||
class DummyCoreClient(BaseCoreClient): | ||
def all_collections(self, *args, **kwargs): | ||
_ = kwargs.pop("request", None) | ||
return kwargs | ||
|
||
def get_collection(self, *args, **kwargs): | ||
raise NotImplementedError | ||
|
||
def get_item(self, *args, **kwargs): | ||
raise NotImplementedError | ||
|
||
def get_search(self, *args, **kwargs): | ||
raise NotImplementedError | ||
|
||
def post_search(self, *args, **kwargs): | ||
return args[0].model_dump() | ||
|
||
def item_collection(self, *args, **kwargs): | ||
raise NotImplementedError | ||
|
||
|
||
def test_collection_search_extension_default(): | ||
"""Test /collections endpoint with collection-search ext.""" | ||
api = StacApi( | ||
settings=ApiSettings(), | ||
client=DummyCoreClient(), | ||
extensions=[CollectionSearchExtension()], | ||
collections_get_request_model=CollectionSearchExtensionGetRequest, | ||
) | ||
with TestClient(api.app) as client: | ||
response = client.get("/conformance") | ||
assert response.is_success, response.json() | ||
response_dict = response.json() | ||
assert ( | ||
"https://api.stacspec.org/v1.0.0-rc.1/collection-search" | ||
in response_dict["conformsTo"] | ||
) | ||
assert ( | ||
"http://www.opengis.net/spec/ogcapi-common-2/1.0/conf/simple-query" | ||
in response_dict["conformsTo"] | ||
) | ||
|
||
response = client.get("/collections") | ||
assert response.is_success, response.json() | ||
response_dict = response.json() | ||
assert "bbox" in response_dict | ||
assert "datetime" in response_dict | ||
assert "limit" in response_dict | ||
|
||
response = client.get( | ||
"/collections", | ||
params={ | ||
"datetime": "2020-06-13T13:00:00Z/2020-06-13T14:00:00Z", | ||
"bbox": "-175.05,-85.05,175.05,85.05", | ||
"limit": 100, | ||
}, | ||
) | ||
assert response.is_success, response.json() | ||
response_dict = response.json() | ||
assert [-175.05, -85.05, 175.05, 85.05] == response_dict["bbox"] | ||
assert [ | ||
"2020-06-13T13:00:00+00:00", | ||
"2020-06-13T14:00:00+00:00", | ||
] == response_dict["datetime"] | ||
assert 100 == response_dict["limit"] | ||
|
||
|
||
def test_collection_search_extension_models(): | ||
"""Test /collections endpoint with collection-search ext with additional models.""" | ||
collections_get_request_model = create_request_model( | ||
model_name="SearchGetRequest", | ||
base_model=CollectionSearchExtensionGetRequest, | ||
mixins=[ | ||
FreeTextExtensionGetRequest, | ||
FilterExtensionGetRequest, | ||
QueryExtensionGetRequest, | ||
SortExtensionGetRequest, | ||
FieldsExtensionGetRequest, | ||
], | ||
request_type="GET", | ||
) | ||
|
||
api = StacApi( | ||
settings=ApiSettings(), | ||
client=DummyCoreClient(), | ||
extensions=[ | ||
CollectionSearchExtension( | ||
conformance_classes=[ | ||
ConformanceClasses.COLLECTIONSEARCH, | ||
ConformanceClasses.BASIS, | ||
ConformanceClasses.FREETEXT, | ||
ConformanceClasses.FILTER, | ||
ConformanceClasses.QUERY, | ||
ConformanceClasses.SORT, | ||
ConformanceClasses.FIELDS, | ||
] | ||
) | ||
], | ||
collections_get_request_model=collections_get_request_model, | ||
) | ||
with TestClient(api.app) as client: | ||
response = client.get("/conformance") | ||
assert response.is_success, response.json() | ||
response_dict = response.json() | ||
conforms = response_dict["conformsTo"] | ||
assert "https://api.stacspec.org/v1.0.0-rc.1/collection-search" in conforms | ||
assert ( | ||
"http://www.opengis.net/spec/ogcapi-common-2/1.0/conf/simple-query" | ||
in conforms | ||
) | ||
assert ( | ||
"https://api.stacspec.org/v1.0.0-rc.1/collection-search#free-text" in conforms | ||
) | ||
assert "https://api.stacspec.org/v1.0.0-rc.1/collection-search#filter" in conforms | ||
assert "https://api.stacspec.org/v1.0.0-rc.1/collection-search#query" in conforms | ||
assert "https://api.stacspec.org/v1.0.0-rc.1/collection-search#sort" in conforms | ||
assert "https://api.stacspec.org/v1.0.0-rc.1/collection-search#fields" in conforms | ||
|
||
response = client.get("/collections") | ||
assert response.is_success, response.json() | ||
response_dict = response.json() | ||
assert "bbox" in response_dict | ||
assert "datetime" in response_dict | ||
assert "limit" in response_dict | ||
assert "q" in response_dict | ||
assert "filter" in response_dict | ||
assert "query" in response_dict | ||
assert "sortby" in response_dict | ||
assert "fields" in response_dict | ||
|
||
response = client.get( | ||
"/collections", | ||
params={ | ||
"datetime": "2020-06-13T13:00:00Z/2020-06-13T14:00:00Z", | ||
"bbox": "-175.05,-85.05,175.05,85.05", | ||
"limit": 100, | ||
"q": "EO,Earth Observation", | ||
"filter": "id='item_id' AND collection='collection_id'", | ||
"query": quote_plus( | ||
json.dumps({"eo:cloud_cover": {"gte": 95}}), | ||
), | ||
"sortby": "-gsd,-datetime", | ||
"fields": "properties.datetime", | ||
}, | ||
) | ||
assert response.is_success, response.json() | ||
response_dict = response.json() | ||
assert [-175.05, -85.05, 175.05, 85.05] == response_dict["bbox"] | ||
assert [ | ||
"2020-06-13T13:00:00+00:00", | ||
"2020-06-13T14:00:00+00:00", | ||
] == response_dict["datetime"] | ||
assert 100 == response_dict["limit"] | ||
assert ["EO", "Earth Observation"] == response_dict["q"] | ||
assert "id='item_id' AND collection='collection_id'" == response_dict["filter"] | ||
assert "filter_crs" in response_dict | ||
assert "cql2-text" in response_dict["filter_lang"] | ||
assert "query" in response_dict | ||
assert ["-gsd", "-datetime"] == response_dict["sortby"] | ||
assert ["properties.datetime"] == response_dict["fields"] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.