fix: handle P2P fetch errors gracefully, add logging for diagnostics
Browse files
main.py
CHANGED
|
@@ -7,8 +7,12 @@ import time
|
|
| 7 |
import re
|
| 8 |
import certifi
|
| 9 |
import jwt
|
|
|
|
| 10 |
from jwt import PyJWKClient
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
app = FastAPI(title="Tazas API", version="1.0.0")
|
| 13 |
|
| 14 |
app.add_middleware(
|
|
@@ -108,6 +112,7 @@ async def fetch_bcv() -> dict:
|
|
| 108 |
rates[f"bcv{suffix}"] = _parse_bcv_rate(tag.text) if tag else None
|
| 109 |
|
| 110 |
if all(v is None for v in rates.values()):
|
|
|
|
| 111 |
raise HTTPException(status_code=503, detail="No se pudo obtener las tasas BCV")
|
| 112 |
|
| 113 |
updated_tag = soup.find("span", {"class": re.compile(r"date-display")})
|
|
@@ -194,8 +199,11 @@ async def root():
|
|
| 194 |
|
| 195 |
@app.get("/v1/rates", dependencies=[Depends(verify_app_check)])
|
| 196 |
async def rates():
|
| 197 |
-
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
|
| 201 |
@app.get("/v1/bcv", dependencies=[Depends(verify_app_check)])
|
|
@@ -203,6 +211,14 @@ async def bcv():
|
|
| 203 |
return await fetch_bcv()
|
| 204 |
|
| 205 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
@app.get("/v1/p2p", dependencies=[Depends(verify_app_check)])
|
| 207 |
async def p2p():
|
| 208 |
return await fetch_p2p()
|
|
|
|
| 7 |
import re
|
| 8 |
import certifi
|
| 9 |
import jwt
|
| 10 |
+
import logging
|
| 11 |
from jwt import PyJWKClient
|
| 12 |
|
| 13 |
+
logging.basicConfig(level=logging.INFO)
|
| 14 |
+
logger = logging.getLogger(__name__)
|
| 15 |
+
|
| 16 |
app = FastAPI(title="Tazas API", version="1.0.0")
|
| 17 |
|
| 18 |
app.add_middleware(
|
|
|
|
| 112 |
rates[f"bcv{suffix}"] = _parse_bcv_rate(tag.text) if tag else None
|
| 113 |
|
| 114 |
if all(v is None for v in rates.values()):
|
| 115 |
+
logger.error("BCV: all rates are None — scraping failed")
|
| 116 |
raise HTTPException(status_code=503, detail="No se pudo obtener las tasas BCV")
|
| 117 |
|
| 118 |
updated_tag = soup.find("span", {"class": re.compile(r"date-display")})
|
|
|
|
| 199 |
|
| 200 |
@app.get("/v1/rates", dependencies=[Depends(verify_app_check)])
|
| 201 |
async def rates():
|
| 202 |
+
bcv_data, p2p_data = await asyncio.gather(
|
| 203 |
+
fetch_bcv(),
|
| 204 |
+
fetch_p2p_safe(),
|
| 205 |
+
)
|
| 206 |
+
return {**bcv_data, **p2p_data}
|
| 207 |
|
| 208 |
|
| 209 |
@app.get("/v1/bcv", dependencies=[Depends(verify_app_check)])
|
|
|
|
| 211 |
return await fetch_bcv()
|
| 212 |
|
| 213 |
|
| 214 |
+
async def fetch_p2p_safe() -> dict:
|
| 215 |
+
try:
|
| 216 |
+
return await fetch_p2p()
|
| 217 |
+
except Exception as e:
|
| 218 |
+
logger.error("fetch_p2p failed: %s", e, exc_info=True)
|
| 219 |
+
return {"p2pBuyMax": None, "p2pSellMax": None}
|
| 220 |
+
|
| 221 |
+
|
| 222 |
@app.get("/v1/p2p", dependencies=[Depends(verify_app_check)])
|
| 223 |
async def p2p():
|
| 224 |
return await fetch_p2p()
|