hchevva commited on
Commit
a37148f
·
verified ·
1 Parent(s): 65f3341

Create ctx.py

Browse files
Files changed (1) hide show
  1. core/sources/ctx.py +45 -0
core/sources/ctx.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+ import re
3
+ import httpx
4
+
5
+ from ..config import settings
6
+ from ..validate import normalize_cas
7
+
8
+ DTXSID_IN_HTML = re.compile(r"DTXSID\d{7,}")
9
+
10
+ async def resolve_dtxsid_from_dashboard(search: str, http: httpx.AsyncClient) -> str | None:
11
+ url = f"https://comptox.epa.gov/dashboard/dsstoxdb/results?search={httpx.utils.quote(search, safe='')}"
12
+ r = await http.get(url, headers={"accept": "text/html"})
13
+ r.raise_for_status()
14
+ m = DTXSID_IN_HTML.search(r.text)
15
+ return m.group(0) if m else None
16
+
17
+ async def genetox_summary_by_dtxsid(dtxsid: str, http: httpx.AsyncClient) -> dict:
18
+ # confirmed working endpoint pattern
19
+ url = f"{settings.ctx_base_url}/hazard/genetox/summary/search/by-dtxsid/{httpx.utils.quote(dtxsid, safe='')}"
20
+ headers = {"accept": "application/json"}
21
+ if settings.ctx_api_key:
22
+ headers["x-api-key"] = settings.ctx_api_key
23
+
24
+ r = await http.get(url, headers=headers)
25
+ if r.status_code == 401:
26
+ return {"ok": False, "error": "CTX 401: missing/invalid x-api-key (set CTX_API_KEY secret).", "status": 401, "url": url}
27
+ if r.status_code >= 400:
28
+ return {"ok": False, "error": f"CTX {r.status_code}: {r.text[:600]}", "status": r.status_code, "url": url}
29
+ return {"ok": True, "summary": r.json(), "status": r.status_code, "url": url}
30
+
31
+ async def fetch_ctx_genetox(cas: str, http: httpx.AsyncClient) -> dict:
32
+ q = (cas or "").strip()
33
+ if not q:
34
+ return {"ok": False, "error": "Empty CAS."}
35
+
36
+ dtxsid = await resolve_dtxsid_from_dashboard(normalize_cas(q), http)
37
+ if not dtxsid:
38
+ return {
39
+ "ok": False,
40
+ "error": "No DTXSID found for this CAS via dashboard search.",
41
+ "resolveUrl": f"https://comptox.epa.gov/dashboard/dsstoxdb/results?search={httpx.utils.quote(q, safe='')}",
42
+ }
43
+
44
+ s = await genetox_summary_by_dtxsid(dtxsid, http)
45
+ return {"ok": s.get("ok", False), "dtxsid": dtxsid, **s}