File size: 950 Bytes
5373a3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""
DrugCentral SMART API helper
Docs: https://drugcentral.org/api  (public; key optional)
• Returns {} on any error so orchestrator never fails.
• Optional DRUGCENTRAL_KEY (JWT) lifts the rate-limit.
"""

from __future__ import annotations
import os, httpx, asyncio
from functools import lru_cache
from typing import Dict

_TOKEN = os.getenv("DRUGCENTRAL_KEY")         # optional
_BASE  = "https://drugcentral.org/api/v1/drug"
_HDR   = {"Accept": "application/json"}
if _TOKEN:
    _HDR["Authorization"] = f"Bearer {_TOKEN}"

@lru_cache(maxsize=256)
async def fetch_drugcentral(name: str) -> Dict:
    """Return DrugCentral record or {}."""
    try:
        async with httpx.AsyncClient(timeout=10, headers=_HDR) as cli:
            r = await cli.get(_BASE, params={"name": name})
            if r.status_code == 404:
                return {}
            r.raise_for_status()
            return r.json()
    except Exception:
        return {}