22
33import re
44from typing import Any , Dict , List , Optional , Set , Union
5- from urllib .parse import unquote_plus , urljoin
5+ from urllib .parse import unquote_plus
66
77import attr
88import orjson
1414from pygeofilter .parsers .cql2_text import parse as parse_cql2_text
1515from pypgstac .hydration import hydrate
1616from stac_fastapi .api .models import JSONResponse
17+ from stac_fastapi .extensions .core .collection_search .request import (
18+ BaseCollectionSearchPostRequest ,
19+ )
1720from stac_fastapi .types .core import AsyncBaseCoreClient
1821from stac_fastapi .types .errors import InvalidQueryParameter , NotFoundError
19- from stac_fastapi .types .requests import get_base_url
2022from stac_fastapi .types .rfc3339 import DateTimeType
2123from 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
2425
2526from stac_fastapi .pgstac .config import Settings
2627from stac_fastapi .pgstac .models .links import (
3940class CoreCrudClient (AsyncBaseCoreClient ):
4041 """Client for core endpoints defined by stac."""
4142
43+ collections_post_request_model : BaseCollectionSearchPostRequest = attr .ib (
44+ default = BaseCollectionSearchPostRequest
45+ )
46+
4247 async def all_collections ( # noqa: C901
4348 self ,
4449 request : Request ,
50+ # Extensions
4551 bbox : Optional [BBox ] = None ,
4652 datetime : Optional [DateTimeType ] = None ,
4753 limit : Optional [int ] = None ,
48- # Extensions
4954 query : Optional [str ] = None ,
5055 token : Optional [str ] = None ,
5156 fields : Optional [List [str ]] = None ,
@@ -62,13 +67,6 @@ async def all_collections( # noqa: C901
6267 Collections which match the search criteria, returns all
6368 collections by default.
6469 """
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 )
7270
7371 # Parse request parameters
7472 base_args = {
@@ -89,7 +87,8 @@ async def all_collections( # noqa: C901
8987
9088 # Do the request
9189 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)
9392 except ValidationError as e :
9493 raise HTTPException (
9594 status_code = 400 , detail = f"Invalid parameters provided { e } "
@@ -102,9 +101,9 @@ async def _collection_search_base( # noqa: C901
102101 search_request : PgstacSearch ,
103102 request : Request ,
104103 ) -> Collections :
105- """Cross catalog search (POST ).
104+ """Cross catalog search (GET ).
106105
107- Called with `POST /search`.
106+ Called with `GET /search`.
108107
109108 Args:
110109 search_request: search request parameters.
@@ -113,16 +112,6 @@ async def _collection_search_base( # noqa: C901
113112 All collections which match the search criteria.
114113 """
115114
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-
126115 search_request_json = search_request .model_dump_json (
127116 exclude_none = True , by_alias = True
128117 )
@@ -141,8 +130,12 @@ async def _collection_search_base( # noqa: C901
141130 f"Datetime parameter { search_request .datetime } is invalid."
142131 ) from e
143132
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" )
146139
147140 linked_collections : List [Collection ] = []
148141 collections = collections_result ["collections" ]
@@ -167,32 +160,15 @@ async def _collection_search_base( # noqa: C901
167160
168161 linked_collections .append (coll )
169162
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+
193169 return Collections (
194170 collections = linked_collections or [],
195- links = links , # + paging_links
171+ links = links ,
196172 )
197173
198174 async def get_collection (
0 commit comments