Skip to content

Commit 8141147

Browse files
committed
use verbose filter check
1 parent a4c796b commit 8141147

File tree

3 files changed

+143
-135
lines changed
  • stac_fastapi
    • core/stac_fastapi/core
    • elasticsearch/stac_fastapi/elasticsearch
    • opensearch/stac_fastapi/opensearch

3 files changed

+143
-135
lines changed

stac_fastapi/core/stac_fastapi/core/core.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -315,41 +315,41 @@ async def all_collections(
315315
detail=f"Only 'cql2-json' and 'cql2-text' filter languages are supported for collections. Got '{filter_lang}'.",
316316
)
317317

318-
# # Handle different filter formats
319-
# try:
320-
# if filter_lang == "cql2-text" or filter_lang is None:
321-
# # For cql2-text or when no filter_lang is specified, try both formats
322-
# try:
323-
# # First try to parse as JSON
324-
# parsed_filter = orjson.loads(unquote_plus(filter_expr))
325-
# except Exception:
326-
# # If that fails, use pygeofilter to convert CQL2-text to CQL2-JSON
327-
# try:
328-
# # Parse CQL2-text and convert to CQL2-JSON
329-
# text_filter = unquote_plus(filter_expr)
330-
# parsed_ast = parse_cql2_text(text_filter)
331-
# parsed_filter = to_cql2(parsed_ast)
332-
# except Exception as e:
333-
# # If parsing fails, provide a helpful error message
334-
# raise HTTPException(
335-
# status_code=400,
336-
# detail=f"Invalid CQL2-text filter: {e}. Please check your syntax.",
337-
# )
338-
# else:
339-
# # For explicit cql2-json, parse as JSON
340-
# parsed_filter = orjson.loads(unquote_plus(filter_expr))
341-
# except Exception as e:
342-
# # Catch any other parsing errors
343-
# raise HTTPException(
344-
# status_code=400, detail=f"Error parsing filter: {e}"
345-
# )
346-
347-
# Handle both cql2-json and cql2-text
348-
parsed_filter = orjson.loads(
349-
unquote_plus(filter_expr)
350-
if filter_lang == "cql2-json" or filter_lang is None
351-
else to_cql2(parse_cql2_text(filter_expr))
352-
)
318+
# Handle different filter formats
319+
try:
320+
if filter_lang == "cql2-text" or filter_lang is None:
321+
# For cql2-text or when no filter_lang is specified, try both formats
322+
try:
323+
# First try to parse as JSON
324+
parsed_filter = orjson.loads(unquote_plus(filter_expr))
325+
except Exception:
326+
# If that fails, use pygeofilter to convert CQL2-text to CQL2-JSON
327+
try:
328+
# Parse CQL2-text and convert to CQL2-JSON
329+
text_filter = unquote_plus(filter_expr)
330+
parsed_ast = parse_cql2_text(text_filter)
331+
parsed_filter = to_cql2(parsed_ast)
332+
except Exception as e:
333+
# If parsing fails, provide a helpful error message
334+
raise HTTPException(
335+
status_code=400,
336+
detail=f"Invalid CQL2-text filter: {e}. Please check your syntax.",
337+
)
338+
else:
339+
# For explicit cql2-json, parse as JSON
340+
parsed_filter = orjson.loads(unquote_plus(filter_expr))
341+
except Exception as e:
342+
# Catch any other parsing errors
343+
raise HTTPException(
344+
status_code=400, detail=f"Error parsing filter: {e}"
345+
)
346+
347+
# # Handle both cql2-json and cql2-text
348+
# parsed_filter = orjson.loads(
349+
# unquote_plus(filter_expr)
350+
# if filter_lang == "cql2-json" or filter_lang is None
351+
# else to_cql2(parse_cql2_text(filter_expr))
352+
# )
353353
except Exception as e:
354354
raise HTTPException(
355355
status_code=400, detail=f"Invalid filter parameter: {e}"

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/app.py

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@
121121

122122
extensions = [aggregation_extension] + search_extensions
123123

124+
# Collection search related variables
125+
collections_get_request_model = None
126+
124127
# Create collection search extensions if enabled
125128
if ENABLE_COLLECTIONS_SEARCH:
126129
# Create collection search extensions
@@ -140,57 +143,58 @@
140143
)
141144
collections_get_request_model = collection_search_ext.GET
142145

143-
# Create a post request model for collection search
144-
collection_search_post_request_model = create_post_request_model(
145-
collection_search_extensions
146-
)
146+
# Create a post request model for collection search
147+
collection_search_post_request_model = create_post_request_model(
148+
collection_search_extensions
149+
)
147150

