File size: 761 Bytes
c9d29cb
 
 
 
 
 
 
c09fa6f
 
 
c9d29cb
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
"""ClinicalTrials.gov v2 async helper.
   Docs: https://clinicaltrials.gov/api/gui/ref/api_urls#v2
   Provides `search_trials_v2` which returns a list of study dicts.
"""

import httpx, asyncio
from typing import List, Dict

_BASE = "https://clinicaltrials.gov/api/v2/studies"

async def search_trials_v2(query: str, *, max_n: int = 20) -> List[Dict]:
    """Search CT.gov v2 endpoint and return list of studies."""
    params = {
        "query": query,
        "pageSize": max_n,
        "fields": "nctId,briefTitle,phase,status,startDate,conditions,interventions",
    }
    async with httpx.AsyncClient(timeout=15) as client:
        resp = await client.get(_BASE, params=params)
        resp.raise_for_status()
        return resp.json().get("studies", [])