-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
89 lines (65 loc) · 2.21 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from fastapi import APIRouter, FastAPI, HTTPException, Query
from src.api.batter import get_batter_info
from src.api.bowler import get_bowler_info
from src.api.teams import Team, get_all_teams, get_team_desc, team_vs_team
app = FastAPI()
api_v1_router = APIRouter(prefix='/api/v1')
@app.get('/')
async def root():
return {'name': 'IPL API', 'api_route': '/api/v1'}
@api_v1_router.get('/')
async def root_v1_router():
return {'route': '/api/v1', 'version': '1.20.2', 'running': True}
@api_v1_router.get('/teams')
async def teams():
teams = get_all_teams()
return teams
@api_v1_router.get('/team-vs-team')
async def team_vs_team_route(
t1: str = Query(None, description='Name of a IPL team.'),
t2: str = Query(None, description='Name of a IPL team.'),
):
if t1 is None or t2 is None:
raise HTTPException(
status_code=400, detail='Please provide both team names',
)
try:
team1, team2 = Team.decode(t1), Team.decode(t2)
except AttributeError:
raise HTTPException(400, "Entered team doesn't exists.")
return team_vs_team(team1, team2)
@api_v1_router.get('/team_desc')
async def get_team_desc(
t: str = Query(None, description='Name of a IPL team.'),
):
if t is None:
raise HTTPException(
status_code=400, detail='Please provide a team name',
)
try:
t = Team.decode(t)
except AttributeError:
raise HTTPException(400, "Entered team doesn't exists.")
return get_team_desc(t)
@api_v1_router.get('/batter')
async def batter(
s: str = Query(None, description='Player name who played IPL.'),
):
if s is None:
raise HTTPException(
status_code=400, detail='Please provide a batter name',
)
return get_batter_info(s)
@api_v1_router.get('/batter-vs-team')
async def batter_vs_team():
raise HTTPException(status_code=404, detail='Not implemented yet')
@api_v1_router.get('/bowler')
async def bowler(
s: str = Query(None, description='Bowler name who played IPL'),
):
if s is None:
raise HTTPException(
status_code=400, detail='Please provide a bowler name',
)
return get_bowler_info(s)
app.include_router(api_v1_router)