2
2
import re
3
3
from datetime import datetime
4
4
from typing import Any , Dict , List , Optional , Union
5
- from urllib .parse import urljoin
6
5
7
6
import attr
8
7
import orjson
13
12
from pygeofilter .backends .cql2_json import to_cql2
14
13
from pygeofilter .parsers .cql2_text import parse as parse_cql2_text
15
14
from pypgstac .hydration import hydrate
16
- from stac_pydantic .links import Relations
17
- from stac_pydantic .shared import MimeTypes
18
15
from starlette .requests import Request
19
16
20
17
from stac_fastapi .pgstac .config import Settings
23
20
from stac_fastapi .pgstac .utils import filter_fields
24
21
from stac_fastapi .types .core import AsyncBaseCoreClient
25
22
from stac_fastapi .types .errors import InvalidQueryParameter , NotFoundError
26
- from stac_fastapi .types .requests import get_base_url
27
23
from stac_fastapi .types .stac import Collection , Collections , Item , ItemCollection
28
24
29
25
NumType = Union [float , int ]
33
29
class CoreCrudClient (AsyncBaseCoreClient ):
34
30
"""Client for core endpoints defined by stac."""
35
31
36
- async def all_collections (self , ** kwargs ) -> Collections :
32
+ async def fetch_all_collections (self , request : Request ) -> Collections :
37
33
"""Read all collections from the database."""
38
- request : Request = kwargs ["request" ]
39
- base_url = get_base_url (request )
40
34
pool = request .app .state .readpool
41
35
42
36
async with pool .acquire () as conn :
43
- collections = await conn .fetchval (
37
+ return await conn .fetchval (
44
38
"""
45
39
SELECT * FROM all_collections();
46
40
"""
47
41
)
48
- linked_collections : List [Collection ] = []
49
- if collections is not None and len (collections ) > 0 :
50
- for c in collections :
51
- coll = Collection (** c )
52
- coll ["links" ] = await CollectionLinks (
53
- collection_id = coll ["id" ], request = request
54
- ).get_links (extra_links = coll .get ("links" ))
55
-
56
- linked_collections .append (coll )
57
-
58
- links = [
59
- {
60
- "rel" : Relations .root .value ,
61
- "type" : MimeTypes .json ,
62
- "href" : base_url ,
63
- },
64
- {
65
- "rel" : Relations .parent .value ,
66
- "type" : MimeTypes .json ,
67
- "href" : base_url ,
68
- },
69
- {
70
- "rel" : Relations .self .value ,
71
- "type" : MimeTypes .json ,
72
- "href" : urljoin (base_url , "collections" ),
73
- },
74
- ]
75
- collection_list = Collections (collections = linked_collections or [], links = links )
76
- return collection_list
77
-
78
- async def get_collection (self , collection_id : str , ** kwargs ) -> Collection :
42
+
43
+ async def fetch_collection (
44
+ self , collection_id : str , request : Request
45
+ ) -> Collection :
79
46
"""Get collection by id.
80
47
81
48
Called with `GET /collections/{collection_id}`.
@@ -86,9 +53,6 @@ async def get_collection(self, collection_id: str, **kwargs) -> Collection:
86
53
Returns:
87
54
Collection.
88
55
"""
89
- collection : Optional [Dict [str , Any ]]
90
-
91
- request : Request = kwargs ["request" ]
92
56
pool = request .app .state .readpool
93
57
async with pool .acquire () as conn :
94
58
q , p = render (
@@ -97,15 +61,7 @@ async def get_collection(self, collection_id: str, **kwargs) -> Collection:
97
61
""" ,
98
62
id = collection_id ,
99
63
)
100
- collection = await conn .fetchval (q , * p )
101
- if collection is None :
102
- raise NotFoundError (f"Collection { collection_id } does not exist." )
103
-
104
- collection ["links" ] = await CollectionLinks (
105
- collection_id = collection_id , request = request
106
- ).get_links (extra_links = collection .get ("links" ))
107
-
108
- return Collection (** collection )
64
+ return await conn .fetchval (q , * p )
109
65
110
66
async def _get_base_item (
111
67
self , collection_id : str , request : Request
@@ -245,7 +201,7 @@ async def _get_base_item(collection_id: str) -> Dict[str, Any]:
245
201
).get_links ()
246
202
return collection
247
203
248
- async def item_collection (
204
+ async def handle_collection_items (
249
205
self ,
250
206
collection_id : str ,
251
207
limit : Optional [int ] = None ,
@@ -265,7 +221,7 @@ async def item_collection(
265
221
An ItemCollection.
266
222
"""
267
223
# If collection does not exist, NotFoundError wil be raised
268
- await self .get_collection (collection_id , ** kwargs )
224
+ await self .handle_get_collection (collection_id , ** kwargs )
269
225
270
226
req = self .post_request_model (
271
227
collections = [collection_id ], limit = limit , token = token
@@ -277,7 +233,7 @@ async def item_collection(
277
233
item_collection ["links" ] = links
278
234
return item_collection
279
235
280
- async def get_item (self , item_id : str , collection_id : str , ** kwargs ) -> Item :
236
+ async def handle_get_item (self , item_id : str , collection_id : str , ** kwargs ) -> Item :
281
237
"""Get item by id.
282
238
283
239
Called with `GET /collections/{collection_id}/items/{item_id}`.
@@ -290,7 +246,7 @@ async def get_item(self, item_id: str, collection_id: str, **kwargs) -> Item:
290
246
Item.
291
247
"""
292
248
# If collection does not exist, NotFoundError wil be raised
293
- await self .get_collection (collection_id , ** kwargs )
249
+ await self .handle_get_collection (collection_id , ** kwargs )
294
250
295
251
req = self .post_request_model (
296
252
ids = [item_id ], collections = [collection_id ], limit = 1
@@ -303,7 +259,7 @@ async def get_item(self, item_id: str, collection_id: str, **kwargs) -> Item:
303
259
304
260
return Item (** item_collection ["features" ][0 ])
305
261
306
- async def post_search (
262
+ async def handle_post_search (
307
263
self , search_request : PgstacSearch , ** kwargs
308
264
) -> ItemCollection :
309
265
"""Cross catalog search (POST).
@@ -319,7 +275,7 @@ async def post_search(
319
275
item_collection = await self ._search_base (search_request , ** kwargs )
320
276
return ItemCollection (** item_collection )
321
277
322
- async def get_search (
278
+ async def handle_get_search (
323
279
self ,
324
280
collections : Optional [List [str ]] = None ,
325
281
ids : Optional [List [str ]] = None ,
@@ -408,4 +364,4 @@ async def get_search(
408
364
raise HTTPException (
409
365
status_code = 400 , detail = f"Invalid parameters provided { e } "
410
366
)
411
- return await self .post_search (search_request , request = kwargs ["request" ])
367
+ return await self .handle_post_search (search_request , request = kwargs ["request" ])
0 commit comments