2
2
3
3
import re
4
4
from typing import Any , Dict , List , Optional , Set , Union
5
- from urllib .parse import unquote_plus , urljoin
5
+ from urllib .parse import unquote_plus
6
6
7
7
import attr
8
8
import orjson
14
14
from pygeofilter .parsers .cql2_text import parse as parse_cql2_text
15
15
from pypgstac .hydration import hydrate
16
16
from stac_fastapi .api .models import JSONResponse
17
+ from stac_fastapi .extensions .core .collection_search .request import (
18
+ BaseCollectionSearchPostRequest ,
19
+ )
17
20
from stac_fastapi .types .core import AsyncBaseCoreClient
18
21
from stac_fastapi .types .errors import InvalidQueryParameter , NotFoundError
19
- from stac_fastapi .types .requests import get_base_url
20
22
from stac_fastapi .types .rfc3339 import DateTimeType
21
23
from stac_fastapi .types .stac import Collection , Collections , Item , ItemCollection
22
- from stac_pydantic .links import Relations
23
- from stac_pydantic .shared import BBox , MimeTypes
24
+ from stac_pydantic .shared import BBox
24
25
25
26
from stac_fastapi .pgstac .config import Settings
26
27
from stac_fastapi .pgstac .models .links import (
39
40
class CoreCrudClient (AsyncBaseCoreClient ):
40
41
"""Client for core endpoints defined by stac."""
41
42
43
+ collections_post_request_model : BaseCollectionSearchPostRequest = attr .ib (
44
+ default = BaseCollectionSearchPostRequest
45
+ )
46
+
42
47
async def all_collections ( # noqa: C901
43
48
self ,
44
49
request : Request ,
50
+ # Extensions
45
51
bbox : Optional [BBox ] = None ,
46
52
datetime : Optional [DateTimeType ] = None ,
47
53
limit : Optional [int ] = None ,
48
- # Extensions
49
54
query : Optional [str ] = None ,
50
55
token : Optional [str ] = None ,
51
56
fields : Optional [List [str ]] = None ,
@@ -62,13 +67,6 @@ async def all_collections( # noqa: C901
62
67
Collections which match the search criteria, returns all
63
68
collections by default.
64
69
"""
65
- query_params = str (request .query_params )
66
-
67
- # Kludgy fix because using factory does not allow alias for filter-lang
68
- if filter_lang is None :
69
- match = re .search (r"filter-lang=([a-z0-9-]+)" , query_params , re .IGNORECASE )
70
- if match :
71
- filter_lang = match .group (1 )
72
70
73
71
# Parse request parameters
74
72
base_args = {
@@ -89,7 +87,8 @@ async def all_collections( # noqa: C901
89
87
90
88
# Do the request
91
89
try :
92
- search_request = self .post_request_model (** clean )
90
+ search_request = self .collections_post_request_model (** clean )
91
+ # search_request = self.post_request_model(**clean)
93
92
except ValidationError as e :
94
93
raise HTTPException (
95
94
status_code = 400 , detail = f"Invalid parameters provided { e } "
@@ -102,9 +101,9 @@ async def _collection_search_base( # noqa: C901
102
101
search_request : PgstacSearch ,
103
102
request : Request ,
104
103
) -> Collections :
105
- """Cross catalog search (POST ).
104
+ """Cross catalog search (GET ).
106
105
107
- Called with `POST /search`.
106
+ Called with `GET /search`.
108
107
109
108
Args:
110
109
search_request: search request parameters.
@@ -113,16 +112,6 @@ async def _collection_search_base( # noqa: C901
113
112
All collections which match the search criteria.
114
113
"""
115
114
116
- base_url = get_base_url (request )
117
-
118
- settings : Settings = request .app .state .settings
119
-
120
- if search_request .datetime :
121
- search_request .datetime = format_datetime_range (search_request .datetime )
122
-
123
- search_request .conf = search_request .conf or {}
124
- search_request .conf ["nohydrate" ] = settings .use_api_hydrate
125
-
126
115
search_request_json = search_request .model_dump_json (
127
116
exclude_none = True , by_alias = True
128
117
)
@@ -141,8 +130,12 @@ async def _collection_search_base( # noqa: C901
141
130
f"Datetime parameter { search_request .datetime } is invalid."
142
131
) from e
143
132
144
- # next: Optional[str] = collections_result["links"].pop("next")
145
- # prev: Optional[str] = collections_result["links"].pop("prev")
133
+ next : Optional [str ] = None
134
+ prev : Optional [str ] = None
135
+
136
+ if links := collections_result .get ("links" ):
137
+ next = collections_result ["links" ].pop ("next" )
138
+ prev = collections_result ["links" ].pop ("prev" )
146
139
147
140
linked_collections : List [Collection ] = []
148
141
collections = collections_result ["collections" ]
@@ -167,32 +160,15 @@ async def _collection_search_base( # noqa: C901
167
160
168
161
linked_collections .append (coll )
169
162
170
- # paging_links = await PagingLinks(
171
- # request=request,
172
- # next=next,
173
- # prev=prev,
174
- # ).get_links()
175
-
176
- links = [
177
- {
178
- "rel" : Relations .root .value ,
179
- "type" : MimeTypes .json ,
180
- "href" : base_url ,
181
- },
182
- {
183
- "rel" : Relations .parent .value ,
184
- "type" : MimeTypes .json ,
185
- "href" : base_url ,
186
- },
187
- {
188
- "rel" : Relations .self .value ,
189
- "type" : MimeTypes .json ,
190
- "href" : urljoin (base_url , "collections" ),
191
- },
192
- ]
163
+ links = await PagingLinks (
164
+ request = request ,
165
+ next = next ,
166
+ prev = prev ,
167
+ ).get_links ()
168
+
193
169
return Collections (
194
170
collections = linked_collections or [],
195
- links = links , # + paging_links
171
+ links = links ,
196
172
)
197
173
198
174
async def get_collection (
0 commit comments