Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, json, time, hashlib
|
| 2 |
+
from fastapi import FastAPI, Request, HTTPException
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
WAKEUP_URL = os.getenv("WAKEUP_URL", "http://163.245.192.149:8080/wakeup/")
|
| 6 |
+
TIMEOUT = int(os.getenv("RELAY_TIMEOUT", "30"))
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
def norm(v):
|
| 11 |
+
if v is None:
|
| 12 |
+
return ""
|
| 13 |
+
if isinstance(v, str):
|
| 14 |
+
s = v.strip()
|
| 15 |
+
return "" if s.lower() in ("null", "none") else s
|
| 16 |
+
return v
|
| 17 |
+
|
| 18 |
+
def compute_fingerprint(payload: dict) -> str:
|
| 19 |
+
"""
|
| 20 |
+
MVP fingerprint: stable hash from fields that should represent the same upload.
|
| 21 |
+
Upgrade later to sha256(file-bytes) when you have a download URL.
|
| 22 |
+
"""
|
| 23 |
+
parts = {
|
| 24 |
+
"rid": norm(payload.get("rid")),
|
| 25 |
+
"file_type": norm(payload.get("file_type")),
|
| 26 |
+
"case_vendor_rid": norm(payload.get("case_vendor_rid")),
|
| 27 |
+
"date_modified": norm(payload.get("date_modified")),
|
| 28 |
+
}
|
| 29 |
+
raw = json.dumps(parts, sort_keys=True, separators=(",", ":")).encode("utf-8")
|
| 30 |
+
return hashlib.sha256(raw).hexdigest()
|
| 31 |
+
|
| 32 |
+
# ultra-light “db” for HF: in-memory set (fine for relay testing)
|
| 33 |
+
SEEN = set()
|
| 34 |
+
|
| 35 |
+
@app.post("/webhook")
|
| 36 |
+
async def webhook(req: Request):
|
| 37 |
+
payload = await req.json()
|
| 38 |
+
|
| 39 |
+
# 1) processing state default
|
| 40 |
+
state = norm(payload.get("sa_processing_state"))
|
| 41 |
+
if not state:
|
| 42 |
+
payload["sa_processing_state"] = "new" # null -> new [oai_citation:3‡API Integration Setup Guide_otter.ai.txt](sediment://file_0000000079e8722fb44223541f5b71cb)
|
| 43 |
+
|
| 44 |
+
# 2) fingerprint + dedupe
|
| 45 |
+
fp = norm(payload.get("sa_fingerprint"))
|
| 46 |
+
if not fp:
|
| 47 |
+
fp = compute_fingerprint(payload)
|
| 48 |
+
payload["sa_fingerprint"] = fp
|
| 49 |
+
|
| 50 |
+
if fp in SEEN:
|
| 51 |
+
payload["sa_processing_state"] = "duplicate"
|
| 52 |
+
return {"ok": True, "duplicate": True, "fingerprint": fp}
|
| 53 |
+
|
| 54 |
+
SEEN.add(fp)
|
| 55 |
+
|
| 56 |
+
# 3) forward/relay
|
| 57 |
+
try:
|
| 58 |
+
r = requests.post(WAKEUP_URL, json=payload, timeout=TIMEOUT)
|
| 59 |
+
return {
|
| 60 |
+
"ok": True,
|
| 61 |
+
"forward_status": r.status_code,
|
| 62 |
+
"forward_body": r.text[:2000],
|
| 63 |
+
"fingerprint": fp,
|
| 64 |
+
}
|
| 65 |
+
except requests.RequestException as e:
|
| 66 |
+
raise HTTPException(status_code=502, detail=f"relay_failed: {e}")
|