Spaces:
Running
Running
Wake CoS on Robinhood refresh via webhook (best-effort)
Browse files
main.py
CHANGED
|
@@ -391,6 +391,50 @@ async def forward_webhook(payload: dict[str, Any]) -> None:
|
|
| 391 |
raise HTTPException(502, f"Grok webhook HTTP {res.status_code}: {res.text[:200]}")
|
| 392 |
|
| 393 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
@app.get("/")
|
| 395 |
@app.get("/health")
|
| 396 |
def health() -> dict[str, Any]:
|
|
@@ -539,12 +583,16 @@ async def post_robinhood(request: Request, authorization: str | None = Header(de
|
|
| 539 |
|
| 540 |
@app.post("/robinhood/refresh")
|
| 541 |
@app.post("/api/robinhood/refresh")
|
| 542 |
-
def post_robinhood_refresh(request: Request) -> JSONResponse:
|
| 543 |
"""Public. Phone asks Chief of Staff to pull Robinhood; it never calls Robinhood itself."""
|
| 544 |
if rate_limited(f"robinhood:refresh:{client_addr(request)}", max_n=1, window_s=REFRESH_INTERVAL_S):
|
| 545 |
raise HTTPException(429, "A refresh was just requested. Wait a few seconds.")
|
| 546 |
requested_at = utc_stamp()
|
| 547 |
save_refresh_request(requested_at)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 548 |
return JSONResponse(
|
| 549 |
{"ok": True, "requestedAt": requested_at, "refreshPending": True},
|
| 550 |
headers=NO_STORE,
|
|
|
|
| 391 |
raise HTTPException(502, f"Grok webhook HTTP {res.status_code}: {res.text[:200]}")
|
| 392 |
|
| 393 |
|
| 394 |
+
async def wake_robinhood_refresh(requested_at: str) -> None:
|
| 395 |
+
"""Best-effort wake for Chief of Staff. Never raises — phone must still get 200."""
|
| 396 |
+
payload = {"type": "robinhood_refresh", "requestedAt": requested_at}
|
| 397 |
+
rh_url = (os.environ.get("ROBINHOOD_REFRESH_WEBHOOK_URL") or "").strip()
|
| 398 |
+
if rh_url:
|
| 399 |
+
key = (os.environ.get("ROBINHOOD_REFRESH_WEBHOOK_KEY") or "").strip()
|
| 400 |
+
target = rh_url
|
| 401 |
+
headers = {"Content-Type": "application/json"}
|
| 402 |
+
if key:
|
| 403 |
+
parsed = urlparse(rh_url)
|
| 404 |
+
q = dict(parse_qsl(parsed.query, keep_blank_values=True))
|
| 405 |
+
if "key" not in q:
|
| 406 |
+
q["key"] = key
|
| 407 |
+
target = urlunparse(parsed._replace(query=urlencode(q)))
|
| 408 |
+
headers["Authorization"] = f"Bearer {key}"
|
| 409 |
+
headers["X-Webhook-Key"] = key
|
| 410 |
+
try:
|
| 411 |
+
async with httpx.AsyncClient(timeout=20.0) as client:
|
| 412 |
+
await client.post(target, headers=headers, json=payload)
|
| 413 |
+
except Exception:
|
| 414 |
+
return
|
| 415 |
+
return
|
| 416 |
+
# Fall back to the same Grok webhook used for chat (auth pattern matches forward_webhook).
|
| 417 |
+
url_raw = (os.environ.get("GROK_WEBHOOK_URL") or "").strip()
|
| 418 |
+
if not url_raw:
|
| 419 |
+
return
|
| 420 |
+
key = (os.environ.get("GROK_WEBHOOK_SENDER_KEY") or "").strip()
|
| 421 |
+
target = url_raw
|
| 422 |
+
headers = {"Content-Type": "application/json"}
|
| 423 |
+
if key:
|
| 424 |
+
parsed = urlparse(url_raw)
|
| 425 |
+
q = dict(parse_qsl(parsed.query, keep_blank_values=True))
|
| 426 |
+
if "key" not in q:
|
| 427 |
+
q["key"] = key
|
| 428 |
+
target = urlunparse(parsed._replace(query=urlencode(q)))
|
| 429 |
+
headers["Authorization"] = f"Bearer {key}"
|
| 430 |
+
headers["X-Webhook-Key"] = key
|
| 431 |
+
try:
|
| 432 |
+
async with httpx.AsyncClient(timeout=20.0) as client:
|
| 433 |
+
await client.post(target, headers=headers, json=payload)
|
| 434 |
+
except Exception:
|
| 435 |
+
return
|
| 436 |
+
|
| 437 |
+
|
| 438 |
@app.get("/")
|
| 439 |
@app.get("/health")
|
| 440 |
def health() -> dict[str, Any]:
|
|
|
|
| 583 |
|
| 584 |
@app.post("/robinhood/refresh")
|
| 585 |
@app.post("/api/robinhood/refresh")
|
| 586 |
+
async def post_robinhood_refresh(request: Request) -> JSONResponse:
|
| 587 |
"""Public. Phone asks Chief of Staff to pull Robinhood; it never calls Robinhood itself."""
|
| 588 |
if rate_limited(f"robinhood:refresh:{client_addr(request)}", max_n=1, window_s=REFRESH_INTERVAL_S):
|
| 589 |
raise HTTPException(429, "A refresh was just requested. Wait a few seconds.")
|
| 590 |
requested_at = utc_stamp()
|
| 591 |
save_refresh_request(requested_at)
|
| 592 |
+
try:
|
| 593 |
+
await wake_robinhood_refresh(requested_at)
|
| 594 |
+
except Exception:
|
| 595 |
+
pass
|
| 596 |
return JSONResponse(
|
| 597 |
{"ok": True, "requestedAt": requested_at, "refreshPending": True},
|
| 598 |
headers=NO_STORE,
|