|
1 | 1 | import logging |
2 | 2 | import os |
3 | 3 | import threading |
| 4 | +import json |
| 5 | +import requests |
| 6 | +from starlette import status |
4 | 7 | from fastapi import FastAPI |
5 | 8 | from pydantic import BaseModel |
6 | 9 | from typing import List, Optional |
@@ -59,3 +62,56 @@ def get_data(request: DatabaseRequest): |
59 | 62 | thread.start() |
60 | 63 |
|
61 | 64 | 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