forked from osm-fr/osmose-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta_0_3.py
66 lines (53 loc) · 1.89 KB
/
meta_0_3.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
from typing import Any, Dict, List, Literal
from asyncpg import Connection
from fastapi import APIRouter, Depends, Request
from modules import query_meta
from modules.dependencies import database, langs
from modules.utils import LangsNegociation
router = APIRouter()
def _map_items(categories: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
for categorie in categories:
categorie["categ"] = categorie["id"]
del categorie["id"]
for item in categorie["items"]:
item["categ"] = item["categorie_id"]
del item["categorie_id"]
return categories
@router.get(
"/0.3/items",
response_model=Dict[str, List[Dict]],
tags=["metadata"],
)
async def items(
request: Request,
db: Connection = Depends(database.db),
langs: LangsNegociation = Depends(langs.langs),
) -> Dict[Literal["categories"], List[Dict[str, Any]]]:
return {"categories": _map_items(await query_meta._items(db, langs=langs))}
@router.get(
"/0.3/items/{item}/class/{classs}",
response_model=Dict[str, List[Dict]],
tags=["metadata"],
)
async def items_class(
request: Request,
item: int,
classs: int,
db: Connection = Depends(database.db),
langs: LangsNegociation = Depends(langs.langs),
) -> Dict[Literal["categories"], List[Dict[str, Any]]]:
return {
"categories": (
await query_meta._items(db, item=item, classs=classs, langs=langs)
)
}
@router.get("/0.3/countries", response_model=Dict[str, List[str]], tags=["metadata"])
async def countries(
db: Connection = Depends(database.db),
) -> Dict[Literal["countries"], List[str]]:
return {"countries": await query_meta._countries(db)}
@router.get("/0.3/tags", response_model=Dict[str, List[str]], tags=["metadata"])
async def tags(
db: Connection = Depends(database.db),
) -> Dict[Literal["tags"], List[str]]:
return {"tags": await query_meta._tags(db)}