Group passages by ligne

This commit is contained in:
Gabriel Augendre 2021-11-09 13:44:40 +01:00
parent 055a24a820
commit a915581ba1

27
main.py
View file

@ -1,5 +1,5 @@
import enum import enum
from typing import Any, List, Optional from typing import Any, DefaultDict, List, Optional
import httpx import httpx
from fastapi import FastAPI, HTTPException from fastapi import FastAPI, HTTPException
@ -9,26 +9,14 @@ from pydantic import BaseModel
app = FastAPI() app = FastAPI()
class PassageType(enum.Enum):
E = "E"
T = "T"
class Passage(BaseModel): class Passage(BaseModel):
coursetheorique: str
delaipassage: str
direction: str
gid: int
heurepassage: str
id: int
idtarretdestination: int
last_update_fme: str
ligne: str ligne: str
type: PassageType delais: List[str]
class Passages(BaseModel): class Passages(BaseModel):
passages: List[Passage] passages: List[Passage]
stop_id: int
@app.get("/stop/{stop_id}", response_model=Passages) @app.get("/stop/{stop_id}", response_model=Passages)
@ -57,8 +45,11 @@ async def stop(
detail="HTTP error during call to remote API", detail="HTTP error during call to remote API",
) )
passages: List[Passage] = [] passages = DefaultDict(list)
for passage in res.json().get("values"): for passage in res.json().get("values"):
if passage.get("id") == stop_id: if passage.get("id") == stop_id:
passages.append(passage) passages[passage.get("ligne")].append(passage.get("delaipassage"))
return Passages(passages=passages) passages_list = []
for ligne, delais in passages.items():
passages_list.append(Passage(ligne=ligne, delais=delais))
return Passages(passages=passages_list, stop_id=stop_id)