diff --git a/app/main.py b/app/main.py index 4a4b6f4..39a8b19 100644 --- a/app/main.py +++ b/app/main.py @@ -2,10 +2,10 @@ from typing import Annotated import fastf1 -from fastapi import FastAPI, Path +from fastapi import FastAPI, Path, status from app.constants import EVENT_SCHEDULE_DATETIME_DTYPE_LIST, METADATA_DESCRIPTION -from app.models import Schedule +from app.models import HealthCheck, Schedule # fastf1.set_log_level("WARNING") # TODO use for production @@ -25,12 +25,47 @@ ) -@app.get("/") +@app.get( + "/", + tags=["root"], + summary="Read root", + response_description="Return root info", + status_code=status.HTTP_200_OK, +) def read_root(): - return {"We Are": "SlickTelemetry"} + return {"we_are": "SlickTelemetry"} + +# https://gist.github.com/Jarmos-san/0b655a3f75b698833188922b714562e5 +@app.get( + "/health", + tags=["healthcheck"], + summary="Perform a Health Check", + response_description="Return HTTP Status Code 200 (OK)", + status_code=status.HTTP_200_OK, + response_model=HealthCheck, +) +def get_health() -> HealthCheck: + """ + ## Perform a Health Check + Endpoint to perform a healthcheck on. This endpoint can primarily be used Docker + to ensure a robust container orchestration and management is in place. Other + services which rely on proper functioning of the API service will not deploy if this + endpoint returns any other HTTP status code except 200 (OK). + Returns: + HealthCheck: Returns a JSON response with the health status + """ + return HealthCheck(status="OK") -@app.get("/schedule/{year}", response_model=list[Schedule]) + +@app.get( + "/schedule/{year}", + tags=["schedule"], + summary="Get events schedule for a Formula 1 calendar year", + response_description="Return list of events schedule", + status_code=status.HTTP_200_OK, + response_model=list[Schedule], +) def get_schedule( year: Annotated[ int, @@ -40,6 +75,12 @@ def get_schedule( ), ] ) -> list[Schedule]: + """ + ## Get events schedule for a Formula 1 calendar year + Endpoint to get events schedule for Formula 1 calendar year. + Returns: + list[Schedule]: Returns a JSON response with the list of event schedule + """ event_schedule = fastf1.get_event_schedule(year) # Convert timestamp(z) related columns' data into a string type diff --git a/app/models.py b/app/models.py index 0e677c0..56b66ca 100644 --- a/app/models.py +++ b/app/models.py @@ -1,7 +1,15 @@ from pydantic import BaseModel +class ReadRoot(BaseModel): + """Response model to validate and return when performing a health check.""" + + we_are: str = "SlickTelemetry" + + class Schedule(BaseModel): + """Model to store schedule data for a Formula 1 calendar year.""" + RoundNumber: int Country: str Location: str @@ -25,3 +33,9 @@ class Schedule(BaseModel): Session5Date: str Session5DateUtc: str F1ApiSupport: bool + + +class HealthCheck(BaseModel): + """Response model to validate and return when performing a health check.""" + + status: str = "OK" diff --git a/app/test_main.py b/app/test_main.py index 79e7914..32ed830 100644 --- a/app/test_main.py +++ b/app/test_main.py @@ -8,7 +8,13 @@ def test_read_root(): response = client.get("/") assert response.status_code == 200 - assert response.json() == {"We Are": "SlickTelemetry"} + assert response.json() == {"we_are": "SlickTelemetry"} + + +def test_healthcheck(): + response = client.get("/health") + assert response.status_code == 200 + assert response.json() == {"status": "OK"} def test_get_schedule():