Skip to content

Commit

Permalink
feat(wekeo_ecmwf): make search request more robust
Browse files Browse the repository at this point in the history
fix queryables

simplify config
  • Loading branch information
alambare committed Dec 2, 2024
1 parent 96210a6 commit 44bc0fe
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 678 deletions.
43 changes: 26 additions & 17 deletions eodag/plugins/search/build_search_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
from eodag.api.product.metadata_mapping import (
NOT_AVAILABLE,
NOT_MAPPED,
eodag_key_from_provider_key,
format_metadata,
format_query_params,
mtd_cfg_as_conversion_and_querypath,
Expand Down Expand Up @@ -317,6 +316,11 @@ def _update_properties_from_element(
prop["description"] = description


def ecmwf_format(v: str) -> str:
"""Add ECMWF prefix to value v if v is a ECMWF keyword."""
return "ecmwf:" + v if v in ECMWF_KEYWORDS + COP_DS_KEYWORDS else v


class ECMWFSearch(PostJsonSearch):
"""ECMWF search plugin.
Expand Down Expand Up @@ -595,7 +599,7 @@ def discover_queryables(
):
formated_kwargs[key] = kwargs[key]
else:
raise ValidationError(f"{key} in not a queryable parameter")
raise ValidationError(f"{key} is not a queryable parameter")

# we use non empty kwargs as default to integrate user inputs
# it is needed because pydantic json schema does not represent "value"
Expand Down Expand Up @@ -883,15 +887,11 @@ def queryables_by_form(
if default and prop["type"] == "string" and isinstance(default, list):
default = ",".join(default)

# rename keywords from form with metadata mapping.
# needed to map constraints like "xxxx" to eodag parameter "cop_cds:xxxx"
key = eodag_key_from_provider_key(name, self.config.metadata_mapping)

is_required = bool(element.get("required"))
if is_required:
required_list.append(name)

queryables[key] = Annotated[
queryables[ecmwf_format(name)] = Annotated[
get_args(
json_field_definition_to_python(
prop,
Expand Down Expand Up @@ -920,16 +920,13 @@ def queryables_by_values(
"""
# Rename keywords from form with metadata mapping.
# Needed to map constraints like "xxxx" to eodag parameter "ecmwf:xxxx"
required = [
eodag_key_from_provider_key(k, self.config.metadata_mapping)
for k in required_keywords
]
required = [ecmwf_format(k) for k in required_keywords]

queryables: Dict[str, Annotated[Any, FieldInfo]] = {}
for name, values in available_values.items():
# Rename keywords from form with metadata mapping.
# Needed to map constraints like "xxxx" to eodag parameter "ecmwf:xxxx"
key = eodag_key_from_provider_key(name, self.config.metadata_mapping)
key = ecmwf_format(name)

default = defaults.get(key)

Expand Down Expand Up @@ -1237,10 +1234,14 @@ def normalize_results(
"""
normalized = QueryStringSearch.normalize_results(self, results, **kwargs)

if len(normalized) > 0:
normalized[0].properties["_dc_qs"] = quote_plus(
orjson.dumps(results.query_params)
)
if not normalized:
return normalized

query_params_encoded = quote_plus(orjson.dumps(results.query_params))
for product in normalized:
properties = {**product.properties, **results.query_params}
properties["_dc_qs"] = query_params_encoded
product.properties = {ecmwf_format(k): v for k, v in properties.items()}

return normalized

Expand All @@ -1262,6 +1263,14 @@ def build_query_string(
:param kwargs: keyword arguments to be used in the query string
:return: formatted query params and encode query string
"""
# reorder kwargs to make sure year/month/day/time if set overwrite default datetime
priority_keys = [
"startTimeFromAscendingNode",
"completionTimeFromAscendingNode",
]
ordered_kwargs = {k: kwargs[k] for k in priority_keys if k in kwargs}
ordered_kwargs.update(kwargs)

return QueryStringSearch.build_query_string(
self, product_type=product_type, **kwargs
self, product_type=product_type, **ordered_kwargs
)
2 changes: 1 addition & 1 deletion eodag/resources/product_types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2396,7 +2396,7 @@ CAMS_EU_AIR_QUALITY_FORECAST:
sensorType: ATMOSPHERIC
license: proprietary
title: CAMS European air quality forecasts
missionStartDate: "2021-11-22T00:00:00Z"
missionStartDate: "2021-11-27T00:00:00Z"

CAMS_GFE_GFAS:
abstract: |
Expand Down
Loading

0 comments on commit 44bc0fe

Please sign in to comment.