Skip to content

Commit e2237ab

Browse files
author
Federico Rossi
committed
Added health and sample endpoint
1 parent d9423df commit e2237ab

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

database-parser/python/app/main.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import logging
22
import os
33
import threading
4+
import json
5+
import requests
6+
from starlette import status
47
from fastapi import FastAPI
58
from pydantic import BaseModel
69
from typing import List, Optional
@@ -59,3 +62,56 @@ def get_data(request: DatabaseRequest):
5962
thread.start()
6063

6164
return "Extraction Started"
65+
66+
67+
class HealthCheck(BaseModel):
68+
"""Response model to validate and return when performing a health check."""
69+
70+
status: str = "UP"
71+
72+
73+
@app.get(
74+
"/health",
75+
tags=["healthcheck"],
76+
summary="Perform a Health Check",
77+
response_description="Return HTTP Status Code 200 (OK)",
78+
status_code=status.HTTP_200_OK,
79+
response_model=HealthCheck,
80+
)
81+
def get_health() -> HealthCheck:
82+
"""
83+
## Perform a Health Check
84+
Endpoint to perform a healthcheck on. This endpoint can primarily be used Docker
85+
to ensure a robust container orchestration and management is in place. Other
86+
services which rely on proper functioning of the API service will not deploy if this
87+
endpoint returns any other HTTP status code except 200 (OK).
88+
Returns:
89+
HealthCheck: Returns a JSON response with the health status
90+
"""
91+
92+
try:
93+
fastapi_response = requests.get("http://localhost:5000/docs")
94+
if fastapi_response.status_code == 200:
95+
return HealthCheck(status="UP")
96+
else:
97+
return HealthCheck(status="DOWN")
98+
except requests.RequestException as e:
99+
logger.error(str(e) + " during request for health check")
100+
raise e
101+
102+
103+
@app.get("/sample",
104+
tags=["sample"],
105+
summary="Get a sample of result",
106+
response_description="Return json sample result", )
107+
def get_sample():
108+
f = open('data/sample.json')
109+
110+
# returns JSON object as
111+
# a dictionary
112+
data = json.load(f)
113+
114+
f.close()
115+
116+
return data
117+

0 commit comments

Comments
 (0)