Skip to content

Commit

Permalink
Bragi new params (#293)
Browse files Browse the repository at this point in the history
* update bragi API model

* codes is optional with new Bragi

* geocoder/models/params: stops type is not used
  • Loading branch information
remi-dupre committed Jan 10, 2022
1 parent cb69b8a commit 0dac1bc
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 41 deletions.
2 changes: 1 addition & 1 deletion idunn/api/instant_answer.py
Expand Up @@ -254,7 +254,7 @@ async def get_instant_answer(
# Direct geocoding query
query = QueryParams.build(q=normalized_query, lang=lang, limit=5, **extra_geocoder_params)
query_tripadvisor = deepcopy(query)
query_tripadvisor.override_indices_name.append("poi_tripadvisor")
query_tripadvisor.poi_dataset.append("tripadvisor")

async def fetch_pj_response():
if not (settings["IA_CALL_PJ_POI"] and user_country == "fr" and intentions):
Expand Down
4 changes: 2 additions & 2 deletions idunn/geocoder/models/geocodejson.py
Expand Up @@ -53,12 +53,12 @@ class AssociatedAdmin(BaseModel):
level: int
label: str
name: str
zip_codes: List[str]
zip_codes: List[str] = []
coord: Coord
bbox: Optional[Rect]
zone_type: Optional[ZoneType]
parent_id: Optional[str]
codes: List[Code]
codes: List[Code] = []


class Line(BaseModel):
Expand Down
80 changes: 48 additions & 32 deletions idunn/geocoder/models/params.py
Expand Up @@ -30,54 +30,70 @@ class Type(str, Enum):
Zone = "zone"


class PlaceDocType(str, Enum):
# pylint: disable=invalid-name
Admin = "admin"
Street = "street"
Addr = "addr"
Poi = "poi"
# Stop = "stop" # this field is available in bragi but not used by Qwant Maps


@dataclass
class QueryParams:
q: str = Query(..., title="Query string")
"""
Mimic bragi's parameters as defined in mimirsbrunn:
https://github.com/CanalTP/mimirsbrunn/blob/v2.2.0/libs/mimir/src/adapters/primary/bragi/api.rs#L27-L42
lon: Optional[confloat(ge=-180, le=180)] = Query(
None, title="Longitude", description="Latitude of the focus point."
)
There are a few extra fields that won't be send to bragi at the end of
struct definition. These are generaly specific to Qwant, so Idunn acts as a
wrapper arround mimirsbrunn to avoid pusing too specific features.
"""

q: str = Query(..., title="Query string")

lat: Optional[confloat(ge=-90, le=90)] = Query(
None, title="Latitude", description="Longitude of the focus point."
)

zoom: confloat(ge=1) = Query(
11,
description=(
"Zoom level used to compute how far from the focus point results will typically be."
),
lon: Optional[confloat(ge=-180, le=180)] = Query(
None, title="Longitude", description="Latitude of the focus point."
)

lang: Optional[str] = Query(None, title="Language")
shape_scope: List[PlaceDocType] = Query(
[], description="Filter type of documents raised from inside of the shape."
)

limit: conint(ge=1, le=100) = Query(10, description="Maximum number of results.")
type: List[Type] = Query([], description="Filter on type of document.")

pt_dataset: List[str] = Query([], description="Point dataset name.")
zone_type: List[ZoneType] = Query([], description="Filter on type of zone.")

poi_dataset: List[str] = Query([], description="POI dataset name.")
poi_types: List[str] = Query([], description="Filter on type of POI.")

all_data: bool = Query(
False,
description=(
"Search through the entire dataset while ignoring the coverage filter of the geocoder."
),
)
limit: conint(ge=1, le=100) = Query(10, description="Maximum number of results.")

offset: int = Query(0, ge=0, description="Skip the first results")
lang: Optional[str] = Query(None, title="Language")

timeout: Optional[int] = Query(
None, ge=0, title="Timeout: ms", description="Timeout for the queries to the geocoder."
)

type: List[Type] = Query([], description="Filter on type of document.")
pt_dataset: List[str] = Query([], description="Point dataset name.")

zone_type: List[ZoneType] = Query([], description="Filter on type of zone.")
poi_dataset: List[str] = Query([], description="POI dataset name.")

request_id: Optional[str] = Query(
None, description="Specify a request ID for debugging purpose."
)

poi_type: List[str] = Query([], description="Filter on type of POI.")
offset: int = Query(0, ge=0, description="Skip the first results")

override_indices_name: List[str] = Query(
[], description="Give indices name to search on that are different from " "default ones"
# Specific to Idunn
zoom: confloat(ge=1) = Query(
11,
description=(
"Zoom level used to compute how far from the focus point results will typically be."
),
)

nlu: bool = Query(
Expand All @@ -103,17 +119,17 @@ def bragi_query_dict(self):
"""
params = {
"q": self.q,
"lang": self.lang,
"shape_scope[]": self.shape_scope,
"type[]": self.type,
"zone_type[]": self.zone_type,
"poi_types[]": self.poi_types,
"limit": self.limit,
"lang": self.lang,
"timeout": self.timeout,
"pt_dataset[]": self.pt_dataset,
"poi_dataset[]": self.poi_dataset,
"all_data": self.all_data,
"request_id": self.request_id,
"offset": self.offset,
"timeout": self.timeout,
"type[]": self.type,
"zone_type[]": self.zone_type,
"poi_type[]": self.poi_type,
"override_indices_name[]": self.override_indices_name,
}

# Enables the focus mode
Expand Down
12 changes: 6 additions & 6 deletions tests/fixtures/autocomplete/__init__.py
Expand Up @@ -105,13 +105,13 @@ def mock_autocomplete_get(httpx_mock):
re.compile(rf"^{BASE_URL}/autocomplete.*q=43\+rue\+de\+paris\+rennes.*")
).respond(json=read_fixture("fixtures/autocomplete/43_rue_de_paris_rennes.json"))

httpx_mock.get(
re.compile(f"^{BASE_URL}/autocomplete.*q=hotel.*override_indices_name.*")
).respond(json=read_fixture("fixtures/autocomplete/tripadvisor/hotel_moliere.json"))
httpx_mock.get(re.compile(f"^{BASE_URL}/autocomplete.*q=hotel.*poi_dataset.*")).respond(
json=read_fixture("fixtures/autocomplete/tripadvisor/hotel_moliere.json")
)

httpx_mock.get(
re.compile(f"^{BASE_URL}/autocomplete.*q=chez.*override_indices_name.*")
).respond(json=read_fixture("fixtures/autocomplete/tripadvisor/chez_eric.json"))
httpx_mock.get(re.compile(f"^{BASE_URL}/autocomplete.*q=chez.*poi_dataset.*")).respond(
json=read_fixture("fixtures/autocomplete/tripadvisor/chez_eric.json")
)

httpx_mock.get(re.compile(f"^{BASE_URL}/autocomplete")).respond(json=FIXTURE_AUTOCOMPLETE)

Expand Down

0 comments on commit 0dac1bc

Please sign in to comment.