Inayatgaming's picture
Update app.py
05d6299 verified
Raw History Blame Contribute Delete
1.46 kB
from fastapi import FastAPI, Query, HTTPException
from MultiFeatures.IndianRailway import whereismytrain
app = FastAPI(
title="Where Is My Train API",
version="1.0.0"
)
wit = whereismytrain.WhereIsMyTrain()
@app.get("/")
async def root():
return {
"status": "online",
"service": "Where Is My Train wrapper",
"endpoints": [
"/live",
"/station"
]
}
@app.get("/live")
async def live(
train: str = Query(...),
date: str = Query(...),
from_station: str = Query(..., alias="from"),
to_station: str = Query(..., alias="to"),
from_day: int = Query(1),
lang: str = Query("en")
):
try:
result = await wit.live_status(
train_no=train,
date=date,
from_station=from_station,
to_station=to_station,
from_day=from_day,
lang=lang
)
return result
except Exception as e:
raise HTTPException(
status_code=502,
detail=str(e)
)
@app.get("/station")
async def station(
code: str = Query(...),
hours: int = Query(8),
lang: str = Query("en")
):
try:
result = await wit.live_station(
station_code=code,
hrs=hours,
lang=lang
)
return result
except Exception as e:
raise HTTPException(
status_code=502,
detail=str(e)
)