148-
# Initialize collection search POST extension
149-
collection_search_post_ext = CollectionSearchPostExtension(
150-
client=CoreClient(
151-
database=database_logic,
152-
session=session,
153-
post_request_model=collection_search_post_request_model,
154-
landing_page_id=os.getenv("STAC_FASTAPI_LANDING_PAGE_ID", "stac-fastapi"),
155-
),
156-
settings=settings,
157-
POST=collection_search_post_request_model,
158-
conformance_classes=[
159-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search",
160-
"http://www.opengis.net/spec/ogcapi-common-2/1.0/conf/simple-query",
161-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search#filter",
162-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search#free-text",
163-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search#query",
164-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search#sort",
165-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search#fields",
166-
],
167-
)
151+
# Initialize collection search POST extension
152+
collection_search_post_ext = CollectionSearchPostExtension(
153+
client=CoreClient(
154+
database=database_logic,
155+
session=session,
156+
post_request_model=collection_search_post_request_model,
157+
landing_page_id=os.getenv("STAC_FASTAPI_LANDING_PAGE_ID", "stac-fastapi"),
158+
),
159+
settings=settings,
160+
POST=collection_search_post_request_model,
161+
conformance_classes=[
162+
"https://api.stacspec.org/v1.0.0-rc.1/collection-search",
163+
QueryConformanceClasses.COLLECTIONS,
164+
FilterConformanceClasses.COLLECTIONS,
165+
FreeTextConformanceClasses.COLLECTIONS,
166+
QueryConformanceClasses.COLLECTIONS,
167+
SortConformanceClasses.COLLECTIONS,
168+
FieldsConformanceClasses.COLLECTIONS,
169+
],
170+
)
168171

169-
# Initialize collections-search endpoint extension
170-
collections_search_endpoint_ext = CollectionsSearchEndpointExtension(
171-
client=CoreClient(
172-
database=database_logic,
173-
session=session,
174-
post_request_model=collection_search_post_request_model,
175-
landing_page_id=os.getenv("STAC_FASTAPI_LANDING_PAGE_ID", "stac-fastapi"),
176-
),
177-
settings=settings,
178-
GET=collections_get_request_model,
179-
POST=collection_search_post_request_model,
180-
conformance_classes=[
181-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search",
182-
"http://www.opengis.net/spec/ogcapi-common-2/1.0/conf/simple-query",
183-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search#filter",
184-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search#free-text",
185-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search#query",
186-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search#sort",
187-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search#fields",
188-
],
189-
)
172+
# Initialize collections-search endpoint extension
173+
collections_search_endpoint_ext = CollectionsSearchEndpointExtension(
174+
client=CoreClient(
175+
database=database_logic,
176+
session=session,
177+
post_request_model=collection_search_post_request_model,
178+
landing_page_id=os.getenv("STAC_FASTAPI_LANDING_PAGE_ID", "stac-fastapi"),
179+
),
180+
settings=settings,
181+
GET=collections_get_request_model,
182+
POST=collection_search_post_request_model,
183+
conformance_classes=[
184+
"https://api.stacspec.org/v1.0.0-rc.1/collection-search",
185+
QueryConformanceClasses.COLLECTIONS,
186+
FilterConformanceClasses.COLLECTIONS,
187+
FreeTextConformanceClasses.COLLECTIONS,
188+
QueryConformanceClasses.COLLECTIONS,
189+
SortConformanceClasses.COLLECTIONS,
190+
FieldsConformanceClasses.COLLECTIONS,
191+
],
192+
)
193+
194+
extensions.append(collection_search_ext)
195+
extensions.append(collection_search_post_ext)
196+
extensions.append(collections_search_endpoint_ext)
190197

191-
extensions.append(collection_search_ext)
192-
extensions.append(collection_search_post_ext)
193-
extensions.append(collections_search_endpoint_ext)
194198

195199
database_logic.extensions = [type(ext).__name__ for ext in extensions]
196200

@@ -230,8 +234,8 @@
230234
"route_dependencies": get_route_dependencies(),
231235
}
232236

233-
# Add collections_get_request_model if collection search is enabled
234-
if ENABLE_COLLECTIONS_SEARCH:
237+
# Add collections_get_request_model if it was created
238+
if collections_get_request_model:
235239
app_config["collections_get_request_model"] = collections_get_request_model
236240

237241
api = StacApi(**app_config)

stac_fastapi/opensearch/stac_fastapi/opensearch/app.py

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@
121121

122122
extensions = [aggregation_extension] + search_extensions
123123

124+
# Collection search related variables
125+
collections_get_request_model = None
126+
124127
# Create collection search extensions if enabled
125128
if ENABLE_COLLECTIONS_SEARCH:
126129
# Create collection search extensions
@@ -140,57 +143,58 @@
140143
)
141144
collections_get_request_model = collection_search_ext.GET
142145

