diff --git a/app/settings_routes.py b/app/settings_routes.py index 2c1c98f..707dce8 100644 --- a/app/settings_routes.py +++ b/app/settings_routes.py @@ -1,3 +1,6 @@ +from urllib.parse import urlparse + +import httpx from fastapi import APIRouter, Depends, HTTPException from pydantic import BaseModel @@ -46,3 +49,45 @@ async def save_secret(body: SecretIn, ident=Depends(require_token), db=Depends(g except vault.SecretsUnconfigured as e: raise HTTPException(503, str(e)) return {"ok": True, "name": body.name, "set": True} + + +@router.post("/test/{service}") +async def test_connection(service: str, ident=Depends(require_token), db=Depends(get_db)): + """Validate stored credentials against the live service — so 'connected' means connected.""" + async with httpx.AsyncClient(timeout=15, follow_redirects=True) as c: + if service == "discogs": + tok = await vault.get_secret(db, "discogs_token") + if not tok: + raise HTTPException(400, "save a Discogs personal access token first") + r = await c.get("https://api.discogs.com/oauth/identity", + headers={"Authorization": f"Discogs token={tok}", + "User-Agent": "RecordGod/0.1"}) + if r.status_code == 200: + return {"ok": True, "connected": True, "as": r.json().get("username")} + return {"ok": True, "connected": False, "detail": f"HTTP {r.status_code}"} + + if service == "woo": + base = await vault.get_secret(db, "woo_base_url") + ck = await vault.get_secret(db, "woo_consumer_key") + cs = await vault.get_secret(db, "woo_consumer_secret") + if not (base and ck and cs): + raise HTTPException(400, "save Woo store URL + consumer key + secret first") + r = await c.get(base.rstrip("/") + "/wp-json/wc/v3/orders", + params={"per_page": 1}, auth=(ck, cs)) + if r.status_code == 200: + return {"ok": True, "connected": True, "as": urlparse(base).netloc} + return {"ok": True, "connected": False, "detail": f"HTTP {r.status_code}"} + + if service == "dealgod": + key = await vault.get_secret(db, "dealgod_api_key") + if not key: + raise HTTPException(400, "save a DealGod API key first") + r = await c.get("https://api.dealgod.pro/api/me", headers={"X-Api-Key": key}) + if r.status_code == 200: + j = r.json() + return {"ok": True, "connected": True, "as": f"{j.get('user')} · {j.get('plan')}"} + if r.status_code == 404: + return {"ok": True, "connected": False, "detail": "DealGod /api/me not deployed yet"} + return {"ok": True, "connected": False, "detail": f"HTTP {r.status_code}"} + + raise HTTPException(400, "unknown service") diff --git a/site/dash.html b/site/dash.html index 423f50d..39ca17b 100644 --- a/site/dash.html +++ b/site/dash.html @@ -48,13 +48,17 @@
Save your Woo store URL + consumer key/secret above, then live orders land here — - managed without wp-admin. (Coming next.)
+Save your Discogs OAuth/token above to enrich intake from the live API and your wantlist.
+Once WooCommerce tests green, live orders land here — managed without wp-admin. (Next build.)