Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(API): Add endpoint to get drivers in a session #45

Merged
merged 1 commit into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
router.register(r'events', views.Events, 'events')
router.register(r'raceLapChart', views.RaceLapChart, 'raceLapChart')
router.register(r'years', views.Years, 'years')
router.register(r'sessionDrivers', views.SessionDrivers, 'sessionDrivers')

urlpatterns = [
path('admin/', admin.site.urls),
Expand Down
52 changes: 49 additions & 3 deletions webviewer/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def assemble_url(params, url):
url = f"{url}{p}={params[p]}"
return url


def get_year_from_request(params: QueryDict):
error = None
year = None
Expand All @@ -45,7 +44,6 @@ def get_year_from_request(params: QueryDict):

return error, year


def get_event_from_request(params: QueryDict):
error = None
event = None
Expand All @@ -63,6 +61,21 @@ def get_event_from_request(params: QueryDict):

return error, event

def get_session_from_request(params: QueryDict):
error = None
session = None
try:
session = int(params['session'])
except KeyError:
res = {'errorMessage': "Please provide the name of the desired session as a parameter named 'session'."}
error = Response(res, status.HTTP_400_BAD_REQUEST)
return error, session
except ValueError:
res = {'errorMessage': "Please send a valid integer for the session you want (e.g. 1 for FP1, 5 for Race etc)."}
error = Response(res, status.HTTP_400_BAD_REQUEST)
return error, session

return error, session

def enable_cache():
if not os.path.isdir('server/fastf1_cache'):
Expand All @@ -77,7 +90,6 @@ def list(self, request, *args, **kwargs):
res = { "years": years }
return Response(res, status.HTTP_200_OK)


class Events(viewsets.ViewSet):

def list(self, request: Request, *args, **kwargs):
Expand All @@ -95,6 +107,40 @@ def list(self, request: Request, *args, **kwargs):
res = {"events": event_names}
return Response(res, status.HTTP_200_OK)

class SessionDrivers(viewsets.ViewSet):

def list(self, request: Request, *args, **kwargs):

params = request.query_params

year_error, year = get_year_from_request(params)

if year_error:
return year_error

event_error, event_name = get_event_from_request(params)

if event_error:
return event_error

session_error, session = get_session_from_request(params)

if session_error:
return session_error

enable_cache()
session = fastf1.get_session(year, event_name, session)
session.load()

drivers = pandas.unique(session.laps['Driver'])
session_drivers = []

for driver in drivers:
driver_data = session.get_driver(driver)
session_drivers.append({ "name": driver_data['FullName'], "abbreviation": driver })

res = { "drivers": session_drivers }
return Response(res, status.HTTP_200_OK)

class RaceLapChart(viewsets.ViewSet):
def list(self, request: Request, *args, **kwargs):
Expand Down