-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcore.py
450 lines (367 loc) · 15.1 KB
/
core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
"""Item crud client."""
import re
from typing import Any, Dict, List, Optional, Set, Union
from urllib.parse import unquote_plus, urljoin
import attr
import orjson
from asyncpg.exceptions import InvalidDatetimeFormatError
from buildpg import render
from fastapi import HTTPException, Request
from pydantic import ValidationError
from pygeofilter.backends.cql2_json import to_cql2
from pygeofilter.parsers.cql2_text import parse as parse_cql2_text
from pypgstac.hydration import hydrate
from stac_fastapi.api.models import JSONResponse
from stac_fastapi.types.core import AsyncBaseCoreClient
from stac_fastapi.types.errors import InvalidQueryParameter, NotFoundError
from stac_fastapi.types.requests import get_base_url
from stac_fastapi.types.rfc3339 import DateTimeType
from stac_fastapi.types.stac import Collection, Collections, Item, ItemCollection
from stac_pydantic.links import Relations
from stac_pydantic.shared import BBox, MimeTypes
from stac_fastapi.pgstac.config import Settings
from stac_fastapi.pgstac.models.links import (
CollectionLinks,
ItemCollectionLinks,
ItemLinks,
PagingLinks,
)
from stac_fastapi.pgstac.types.search import PgstacSearch
from stac_fastapi.pgstac.utils import filter_fields, format_datetime_range
NumType = Union[float, int]
@attr.s
class CoreCrudClient(AsyncBaseCoreClient):
"""Client for core endpoints defined by stac."""
async def all_collections(self, request: Request, **kwargs) -> Collections:
"""Read all collections from the database."""
base_url = get_base_url(request)
async with request.app.state.get_connection(request, "r") as conn:
collections = await conn.fetchval(
"""
SELECT * FROM all_collections();
"""
)
linked_collections: List[Collection] = []
if collections is not None and len(collections) > 0:
for c in collections:
coll = Collection(**c)
coll["links"] = await CollectionLinks(
collection_id=coll["id"], request=request
).get_links(extra_links=coll.get("links"))
linked_collections.append(coll)
links = [
{
"rel": Relations.root.value,
"type": MimeTypes.json,
"href": base_url,
},
{
"rel": Relations.parent.value,
"type": MimeTypes.json,
"href": base_url,
},
{
"rel": Relations.self.value,
"type": MimeTypes.json,
"href": urljoin(base_url, "collections"),
},
]
collection_list = Collections(collections=linked_collections or [], links=links)
return collection_list
async def get_collection(
self, collection_id: str, request: Request, **kwargs
) -> Collection:
"""Get collection by id.
Called with `GET /collections/{collection_id}`.
Args:
collection_id: ID of the collection.
Returns:
Collection.
"""
collection: Optional[Dict[str, Any]]
async with request.app.state.get_connection(request, "r") as conn:
q, p = render(
"""
SELECT * FROM get_collection(:id::text);
""",
id=collection_id,
)
collection = await conn.fetchval(q, *p)
if collection is None:
raise NotFoundError(f"Collection {collection_id} does not exist.")
collection["links"] = await CollectionLinks(
collection_id=collection_id, request=request
).get_links(extra_links=collection.get("links"))
return Collection(**collection)
async def _get_base_item(
self, collection_id: str, request: Request
) -> Dict[str, Any]:
"""Get the base item of a collection for use in rehydrating full item collection properties.
Args:
collection: ID of the collection.
Returns:
Item.
"""
item: Optional[Dict[str, Any]]
async with request.app.state.get_connection(request, "r") as conn:
q, p = render(
"""
SELECT * FROM collection_base_item(:collection_id::text);
""",
collection_id=collection_id,
)
item = await conn.fetchval(q, *p)
if item is None:
raise NotFoundError(f"A base item for {collection_id} does not exist.")
return item
async def _search_base( # noqa: C901
self,
search_request: PgstacSearch,
request: Request,
) -> ItemCollection:
"""Cross catalog search (POST).
Called with `POST /search`.
Args:
search_request: search request parameters.
Returns:
ItemCollection containing items which match the search criteria.
"""
items: Dict[str, Any]
settings: Settings = request.app.state.settings
if search_request.datetime:
search_request.datetime = format_datetime_range(search_request.datetime)
search_request.conf = search_request.conf or {}
search_request.conf["nohydrate"] = settings.use_api_hydrate
search_request_json = search_request.model_dump_json(
exclude_none=True, by_alias=True
)
try:
async with request.app.state.get_connection(request, "r") as conn:
q, p = render(
"""
SELECT * FROM search(:req::text::jsonb);
""",
req=search_request_json,
)
items = await conn.fetchval(q, *p)
except InvalidDatetimeFormatError as e:
raise InvalidQueryParameter(
f"Datetime parameter {search_request.datetime} is invalid."
) from e
next: Optional[str] = items.pop("next", None)
prev: Optional[str] = items.pop("prev", None)
collection = ItemCollection(**items)
fields = getattr(search_request, "fields", None)
include: Set[str] = fields.include if fields and fields.include else set()
exclude: Set[str] = fields.exclude if fields and fields.exclude else set()
async def _add_item_links(
feature: Item,
collection_id: Optional[str] = None,
item_id: Optional[str] = None,
) -> None:
"""Add ItemLinks to the Item.
If the fields extension is excluding links, then don't add them.
Also skip links if the item doesn't provide collection and item ids.
"""
collection_id = feature.get("collection") or collection_id
item_id = feature.get("id") or item_id
if not exclude or "links" not in exclude and all([collection_id, item_id]):
feature["links"] = await ItemLinks(
collection_id=collection_id, # type: ignore
item_id=item_id, # type: ignore
request=request,
).get_links(extra_links=feature.get("links"))
cleaned_features: List[Item] = []
if settings.use_api_hydrate:
async def _get_base_item(collection_id: str) -> Dict[str, Any]:
return await self._get_base_item(collection_id, request=request)
base_item_cache = settings.base_item_cache(
fetch_base_item=_get_base_item, request=request
)
for feature in collection.get("features") or []:
base_item = await base_item_cache.get(feature.get("collection"))
# Exclude None values
base_item = {k: v for k, v in base_item.items() if v is not None}
feature = hydrate(base_item, feature)
# Grab ids needed for links that may be removed by the fields extension.
collection_id = feature.get("collection")
item_id = feature.get("id")
feature = filter_fields(feature, include, exclude)
await _add_item_links(feature, collection_id, item_id)
cleaned_features.append(feature)
else:
for feature in collection.get("features") or []:
await _add_item_links(feature)
cleaned_features.append(feature)
collection["features"] = cleaned_features
collection["links"] = await PagingLinks(
request=request,
next=next,
prev=prev,
).get_links()
return collection
async def item_collection(
self,
collection_id: str,
request: Request,
bbox: Optional[BBox] = None,
datetime: Optional[DateTimeType] = None,
limit: Optional[int] = None,
token: Optional[str] = None,
**kwargs,
) -> ItemCollection:
"""Get all items from a specific collection.
Called with `GET /collections/{collection_id}/items`
Args:
collection_id: id of the collection.
limit: number of items to return.
token: pagination token.
Returns:
An ItemCollection.
"""
# If collection does not exist, NotFoundError wil be raised
await self.get_collection(collection_id, request=request)
if datetime:
datetime = format_datetime_range(datetime)
base_args = {
"collections": [collection_id],
"bbox": bbox,
"datetime": datetime,
"limit": limit,
"token": token,
}
clean = {}
for k, v in base_args.items():
if v is not None and v != []:
clean[k] = v
search_request = self.post_request_model(**clean)
item_collection = await self._search_base(search_request, request=request)
links = await ItemCollectionLinks(
collection_id=collection_id, request=request
).get_links(extra_links=item_collection["links"])
item_collection["links"] = links
return item_collection
async def get_item(
self, item_id: str, collection_id: str, request: Request, **kwargs
) -> Item:
"""Get item by id.
Called with `GET /collections/{collection_id}/items/{item_id}`.
Args:
item_id: ID of the item.
collection_id: ID of the collection the item is in.
Returns:
Item.
"""
# If collection does not exist, NotFoundError wil be raised
await self.get_collection(collection_id, request=request)
search_request = self.post_request_model(
ids=[item_id], collections=[collection_id], limit=1
)
item_collection = await self._search_base(search_request, request=request)
if not item_collection["features"]:
raise NotFoundError(
f"Item {item_id} in Collection {collection_id} does not exist."
)
return Item(**item_collection["features"][0])
async def post_search(
self, search_request: PgstacSearch, request: Request, **kwargs
) -> ItemCollection:
"""Cross catalog search (POST).
Called with `POST /search`.
Args:
search_request: search request parameters.
Returns:
ItemCollection containing items which match the search criteria.
"""
item_collection = await self._search_base(search_request, request=request)
# If we have the `fields` extension enabled
# we need to avoid Pydantic validation because the
# Items might not be a valid STAC Item objects
if fields := getattr(search_request, "fields", None):
if fields.include or fields.exclude:
return JSONResponse(item_collection) # type: ignore
return ItemCollection(**item_collection)
async def get_search( # noqa: C901
self,
request: Request,
collections: Optional[List[str]] = None,
ids: Optional[List[str]] = None,
bbox: Optional[BBox] = None,
intersects: Optional[str] = None,
datetime: Optional[DateTimeType] = None,
limit: Optional[int] = None,
# Extensions
query: Optional[str] = None,
token: Optional[str] = None,
fields: Optional[List[str]] = None,
sortby: Optional[str] = None,
filter: Optional[str] = None,
filter_lang: Optional[str] = None,
**kwargs,
) -> ItemCollection:
"""Cross catalog search (GET).
Called with `GET /search`.
Returns:
ItemCollection containing items which match the search criteria.
"""
query_params = str(request.query_params)
# Kludgy fix because using factory does not allow alias for filter-lang
if filter_lang is None:
match = re.search(r"filter-lang=([a-z0-9-]+)", query_params, re.IGNORECASE)
if match:
filter_lang = match.group(1)
# Parse request parameters
base_args = {
"collections": collections,
"ids": ids,
"bbox": bbox,
"limit": limit,
"token": token,
"query": orjson.loads(unquote_plus(query)) if query else query,
}
if filter:
if filter_lang == "cql2-text":
ast = parse_cql2_text(filter)
base_args["filter"] = orjson.loads(to_cql2(ast))
base_args["filter-lang"] = "cql2-json"
if datetime:
base_args["datetime"] = format_datetime_range(datetime)
if intersects:
base_args["intersects"] = orjson.loads(unquote_plus(intersects))
if sortby:
# https://github.com/radiantearth/stac-spec/tree/master/api-spec/extensions/sort#http-get-or-post-form
sort_param = []
for sort in sortby:
sortparts = re.match(r"^([+-]?)(.*)$", sort)
if sortparts:
sort_param.append(
{
"field": sortparts.group(2).strip(),
"direction": "desc" if sortparts.group(1) == "-" else "asc",
}
)
base_args["sortby"] = sort_param
if fields:
includes = set()
excludes = set()
for field in fields:
if field[0] == "-":
excludes.add(field[1:])
elif field[0] == "+":
includes.add(field[1:])
else:
includes.add(field)
base_args["fields"] = {"include": includes, "exclude": excludes}
# Remove None values from dict
clean = {}
for k, v in base_args.items():
if v is not None and v != []:
clean[k] = v
# Do the request
try:
search_request = self.post_request_model(**clean)
except ValidationError as e:
raise HTTPException(
status_code=400, detail=f"Invalid parameters provided {e}"
) from e
return await self.post_search(search_request, request=request)