This commit is contained in:
Gabriel Augendre 2021-11-09 12:33:52 +01:00
parent ce7eecfd3d
commit 8d0ca77be1
3 changed files with 20 additions and 10 deletions

View file

@ -1,3 +1,3 @@
{ {
"python.pythonPath": "/Users/gaugendre/.pyenv/versions/3.10.0/envs/tcl-filtrage/bin/python" "python.pythonPath": "/Users/gaugendre/.pyenv/versions/3.10.0/envs/tcl-filtrage/bin/python"
} }

20
main.py
View file

@ -1,15 +1,19 @@
import enum
from typing import List, Optional from typing import List, Optional
import httpx
from fastapi import FastAPI, HTTPException from fastapi import FastAPI, HTTPException
from fastapi.params import Header from fastapi.params import Header
from pydantic import BaseModel from pydantic import BaseModel
import enum, httpx
app = FastAPI() app = FastAPI()
class PassageType(enum.Enum): class PassageType(enum.Enum):
E = "E" E = "E"
T = "T" T = "T"
class Passage(BaseModel): class Passage(BaseModel):
coursetheorique: str coursetheorique: str
delaipassage: str delaipassage: str
@ -22,9 +26,11 @@ class Passage(BaseModel):
ligne: str ligne: str
type: PassageType type: PassageType
class Passages(BaseModel): class Passages(BaseModel):
passages: List[Passage] passages: List[Passage]
# Stop id can be optained using # Stop id can be optained using
# https://data.grandlyon.com/jeux-de-donnees/points-arret-reseau-transports-commun-lyonnais/donnees # https://data.grandlyon.com/jeux-de-donnees/points-arret-reseau-transports-commun-lyonnais/donnees
@app.get("/stop/{stop_id}", response_model=Passages) @app.get("/stop/{stop_id}", response_model=Passages)
@ -32,15 +38,19 @@ async def stop(stop_id: int, authorization: Optional[str] = Header(None)):
if authorization is None: if authorization is None:
raise HTTPException(status_code=401, detail="Not authenticated") raise HTTPException(status_code=401, detail="Not authenticated")
headers = {'Authorization': authorization} headers = {"Authorization": authorization}
async with httpx.AsyncClient(headers=headers) as client: async with httpx.AsyncClient(headers=headers) as client:
res = await client.get('https://download.data.grandlyon.com/ws/rdata/tcl_sytral.tclpassagearret/all.json?maxfeatures=-1') res = await client.get(
"https://download.data.grandlyon.com/ws/rdata/tcl_sytral.tclpassagearret/all.json?maxfeatures=-1"
)
if res.status_code != 200: if res.status_code != 200:
raise HTTPException(status_code=res.status_code, detail="HTTP error during call to remote API") raise HTTPException(
status_code=res.status_code,
detail="HTTP error during call to remote API",
)
passages: List[Passage] = [] passages: List[Passage] = []
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.append(passage)
return Passages(passages=passages) return Passages(passages=passages)