Skip to content

Stac fastapi 2.4.3 #127

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 21 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Added bbox and datetime parameters and functionality to item_collection https://github.com/stac-utils/stac-fastapi-elasticsearch/pull/127
- Added collection_id parameter to create_item function https://github.com/stac-utils/stac-fastapi-elasticsearch/pull/127
- Added item_id and collection_id to update_item https://github.com/stac-utils/stac-fastapi-elasticsearch/pull/127
- The default Collection objects index can be overridden by the `STAC_COLLECTIONS_INDEX` environment variable.
- The default Item objects index prefix can be overridden by the `STAC_ITEMS_INDEX_PREFIX` environment variable.


### Changed

- Updated core stac-fastapi libraries to 2.4.3 from 2.3.0 https://github.com/stac-utils/stac-fastapi-elasticsearch/pull/127

## [v0.2.0]

### Deprecated
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Elasticsearch backend for stac-fastapi.

**WIP** This backend does not have any production deployments yet, so use the pgstac backend instead if that's what you need.
Join our [Gitter](https://gitter.im/stac-fastapi-elasticsearch/community) page

For changes, see the [Changelog](CHANGELOG.md).

Expand Down
6 changes: 3 additions & 3 deletions stac_fastapi/elasticsearch/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"attrs",
"pydantic[dotenv]",
"stac_pydantic==2.0.*",
"stac-fastapi.types==2.3.0",
"stac-fastapi.api==2.3.0",
"stac-fastapi.extensions==2.3.0",
"stac-fastapi.types==2.4.3",
"stac-fastapi.api==2.4.3",
"stac-fastapi.extensions==2.4.3",
"elasticsearch[async]==7.17.8",
"elasticsearch-dsl==7.4.0",
"pystac[validation]",
Expand Down
73 changes: 51 additions & 22 deletions stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from urllib.parse import urljoin

import attr
import stac_pydantic.api
from fastapi import HTTPException
from overrides import overrides
from pydantic import ValidationError
Expand All @@ -21,7 +20,6 @@
from stac_fastapi.elasticsearch.models.links import PagingLinks
from stac_fastapi.elasticsearch.serializers import CollectionSerializer, ItemSerializer
from stac_fastapi.elasticsearch.session import Session
from stac_fastapi.extensions.core.filter.request import FilterLang
from stac_fastapi.extensions.third_party.bulk_transactions import (
BaseBulkTransactionsClient,
Items,
Expand All @@ -33,6 +31,7 @@
AsyncBaseTransactionsClient,
)
from stac_fastapi.types.links import CollectionLinks
from stac_fastapi.types.search import BaseSearchPostRequest
from stac_fastapi.types.stac import Collection, Collections, Item, ItemCollection

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -91,21 +90,49 @@ async def get_collection(self, collection_id: str, **kwargs) -> Collection:

@overrides
async def item_collection(
self, collection_id: str, limit: int = 10, token: str = None, **kwargs
self,
collection_id: str,
bbox: Optional[List[NumType]] = None,
datetime: Union[str, datetime_type, None] = None,
limit: int = 10,
token: str = None,
**kwargs,
) -> ItemCollection:
"""Read an item collection from the database."""
request: Request = kwargs["request"]
base_url = str(kwargs["request"].base_url)
base_url = str(request.base_url)

collection = await self.get_collection(
collection_id=collection_id, request=request
)
collection_id = collection.get("id")
if collection_id is None:
raise HTTPException(status_code=404, detail="Collection not found")

search = self.database.make_search()
search = self.database.apply_collections_filter(
search=search, collection_ids=[collection_id]
)

if datetime:
datetime_search = self._return_date(datetime)
search = self.database.apply_datetime_filter(
search=search, datetime_search=datetime_search
)

if bbox:
bbox = [float(x) for x in bbox]
if len(bbox) == 6:
bbox = [bbox[0], bbox[1], bbox[3], bbox[4]]

search = self.database.apply_bbox_filter(search=search, bbox=bbox)

items, maybe_count, next_token = await self.database.execute_search(
search=self.database.apply_collections_filter(
self.database.make_search(), [collection_id]
),
search=search,
limit=limit,
token=token,
sort=None,
token=token, # type: ignore
collection_ids=[collection_id],
ignore_unavailable=False,
)

items = [
Expand Down Expand Up @@ -236,7 +263,7 @@ async def get_search(

@overrides
async def post_search(
self, search_request: stac_pydantic.api.Search, **kwargs
self, search_request: BaseSearchPostRequest, **kwargs
) -> ItemCollection:
"""POST search catalog."""
request: Request = kwargs["request"]
Expand Down Expand Up @@ -280,14 +307,15 @@ async def post_search(
search=search, op=op, field=field, value=value
)

filter_lang = getattr(search_request, "filter_lang", None)

# only cql2_json is supported here
if hasattr(search_request, "filter"):
cql2_filter = getattr(search_request, "filter", None)
if filter_lang in [None, FilterLang.cql2_json]:
try:
search = self.database.apply_cql2_filter(search, cql2_filter)
else:
raise Exception("CQL2-Text is not supported with POST")
except Exception as e:
raise HTTPException(
status_code=400, detail=f"Error with cql2_json filter: {e}"
)

sort = None
if search_request.sortby:
Expand Down Expand Up @@ -358,7 +386,9 @@ class TransactionsClient(AsyncBaseTransactionsClient):
database = DatabaseLogic()

@overrides
async def create_item(self, item: stac_types.Item, **kwargs) -> stac_types.Item:
async def create_item(
self, collection_id: str, item: stac_types.Item, **kwargs
) -> stac_types.Item:
"""Create item."""
base_url = str(kwargs["request"].base_url)

Expand All @@ -369,8 +399,6 @@ async def create_item(self, item: stac_types.Item, **kwargs) -> stac_types.Item:
bulk_client.preprocess_item(item, base_url) for item in item["features"] # type: ignore
]

# not a great way to get the collection_id-- should be part of the method signature
collection_id = processed_items[0]["collection"]
await self.database.bulk_async(
collection_id, processed_items, refresh=kwargs.get("refresh", False)
)
Expand All @@ -382,18 +410,19 @@ async def create_item(self, item: stac_types.Item, **kwargs) -> stac_types.Item:
return item

@overrides
async def update_item(self, item: stac_types.Item, **kwargs) -> stac_types.Item:
async def update_item(
self, collection_id: str, item_id: str, item: stac_types.Item, **kwargs
) -> stac_types.Item:
"""Update item."""
base_url = str(kwargs["request"].base_url)
collection_id = item["collection"]

now = datetime_type.now(timezone.utc).isoformat().replace("+00:00", "Z")
item["properties"]["updated"] = str(now)

await self.database.check_collection_exists(collection_id)
# todo: index instead of delete and create
await self.delete_item(item_id=item["id"], collection_id=collection_id)
await self.create_item(item=item, **kwargs)
await self.delete_item(item_id=item_id, collection_id=collection_id)
await self.create_item(collection_id=collection_id, item=item, **kwargs)

return ItemSerializer.db_to_stac(item, base_url)

Expand Down
2 changes: 1 addition & 1 deletion stac_fastapi/elasticsearch/tests/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"POST /collections",
"POST /collections/{collection_id}/items",
"PUT /collections",
"PUT /collections/{collection_id}/items",
"PUT /collections/{collection_id}/items/{item_id}",
}


Expand Down
30 changes: 24 additions & 6 deletions stac_fastapi/elasticsearch/tests/clients/test_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ async def test_get_collection_items(app_client, ctx, core_client, txn_client):
for _ in range(num_of_items_to_create):
item = deepcopy(ctx.item)
item["id"] = str(uuid.uuid4())
await txn_client.create_item(item, request=MockRequest, refresh=True)
await txn_client.create_item(
collection_id=item["collection"],
item=item,
request=MockRequest,
refresh=True,
)

fc = await core_client.item_collection(coll["id"], request=MockRequest())
assert len(fc["features"]) == num_of_items_to_create + 1 # ctx.item
Expand All @@ -112,15 +117,24 @@ async def test_create_item(ctx, core_client, txn_client):

async def test_create_item_already_exists(ctx, txn_client):
with pytest.raises(ConflictError):
await txn_client.create_item(ctx.item, request=MockRequest, refresh=True)
await txn_client.create_item(
collection_id=ctx.item["collection"],
item=ctx.item,
request=MockRequest,
refresh=True,
)


async def test_update_item(ctx, core_client, txn_client):
ctx.item["properties"]["foo"] = "bar"
await txn_client.update_item(ctx.item, request=MockRequest)
collection_id = ctx.item["collection"]
item_id = ctx.item["id"]
await txn_client.update_item(
collection_id=collection_id, item_id=item_id, item=ctx.item, request=MockRequest
)

updated_item = await core_client.get_item(
ctx.item["id"], ctx.item["collection"], request=MockRequest
item_id, collection_id, request=MockRequest
)
assert updated_item["properties"]["foo"] == "bar"

Expand All @@ -137,10 +151,14 @@ async def test_update_geometry(ctx, core_client, txn_client):
]

