Skip to content

Commit 21d4f64

Browse files
authored
Merge pull request #91 from stac-utils/db_to_stac
Move db to stac serializer to core.py
2 parents a2e4efc + d4a4191 commit 21d4f64

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2222
- Elasticsearch index mappings updated to be more thorough.
2323
- Endpoints that return items (e.g., /search) now sort the results by 'properties.datetime,id,collection'.
2424
Previously, there was no sort order defined.
25+
- Db_to_stac serializer moved to core.py for consistency as it existed in both core and database_logic previously.
26+
- Use genexp in execute_search and get_all_collections to return results.
27+
- Added db_to_stac serializer to item_collection method in core.py.
2528

2629
### Removed
2730

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ class CoreClient(AsyncBaseCoreClient):
5252
async def all_collections(self, **kwargs) -> Collections:
5353
"""Read all collections from the database."""
5454
base_url = str(kwargs["request"].base_url)
55-
collection_list = await self.database.get_all_collections(base_url=base_url)
55+
collection_list = await self.database.get_all_collections()
56+
collection_list = [
57+
self.collection_serializer.db_to_stac(c, base_url=base_url)
58+
for c in collection_list
59+
]
5660

5761
links = [
5862
{
@@ -87,7 +91,7 @@ async def item_collection(
8791
) -> ItemCollection:
8892
"""Read an item collection from the database."""
8993
request: Request = kwargs["request"]
90-
base_url = str(request.base_url)
94+
base_url = str(kwargs["request"].base_url)
9195

9296
items, maybe_count, next_token = await self.database.execute_search(
9397
search=self.database.apply_collections_filter(
@@ -96,9 +100,12 @@ async def item_collection(
96100
limit=limit,
97101
token=token,
98102
sort=None,
99-
base_url=base_url,
100103
)
101104

105+
items = [
106+
self.item_serializer.db_to_stac(item, base_url=base_url) for item in items
107+
]
108+
102109
context_obj = None
103110
if self.extension_is_enabled("ContextExtension"):
104111
context_obj = {
@@ -269,9 +276,12 @@ async def post_search(
269276
limit=limit,
270277
token=search_request.token, # type: ignore
271278
sort=sort,
272-
base_url=base_url,
273279
)
274280

281+
items = [
282+
self.item_serializer.db_to_stac(item, base_url=base_url) for item in items
283+
]
284+
275285
# if self.extension_is_enabled("FieldsExtension"):
276286
# if search_request.query is not None:
277287
# query_include: Set[str] = set(

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import asyncio
33
import logging
44
from base64 import urlsafe_b64decode, urlsafe_b64encode
5-
from typing import Dict, List, Optional, Tuple, Type, Union
5+
from typing import Any, Dict, Iterable, List, Optional, Tuple, Type, Union
66

77
import attr
88
import elasticsearch
@@ -66,16 +66,12 @@ class DatabaseLogic:
6666

6767
"""CORE LOGIC"""
6868

69-
async def get_all_collections(self, base_url: str) -> List[Collection]:
69+
async def get_all_collections(self) -> List[Dict[str, Any]]:
7070
"""Database logic to retrieve a list of all collections."""
7171
# https://github.com/stac-utils/stac-fastapi-elasticsearch/issues/65
7272
# collections should be paginated, but at least return more than the default 10 for now
7373
collections = await self.client.search(index=COLLECTIONS_INDEX, size=1000)
74-
75-
return [
76-
self.collection_serializer.db_to_stac(c["_source"], base_url=base_url)
77-
for c in collections["hits"]["hits"]
78-
]
74+
return (c["_source"] for c in collections["hits"]["hits"])
7975

8076
async def get_one_item(self, collection_id: str, item_id: str) -> Dict:
8177
"""Database logic to retrieve a single item."""
@@ -194,8 +190,7 @@ async def execute_search(
194190
limit: int,
195191
token: Optional[str],
196192
sort: Optional[Dict[str, Dict[str, str]]],
197-
base_url: str,
198-
) -> Tuple[List[Item], Optional[int], Optional[str]]:
193+
) -> Tuple[Iterable[Dict[str, Any]], Optional[int], Optional[str]]:
199194
"""Database logic to execute search with limit."""
200195
search_after = None
201196
if token:
@@ -220,10 +215,7 @@ async def execute_search(
220215
es_response = await search_task
221216

222217
hits = es_response["hits"]["hits"]
223-
items = [
224-
self.item_serializer.db_to_stac(hit["_source"], base_url=base_url)
225-
for hit in hits
226-
]
218+
items = (hit["_source"] for hit in hits)
227219

228220
next_token = None
229221
if hits and (sort_array := hits[-1].get("sort")):

0 commit comments

Comments
 (0)