2626from urllib .parse import unquote_plus
2727
2828import attr
29+ import cql2
2930import orjson
31+ import pygeofilter
3032from fastapi import HTTPException
3133from pydantic import ValidationError
3234from pydantic_core import InitErrorDetails , PydanticCustomError
33- from pygeofilter .backends .cql2_json import to_cql2
34- from pygeofilter .parsers .cql2_json import parse as parse_json
35- from pygeofilter .parsers .cql2_text import parse as parse_cql2_text
3635from stac_fastapi .api .models import create_post_request_model
3736from stac_fastapi .types .errors import NotFoundError
3837from stac_fastapi .types .requests import get_base_url
@@ -202,7 +201,8 @@ async def all_collections(
202201 q : Optional [list [str ]] = None ,
203202 sortby : Optional [list [str ]] = None ,
204203 filter_expr : Optional [str ] = None ,
205- filter_lang : Optional [str ] = "cql2-text" ,
204+ filter_lang : Optional [str ] = None ,
205+ ** kwargs : Any ,
206206 ) -> Collections :
207207 """
208208 Get all collections from EODAG.
@@ -229,10 +229,11 @@ async def all_collections(
229229 cql2_json = None
230230 if filter_expr :
231231 if filter_lang == "cql2-text" :
232- filter_expr = to_cql2 (parse_cql2_text (filter_expr ))
233- filter_lang = "cql2-json"
234-
235- cql2_json = str2json ("filter_expr" , filter_expr )
232+ cql2_json = cql2 .parse_text (filter_expr ).to_json ()
233+ elif filter_lang == "cql2-json" :
234+ cql2_json = str2json ("filter_expr" , filter_expr )
235+ else :
236+ raise HTTPException (status_code = 400 , detail = f"Unsupported filter_lang { filter_lang } " )
236237
237238 collections = cast (
238239 CollectionsList ,
@@ -241,10 +242,10 @@ async def all_collections(
241242 geometry = bbox ,
242243 datetime = datetime ,
243244 limit = limit ,
244- q = q ,
245+ q = " " . join ( q ) if q else None ,
245246 cql2_json = cql2_json ,
246247 sortby = sortby ,
247- )
248+ ),
248249 )
249250
250251 number_matched = cast (int , collections .number_matched )
@@ -475,7 +476,7 @@ def _clean_search_args(
475476 """Clean up search arguments to match format expected by pgstac"""
476477 if filter_expr :
477478 if filter_lang == "cql2-text" :
478- filter_expr = to_cql2 ( parse_cql2_text ( filter_expr ))
479+ filter_expr = cql2 . parse_text ( filter_expr ). to_json ( )
479480 filter_lang = "cql2-json"
480481
481482 base_args ["filter" ] = str2json ("filter_expr" , filter_expr )
@@ -495,12 +496,10 @@ def _clean_search_args(
495496 for sort in sortby :
496497 sortparts = re .match (r"^([+-]?)(.*)$" , sort )
497498 if sortparts :
498- sort_param .append (
499- {
500- "field" : sortparts .group (2 ).strip (),
501- "direction" : "desc" if sortparts .group (1 ) == "-" else "asc" ,
502- }
503- )
499+ sort_param .append ({
500+ "field" : sortparts .group (2 ).strip (),
501+ "direction" : "desc" if sortparts .group (1 ) == "-" else "asc" ,
502+ })
504503 base_args ["sortby" ] = sort_param
505504
506505 # Remove None values from dict
@@ -645,7 +644,7 @@ def add_error(error_message: str) -> None:
645644
646645 errors : list [InitErrorDetails ] = []
647646 try :
648- parsing_result = EodagEvaluator ().evaluate (parse_json (filter_ )) # type: ignore
647+ parsing_result = EodagEvaluator ().evaluate (pygeofilter . parse (filter_ )) # type: ignore
649648 except (ValueError , NotImplementedError ) as e :
650649 add_error (str (e ))
651650 raise ValidationError .from_exception_data (title = "stac-fastapi-eodag" , line_errors = errors ) from e
0 commit comments