Skip to content

Commit

Permalink
Added health and sample endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Federico Rossi committed Feb 29, 2024
1 parent d9423df commit e2237ab
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions database-parser/python/app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import logging
import os
import threading
import json
import requests
from starlette import status
from fastapi import FastAPI
from pydantic import BaseModel
from typing import List, Optional
Expand Down Expand Up @@ -59,3 +62,56 @@ def get_data(request: DatabaseRequest):
thread.start()

return "Extraction Started"


class HealthCheck(BaseModel):
"""Response model to validate and return when performing a health check."""

status: str = "UP"


@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
"""

try:
fastapi_response = requests.get("http://localhost:5000/docs")
if fastapi_response.status_code == 200:
return HealthCheck(status="UP")
else:
return HealthCheck(status="DOWN")
except requests.RequestException as e:
logger.error(str(e) + " during request for health check")
raise e


@app.get("/sample",
tags=["sample"],
summary="Get a sample of result",
response_description="Return json sample result", )
def get_sample():
f = open('data/sample.json')

# returns JSON object as
# a dictionary
data = json.load(f)

f.close()

return data

0 comments on commit e2237ab

Please sign in to comment.