From 210a94b4ede4df267a6a1e47fdbc28fda33fe7b5 Mon Sep 17 00:00:00 2001 From: Pratik Borole Date: Fri, 9 Feb 2024 13:07:02 +0530 Subject: [PATCH] feat: BACK-32 added /next-event path --- app/main.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++- app/test_main.py | 23 +++++++++++----------- 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/app/main.py b/app/main.py index eccc33e..f4c5fc6 100644 --- a/app/main.py +++ b/app/main.py @@ -1,4 +1,5 @@ import json +from datetime import datetime from typing import Annotated import fastf1 @@ -6,6 +7,7 @@ from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import FileResponse from fastf1.ergast import Ergast +from pandas import Timestamp from .constants import ( DEFAULT_SESSION_FOR_RESULTS, @@ -147,10 +149,56 @@ def get_schedule( return schedule_as_json_obj +@app.get( + "/next-event", + tags=["schedule"], + summary="Get upcoming event", + response_description="Returns upcoming event", + status_code=status.HTTP_200_OK, + response_model=EventSchedule, +) +def get_next_event() -> EventSchedule: + """ + ## Get upcoming event + Endpoint to get upcoming event. + + **Returns**: + EventSchedule: Returns upcoming event + """ + + remaining_events = fastf1.get_events_remaining( + dt=datetime.now(), include_testing=False + ) + + if len(remaining_events) == 0: + # EITHER current season has ended OR new season's schedule has not yet been released + + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, detail="Next event not found." + ) + else: + # Current season EITHER has not yet started OR is in progress + + next_event: fastf1.events.Event = remaining_events.iloc[0] + + # Convert timestamp(z) related columns' data into string type + next_event = next_event.apply( + lambda x: str(x) if isinstance(x, Timestamp) else x, + ) + + # Convert the dataframe to a JSON string + next_event_as_json = next_event.to_json() + + # Parse the JSON string to a JSON object + next_event_as_json_obj: EventSchedule = json.loads(next_event_as_json) + + return next_event_as_json_obj + + @app.get( "/standings", tags=["standings"], - summary="Get drivers and constructors standings ", + summary="Get drivers and constructors standings", response_description="Return a list of drivers and constructors standings at specific points of a season. If the season hasn't ended you will get the current standings.", status_code=status.HTTP_200_OK, response_model=Standings, diff --git a/app/test_main.py b/app/test_main.py index 7086636..fc6db69 100644 --- a/app/test_main.py +++ b/app/test_main.py @@ -1,5 +1,6 @@ from datetime import datetime +from fastapi import status from fastapi.testclient import TestClient from .constants import ( @@ -18,7 +19,7 @@ def test_read_root(): response = client.get("/") - assert response.status_code == 200 + assert response.status_code == status.HTTP_200_OK assert response.json() == {"we_are": "SlickTelemetry"} @@ -30,7 +31,7 @@ def test_read_root(): def test_healthcheck(): response = client.get("/health") - assert response.status_code == 200 + assert response.status_code == status.HTTP_200_OK assert response.json() == {"status": "OK"} @@ -44,7 +45,7 @@ def test_healthcheck(): def test_get_schedule(): response = client.get("/schedule") - assert response.status_code == 200 + assert response.status_code == status.HTTP_200_OK assert response.json() == { "year": 2023, "EventSchedule": [ @@ -604,7 +605,7 @@ def test_get_schedule(): def test_get_schedule_good_year(): response = client.get("/schedule?year=2023") - assert response.status_code == 200 + assert response.status_code == status.HTTP_200_OK assert response.json() == { "year": 2023, "EventSchedule": [ @@ -1174,7 +1175,7 @@ def test_get_schedule_good_year(): def test_get_standings(): response = client.get("/standings") - assert response.status_code == 200 + assert response.status_code == status.HTTP_200_OK assert response.json() == { "season": 2023, "round": 22, @@ -1835,7 +1836,7 @@ def test_get_standings(): def test_get_standings_good_year_only(): response = client.get("/standings?year=2023") - assert response.status_code == 200 + assert response.status_code == status.HTTP_200_OK assert response.json() == { "season": 2023, "round": 22, @@ -2496,7 +2497,7 @@ def test_get_standings_good_year_only(): def test_get_standings_good_year_and_round(): response = client.get("/standings?year=2023&round=5") - assert response.status_code == 200 + assert response.status_code == status.HTTP_200_OK assert response.json() == { "season": 2023, "round": 5, @@ -3114,7 +3115,7 @@ def test_get_standings_good_year_and_round(): def test_get_standings_good_round_bad_year_no_input(): response = client.get("/standings?round=3") - assert response.status_code == 400 + assert response.status_code == status.HTTP_400_BAD_REQUEST assert response.json() == { "detail": 'Bad request. Must provide the "year" parameter.' } @@ -3132,7 +3133,7 @@ def test_get_standings_good_round_bad_year_no_input(): def test_get_results(): response = client.get("/results/2023/5") - assert response.status_code == 200 + assert response.status_code == status.HTTP_200_OK assert response.json() == [ { "DriverNumber": "1", @@ -3599,7 +3600,7 @@ def test_get_results(): def test_get_results_with_session(): response = client.get("/results/2023/5?session=4") - assert response.status_code == 200 + assert response.status_code == status.HTTP_200_OK assert response.json() == [ { "DriverNumber": "11", @@ -4071,7 +4072,7 @@ def test_get_results_with_session(): def test_get_results_bad_round_invalid(): response = client.get("/results/2023/25?session=2") - assert response.status_code == 400 + assert response.status_code == status.HTTP_400_BAD_REQUEST assert response.json() == {"detail": "Bad Request. Invalid round: 25"}