Test for btc-data availbe
This commit is contained in:
+25
-6
@@ -133,18 +133,37 @@ async def health():
|
||||
return {"status": "up"}
|
||||
|
||||
|
||||
async def _proxy_btcdata_json(path: str) -> JSONResponse:
|
||||
url = f"{BTCDATA_URL}{path}"
|
||||
try:
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.get(url, timeout=10)
|
||||
return JSONResponse(content=resp.json(), status_code=resp.status_code)
|
||||
except httpx.RequestError as exc:
|
||||
logger.warning("Unable to connect to btcdata at %s: %s", url, exc)
|
||||
return JSONResponse(
|
||||
content={
|
||||
"error": "btcdata unavailable",
|
||||
"detail": f"Could not connect to {BTCDATA_URL}. Start btcdata first or set BTCDATA_URL.",
|
||||
},
|
||||
status_code=503,
|
||||
)
|
||||
except json.JSONDecodeError:
|
||||
logger.warning("btcdata returned non-JSON response from %s", url)
|
||||
return JSONResponse(
|
||||
content={"error": "invalid btcdata response"},
|
||||
status_code=502,
|
||||
)
|
||||
|
||||
|
||||
@app.get("/api/kraken-hour")
|
||||
async def kraken_hour():
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.get(f"{BTCDATA_URL}/api/kraken-hour", timeout=10)
|
||||
return JSONResponse(content=resp.json(), status_code=resp.status_code)
|
||||
return await _proxy_btcdata_json("/api/kraken-hour")
|
||||
|
||||
|
||||
@app.get("/api/kraken-minute")
|
||||
async def kraken_minute():
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.get(f"{BTCDATA_URL}/api/kraken-minute", timeout=10)
|
||||
return JSONResponse(content=resp.json(), status_code=resp.status_code)
|
||||
return await _proxy_btcdata_json("/api/kraken-minute")
|
||||
|
||||
|
||||
# --- Formatting helpers (Danish locale) ---
|
||||
|
||||
Reference in New Issue
Block a user