ctx.item["geometry"]["coordinates"] = new_coordinates
await txn_client.update_item(ctx.item, request=MockRequest)
collection_id = ctx.item["collection"]
item_id = ctx.item["id"]
await txn_client.update_item(
collection_id=collection_id, item_id=item_id, item=ctx.item, request=MockRequest
)

updated_item = await core_client.get_item(
ctx.item["id"], ctx.item["collection"], request=MockRequest
item_id, collection_id, request=MockRequest
)
assert updated_item["geometry"]["coordinates"] == new_coordinates

Expand Down
15 changes: 14 additions & 1 deletion stac_fastapi/elasticsearch/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,20 @@ async def create_collection(txn_client: TransactionsClient, collection: Dict) ->


async def create_item(txn_client: TransactionsClient, item: Dict) -> None:
await txn_client.create_item(item, request=MockRequest, refresh=True)
if "collection" in item:
await txn_client.create_item(
collection_id=item["collection"],
item=item,
request=MockRequest,
refresh=True,
)
else:
await txn_client.create_item(
collection_id=item["features"][0]["collection"],
item=item,
request=MockRequest,
refresh=True,
)


async def delete_collections_and_items(txn_client: TransactionsClient) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async def test_create_and_delete_collection(app_client, load_test_data):
assert resp.status_code == 200

resp = await app_client.delete(f"/collections/{test_collection['id']}")
assert resp.status_code == 200
assert resp.status_code == 204


async def test_create_collection_conflict(app_client, ctx):
Expand Down
Loading