-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
51 lines (39 loc) · 1.42 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""API controller for the Telegram bot."""
import httpx
from fastapi import FastAPI, HTTPException
from telegram import Bot
from telegram.error import TelegramError
from release import __version__ as version
from tracker.config import ConfigHandler
app = FastAPI()
config = ConfigHandler()
@app.get("/health")
async def health_check():
"""Health check endpoint."""
results = {}
# Check Telegram Bot API connectivity
try:
bot = Bot(token=config.telegram_token)
bot_info = await bot.get_me()
results["telegram_api"] = bot_info is not None
except TelegramError:
results["telegram_api"] = False
# Check Etherscan API health
try:
async with httpx.AsyncClient() as client:
response = await client.get(
config.etherscan_api_url,
params={"module": "stats", "action": "ping"},
timeout=5,
)
results["etherscan_api"] = response.status_code == 200
except httpx.RequestError:
results["etherscan_api"] = False
# Determine the overall health status based on individual checks
overall_health = all(status is True for status in results.values())
if overall_health:
return {"version": version, "status": "healthy", "details": results}
raise HTTPException(
status_code=503,
detail={"version": version, "status": "unhealthy", "details": results},
)