143-
# Create a post request model for collection search
144-
collection_search_post_request_model = create_post_request_model(
145-
collection_search_extensions
146-
)
146+
# Create a post request model for collection search
147+
collection_search_post_request_model = create_post_request_model(
148+
collection_search_extensions
149+
)
147150

148-
# Initialize collection search POST extension
149-
collection_search_post_ext = CollectionSearchPostExtension(
150-
client=CoreClient(
151-
database=database_logic,
152-
session=session,
153-
post_request_model=collection_search_post_request_model,
154-
landing_page_id=os.getenv("STAC_FASTAPI_LANDING_PAGE_ID", "stac-fastapi"),
155-
),
156-
settings=settings,
157-
POST=collection_search_post_request_model,
158-
conformance_classes=[
159-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search",
160-
"http://www.opengis.net/spec/ogcapi-common-2/1.0/conf/simple-query",
161-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search#filter",
162-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search#free-text",
163-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search#query",
164-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search#sort",
165-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search#fields",
166-
],
167-
)
151+
# Initialize collection search POST extension
152+
collection_search_post_ext = CollectionSearchPostExtension(
153+
client=CoreClient(
154+
database=database_logic,
155+
session=session,
156+
post_request_model=collection_search_post_request_model,
157+
landing_page_id=os.getenv("STAC_FASTAPI_LANDING_PAGE_ID", "stac-fastapi"),
158+
),
159+
settings=settings,
160+
POST=collection_search_post_request_model,
161+
conformance_classes=[
162+
"https://api.stacspec.org/v1.0.0-rc.1/collection-search",
163+
QueryConformanceClasses.COLLECTIONS,
164+
FilterConformanceClasses.COLLECTIONS,
165+
FreeTextConformanceClasses.COLLECTIONS,
166+
QueryConformanceClasses.COLLECTIONS,
167+
SortConformanceClasses.COLLECTIONS,
168+
FieldsConformanceClasses.COLLECTIONS,
169+
],
170+
)
168171

169-
# Initialize collections-search endpoint extension
170-
collections_search_endpoint_ext = CollectionsSearchEndpointExtension(
171-
client=CoreClient(
172-
database=database_logic,
173-
session=session,
174-
post_request_model=collection_search_post_request_model,
175-
landing_page_id=os.getenv("STAC_FASTAPI_LANDING_PAGE_ID", "stac-fastapi"),
176-
),
177-
settings=settings,
178-
GET=collections_get_request_model,
179-
POST=collection_search_post_request_model,
180-
conformance_classes=[
181-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search",
182-
"http://www.opengis.net/spec/ogcapi-common-2/1.0/conf/simple-query",
183-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search#filter",
184-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search#free-text",
185-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search#query",
186-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search#sort",
187-
"https://api.stacspec.org/v1.0.0-rc.1/collection-search#fields",
188-
],
189-
)
172+
# Initialize collections-search endpoint extension
173+
collections_search_endpoint_ext = CollectionsSearchEndpointExtension(
174+
client=CoreClient(
175+
database=database_logic,
176+
session=session,
177+
post_request_model=collection_search_post_request_model,
178+
landing_page_id=os.getenv("STAC_FASTAPI_LANDING_PAGE_ID", "stac-fastapi"),
179+
),
180+
settings=settings,
181+
GET=collections_get_request_model,
182+
POST=collection_search_post_request_model,
183+
conformance_classes=[
184+
"https://api.stacspec.org/v1.0.0-rc.1/collection-search",
185+
QueryConformanceClasses.COLLECTIONS,
186+
FilterConformanceClasses.COLLECTIONS,
187+
FreeTextConformanceClasses.COLLECTIONS,
188+
QueryConformanceClasses.COLLECTIONS,
189+
SortConformanceClasses.COLLECTIONS,
190+
FieldsConformanceClasses.COLLECTIONS,
191+
],
192+
)
193+
194+
extensions.append(collection_search_ext)
195+
extensions.append(collection_search_post_ext)
196+
extensions.append(collections_search_endpoint_ext)
190197

191-
extensions.append(collection_search_ext)
192-
extensions.append(collection_search_post_ext)
193-
extensions.append(collections_search_endpoint_ext)
194198

195199
database_logic.extensions = [type(ext).__name__ for ext in extensions]
196200

@@ -231,8 +235,8 @@
231235
"route_dependencies": get_route_dependencies(),
232236
}
233237

234-
# Add collections_get_request_model if collection search is enabled
235-
if ENABLE_COLLECTIONS_SEARCH:
238+
# Add collections_get_request_model if it was created
239+
if collections_get_request_model:
236240
app_config["collections_get_request_model"] = collections_get_request_model
237241

238242
api = StacApi(**app_config)

0 commit comments

Comments
 (0)