/
params.py
66 lines (57 loc) · 2.14 KB
/
params.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Bragi parameters as defined here:
https://github.com/CanalTP/mimirsbrunn/blob/master/libs/bragi/src/routes/autocomplete.rs#L59
"""
from enum import Enum
from typing import List, Optional
from fastapi import Query
from pydantic import BaseModel, Field, PositiveInt, confloat, conint
from pydantic.dataclasses import dataclass
from idunn import settings
from .cosmogony import ZoneType
class Type(str, Enum):
# City = "city" # this field is available in Bragi but deprecated
House = "house"
Poi = "poi"
StopArea = "public_transport:stop_area"
Street = "street"
Zone = "zone"
@dataclass
class QueryParams:
q: str = Query(..., title="query string")
lon: Optional[confloat(ge=-180, le=180)] = Query(None, title="latitude for the focus")
lat: Optional[confloat(ge=-90, le=90)] = Query(None, title="longitude for the focus")
lang: Optional[str] = Query(None, title="language")
limit: PositiveInt = 10
pt_dataset: List[str] = Query([])
poi_dataset: List[str] = Query([])
all_data: bool = False
offset: conint(gt=-1) = 0
timeout: Optional[conint(gt=-1)] = None
type: List[Type] = Query([])
zone_type: List[ZoneType] = Query([])
poi_type: List[str] = Query([])
nlu: bool = settings["AUTOCOMPLETE_NLU_DEFAULT"]
def bragi_query_dict(self):
"""
Return a dict with parameters accepted by the bragi API
See https://github.com/CanalTP/mimirsbrunn/blob/v1.14.0/libs/bragi/src/routes/autocomplete.rs#L60
"""
params = {
"q": self.q,
"lon": self.lon,
"lat": self.lat,
"lang": self.lang,
"limit": self.limit,
"pt_dataset[]": self.pt_dataset,
"poi_dataset[]": self.poi_dataset,
"all_data": self.all_data,
"offset": self.offset,
"timeout": self.timeout,
"type[]": self.type,
"zone_type[]": self.zone_type,
"poi_type[]": self.poi_type,
}
return {k: v for k, v in params.items() if v is not None}
class ExtraParams(BaseModel):
shape: dict = Field(None, title="restrict search inside of a polygon")