|
|
|
|
|
import httpx |
|
|
|
OPENFDA_DRUG_EVENT = "https://api.fda.gov/drug/event.json" |
|
|
|
async def fetch_drug_safety(drug: str, max_results: int = 3): |
|
"""Fetch recent adverse event reports for the drug from OpenFDA.""" |
|
params = {"search": f'patient.drug.medicinalproduct:"{drug}"', "limit": max_results} |
|
async with httpx.AsyncClient() as client: |
|
try: |
|
resp = await client.get(OPENFDA_DRUG_EVENT, params=params) |
|
results = resp.json().get("results", []) |
|
output = [] |
|
for ev in results: |
|
output.append({ |
|
"safety_report_id": ev.get("safetyreportid"), |
|
"serious": ev.get("serious"), |
|
"reactions": [r["reactionmeddrapt"] for r in ev.get("patient", {}).get("reaction", [])], |
|
"receivedate": ev.get("receivedate"), |
|
}) |
|
return output |
|
except Exception: |
|
return [] |
|
|