Skip to content

Commit

Permalink
added /health endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
borolepratik committed Dec 30, 2023
1 parent 3c381e7 commit 4bcbc80
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
51 changes: 46 additions & 5 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
8 changes: 7 additions & 1 deletion app/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit 4bcbc80

Please sign in to comment.