9
9
import elasticsearch
10
10
from elasticsearch_dsl import Q , Search
11
11
from fastapi import HTTPException
12
+ from overrides import overrides
12
13
13
14
# from geojson_pydantic.geometries import Polygon
14
15
from pydantic import ValidationError
22
23
# from stac_fastapi.elasticsearch.types.error_checks import ErrorChecks
23
24
from stac_fastapi .types .core import BaseCoreClient
24
25
from stac_fastapi .types .errors import NotFoundError
25
- from stac_fastapi .types .search import BaseSearchPostRequest
26
26
from stac_fastapi .types .stac import Collection , Collections , Item , ItemCollection
27
27
28
28
logger = logging .getLogger (__name__ )
29
29
30
30
NumType = Union [float , int ]
31
31
32
+ ITEMS_INDEX = "stac_items"
33
+ COLLECTIONS_INDEX = "stac_collections"
34
+
32
35
33
36
@attr .s
34
37
class CoreCrudClient (BaseCoreClient ):
@@ -44,17 +47,13 @@ class CoreCrudClient(BaseCoreClient):
44
47
settings = ElasticsearchSettings ()
45
48
client = settings .create_client
46
49
47
- @staticmethod
48
- def _lookup_id (id : str , table , session ):
49
- """Lookup row by id."""
50
- pass
51
-
50
+ @overrides
52
51
def all_collections (self , ** kwargs ) -> Collections :
53
52
"""Read all collections from the database."""
54
53
base_url = str (kwargs ["request" ].base_url )
55
54
try :
56
55
collections = self .client .search (
57
- index = "stac_collections" , doc_type = "_doc" , query = {"match_all" : {}}
56
+ index = COLLECTIONS_INDEX , query = {"match_all" : {}}
58
57
)
59
58
except elasticsearch .exceptions .NotFoundError :
60
59
raise NotFoundError ("No collections exist" )
@@ -86,18 +85,20 @@ def all_collections(self, **kwargs) -> Collections:
86
85
)
87
86
return collection_list
88
87
88
+ @overrides
89
89
def get_collection (self , collection_id : str , ** kwargs ) -> Collection :
90
90
"""Get collection by id."""
91
91
base_url = str (kwargs ["request" ].base_url )
92
92
try :
93
- collection = self .client .get (index = "stac_collections" , id = collection_id )
93
+ collection = self .client .get (index = COLLECTIONS_INDEX , id = collection_id )
94
94
except elasticsearch .exceptions .NotFoundError :
95
95
raise NotFoundError (f"Collection { collection_id } not found" )
96
96
97
97
return self .collection_serializer .db_to_stac (collection ["_source" ], base_url )
98
98
99
+ @overrides
99
100
def item_collection (
100
- self , collection_id : str , limit : int = 10 , ** kwargs
101
+ self , collection_id : str , limit : int = 10 , token : str = None , ** kwargs
101
102
) -> ItemCollection :
102
103
"""Read an item collection from the database."""
103
104
links = []
@@ -136,11 +137,12 @@ def item_collection(
136
137
context = context_obj ,
137
138
)
138
139
140
+ @overrides
139
141
def get_item (self , item_id : str , collection_id : str , ** kwargs ) -> Item :
140
142
"""Get item by item id, collection id."""
141
143
base_url = str (kwargs ["request" ].base_url )
142
144
try :
143
- item = self .client .get (index = "stac_items" , id = item_id )
145
+ item = self .client .get (index = ITEMS_INDEX , id = item_id )
144
146
except elasticsearch .exceptions .NotFoundError :
145
147
raise NotFoundError (
146
148
f"Item { item_id } does not exist in Collection { collection_id } "
@@ -171,6 +173,7 @@ def _return_date(interval_str):
171
173
172
174
return {"lte" : end_date , "gte" : start_date }
173
175
176
+ @overrides
174
177
def get_search (
175
178
self ,
176
179
collections : Optional [List [str ]] = None ,
@@ -234,15 +237,13 @@ def bbox2poly(b0, b1, b2, b3):
234
237
poly = [[[b0 , b1 ], [b2 , b1 ], [b2 , b3 ], [b0 , b3 ], [b0 , b1 ]]]
235
238
return poly
236
239
237
- def post_search (
238
- self , search_request : BaseSearchPostRequest , ** kwargs
239
- ) -> ItemCollection :
240
+ def post_search (self , search_request : Search , ** kwargs ) -> ItemCollection :
240
241
"""POST search catalog."""
241
242
base_url = str (kwargs ["request" ].base_url )
242
243
search = (
243
244
Search ()
244
245
.using (self .client )
245
- .index ("stac_items" )
246
+ .index (ITEMS_INDEX )
246
247
.sort (
247
248
{"properties.datetime" : {"order" : "desc" }},
248
249
{"id" : {"order" : "desc" }},
0 commit comments