Create drugcentral_ext.py
Browse files- mcp/drugcentral_ext.py +30 -0
mcp/drugcentral_ext.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
DrugCentral SMART API helper
|
| 3 |
+
Docs: https://drugcentral.org/api (public; key optional)
|
| 4 |
+
• Returns {} on any error so orchestrator never fails.
|
| 5 |
+
• Optional DRUGCENTRAL_KEY (JWT) lifts the rate-limit.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
import os, httpx, asyncio
|
| 10 |
+
from functools import lru_cache
|
| 11 |
+
from typing import Dict
|
| 12 |
+
|
| 13 |
+
_TOKEN = os.getenv("DRUGCENTRAL_KEY") # optional
|
| 14 |
+
_BASE = "https://drugcentral.org/api/v1/drug"
|
| 15 |
+
_HDR = {"Accept": "application/json"}
|
| 16 |
+
if _TOKEN:
|
| 17 |
+
_HDR["Authorization"] = f"Bearer {_TOKEN}"
|
| 18 |
+
|
| 19 |
+
@lru_cache(maxsize=256)
|
| 20 |
+
async def fetch_drugcentral(name: str) -> Dict:
|
| 21 |
+
"""Return DrugCentral record or {}."""
|
| 22 |
+
try:
|
| 23 |
+
async with httpx.AsyncClient(timeout=10, headers=_HDR) as cli:
|
| 24 |
+
r = await cli.get(_BASE, params={"name": name})
|
| 25 |
+
if r.status_code == 404:
|
| 26 |
+
return {}
|
| 27 |
+
r.raise_for_status()
|
| 28 |
+
return r.json()
|
| 29 |
+
except Exception:
|
| 30 |
+
return {}
|