Skip to content

Commit

Permalink
feat: BACK-32 added /next-event path
Browse files Browse the repository at this point in the history
  • Loading branch information
borolepratik committed Feb 11, 2024
1 parent 0b66830 commit 210a94b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
50 changes: 49 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import json
from datetime import datetime
from typing import Annotated

import fastf1
from fastapi import FastAPI, HTTPException, Path, Query, status
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,
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 12 additions & 11 deletions app/test_main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime

from fastapi import status
from fastapi.testclient import TestClient

from .constants import (
Expand All @@ -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"}


Expand All @@ -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"}


Expand All @@ -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": [
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.'
}
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"}


Expand Down

0 comments on commit 210a94b

Please sign in to comment.