pytrials-v2 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pytrials/__init__.py +63 -0
- pytrials/client.py +87 -0
- pytrials/errors.py +28 -0
- pytrials/models/__init__.py +33 -0
- pytrials/models/enums.py +46 -0
- pytrials/models/search.py +17 -0
- pytrials/models/study.py +133 -0
- pytrials/modules/__init__.py +7 -0
- pytrials/modules/studies.py +81 -0
- pytrials_v2-0.1.0.dist-info/METADATA +82 -0
- pytrials_v2-0.1.0.dist-info/RECORD +13 -0
- pytrials_v2-0.1.0.dist-info/WHEEL +4 -0
- pytrials_v2-0.1.0.dist-info/licenses/LICENSE +21 -0
pytrials/__init__.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""pytrials-v2: a modern, fully-typed Python SDK for the ClinicalTrials.gov API v2."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from .client import CTGClient
|
|
6
|
+
from .errors import CTGError, NotFoundError, RateLimitError
|
|
7
|
+
from .models import OverallStatus, Phase, Study, StudySearchResult, StudyType
|
|
8
|
+
from .modules.studies import StudiesModule
|
|
9
|
+
|
|
10
|
+
__version__ = "0.1.0"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class ClinicalTrials:
|
|
14
|
+
"""The main entry point for the SDK.
|
|
15
|
+
|
|
16
|
+
Exposes module namespaces (currently :attr:`studies`) over a shared HTTP
|
|
17
|
+
client. Async support, the QueryBuilder, pagination, and stats endpoints are
|
|
18
|
+
planned for later releases.
|
|
19
|
+
|
|
20
|
+
Example:
|
|
21
|
+
>>> ctg = ClinicalTrials()
|
|
22
|
+
>>> results = ctg.studies.search(condition="breast cancer")
|
|
23
|
+
>>> study = ctg.studies.get("NCT04852770")
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
def __init__(
|
|
27
|
+
self,
|
|
28
|
+
base_url: str = "https://clinicaltrials.gov/api/v2",
|
|
29
|
+
timeout: float = 30.0,
|
|
30
|
+
page_size: int = 100,
|
|
31
|
+
) -> None:
|
|
32
|
+
self._client = CTGClient(
|
|
33
|
+
base_url=base_url,
|
|
34
|
+
timeout=timeout,
|
|
35
|
+
default_page_size=page_size,
|
|
36
|
+
)
|
|
37
|
+
self.studies = StudiesModule(self._client)
|
|
38
|
+
|
|
39
|
+
def close(self) -> None:
|
|
40
|
+
"""Close the underlying HTTP client."""
|
|
41
|
+
self._client.close()
|
|
42
|
+
|
|
43
|
+
def __enter__(self) -> ClinicalTrials:
|
|
44
|
+
return self
|
|
45
|
+
|
|
46
|
+
def __exit__(self, *exc: object) -> None:
|
|
47
|
+
self.close()
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
__all__ = [
|
|
51
|
+
"CTGClient",
|
|
52
|
+
"CTGError",
|
|
53
|
+
"ClinicalTrials",
|
|
54
|
+
"NotFoundError",
|
|
55
|
+
"OverallStatus",
|
|
56
|
+
"Phase",
|
|
57
|
+
"RateLimitError",
|
|
58
|
+
"StudiesModule",
|
|
59
|
+
"Study",
|
|
60
|
+
"StudySearchResult",
|
|
61
|
+
"StudyType",
|
|
62
|
+
"__version__",
|
|
63
|
+
]
|
pytrials/client.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""Low level HTTP client wrapping httpx for the ClinicalTrials.gov API v2."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any, NoReturn
|
|
6
|
+
|
|
7
|
+
import httpx
|
|
8
|
+
|
|
9
|
+
from .errors import CTGError, NotFoundError, RateLimitError
|
|
10
|
+
|
|
11
|
+
DEFAULT_BASE_URL = "https://clinicaltrials.gov/api/v2"
|
|
12
|
+
DEFAULT_TIMEOUT = 30.0
|
|
13
|
+
DEFAULT_PAGE_SIZE = 100
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class CTGClient:
|
|
17
|
+
"""Synchronous HTTP client for the ClinicalTrials.gov API v2.
|
|
18
|
+
|
|
19
|
+
This is an internal building block. Most users should go through the
|
|
20
|
+
:class:`pytrials.ClinicalTrials` facade and its module namespaces.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
def __init__(
|
|
24
|
+
self,
|
|
25
|
+
base_url: str = DEFAULT_BASE_URL,
|
|
26
|
+
timeout: float = DEFAULT_TIMEOUT,
|
|
27
|
+
default_page_size: int = DEFAULT_PAGE_SIZE,
|
|
28
|
+
) -> None:
|
|
29
|
+
self.base_url = base_url.rstrip("/")
|
|
30
|
+
self.default_page_size = default_page_size
|
|
31
|
+
self._client = httpx.Client(
|
|
32
|
+
base_url=self.base_url,
|
|
33
|
+
timeout=timeout,
|
|
34
|
+
headers={"Accept": "application/json"},
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
def get(self, path: str, params: dict[str, Any] | None = None) -> dict[str, Any]:
|
|
38
|
+
"""Issue a GET request and return the parsed JSON body.
|
|
39
|
+
|
|
40
|
+
Raises :class:`NotFoundError` on 404, :class:`RateLimitError` on 429,
|
|
41
|
+
and :class:`CTGError` on any other 4xx or 5xx response.
|
|
42
|
+
"""
|
|
43
|
+
cleaned = _clean_params(params or {})
|
|
44
|
+
response = self._client.get(path, params=cleaned)
|
|
45
|
+
if response.is_success:
|
|
46
|
+
data: dict[str, Any] = response.json()
|
|
47
|
+
return data
|
|
48
|
+
self._raise_for_status(response)
|
|
49
|
+
|
|
50
|
+
@staticmethod
|
|
51
|
+
def _raise_for_status(response: httpx.Response) -> NoReturn:
|
|
52
|
+
status = response.status_code
|
|
53
|
+
message = _extract_message(response)
|
|
54
|
+
if status == 404:
|
|
55
|
+
raise NotFoundError(status, message)
|
|
56
|
+
if status == 429:
|
|
57
|
+
raise RateLimitError(status, message)
|
|
58
|
+
raise CTGError(status, message)
|
|
59
|
+
|
|
60
|
+
def close(self) -> None:
|
|
61
|
+
"""Close the underlying httpx client and its connection pool."""
|
|
62
|
+
self._client.close()
|
|
63
|
+
|
|
64
|
+
def __enter__(self) -> CTGClient:
|
|
65
|
+
return self
|
|
66
|
+
|
|
67
|
+
def __exit__(self, *exc: object) -> None:
|
|
68
|
+
self.close()
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _clean_params(params: dict[str, Any]) -> dict[str, Any]:
|
|
72
|
+
"""Drop None values so they are not serialized into the query string."""
|
|
73
|
+
return {key: value for key, value in params.items() if value is not None}
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _extract_message(response: httpx.Response) -> str:
|
|
77
|
+
"""Pull a useful error message out of an error response body."""
|
|
78
|
+
try:
|
|
79
|
+
body = response.json()
|
|
80
|
+
except ValueError:
|
|
81
|
+
return response.text or response.reason_phrase
|
|
82
|
+
if isinstance(body, dict):
|
|
83
|
+
for key in ("message", "detail", "error"):
|
|
84
|
+
value = body.get(key)
|
|
85
|
+
if isinstance(value, str):
|
|
86
|
+
return value
|
|
87
|
+
return response.reason_phrase
|
pytrials/errors.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"""Exception types raised by the SDK."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class CTGError(Exception):
|
|
7
|
+
"""Base error for all ClinicalTrials.gov API failures.
|
|
8
|
+
|
|
9
|
+
Carries the HTTP status code (when available) and a human readable message.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
def __init__(self, status_code: int | None, message: str) -> None:
|
|
13
|
+
self.status_code = status_code
|
|
14
|
+
self.message = message
|
|
15
|
+
super().__init__(message)
|
|
16
|
+
|
|
17
|
+
def __str__(self) -> str:
|
|
18
|
+
if self.status_code is None:
|
|
19
|
+
return self.message
|
|
20
|
+
return f"[{self.status_code}] {self.message}"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class NotFoundError(CTGError):
|
|
24
|
+
"""Raised on HTTP 404, for example when an NCT ID does not exist."""
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class RateLimitError(CTGError):
|
|
28
|
+
"""Raised on HTTP 429 when the API rate limit (around 50 req/min) is hit."""
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""Pydantic models for ClinicalTrials.gov API v2 responses."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from .enums import OverallStatus, Phase, StudyType
|
|
6
|
+
from .search import StudySearchResult
|
|
7
|
+
from .study import (
|
|
8
|
+
ConditionsModule,
|
|
9
|
+
DesignModule,
|
|
10
|
+
IdentificationModule,
|
|
11
|
+
Organization,
|
|
12
|
+
ProtocolSection,
|
|
13
|
+
Sponsor,
|
|
14
|
+
SponsorCollaboratorsModule,
|
|
15
|
+
StatusModule,
|
|
16
|
+
Study,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
__all__ = [
|
|
20
|
+
"ConditionsModule",
|
|
21
|
+
"DesignModule",
|
|
22
|
+
"IdentificationModule",
|
|
23
|
+
"Organization",
|
|
24
|
+
"OverallStatus",
|
|
25
|
+
"Phase",
|
|
26
|
+
"ProtocolSection",
|
|
27
|
+
"Sponsor",
|
|
28
|
+
"SponsorCollaboratorsModule",
|
|
29
|
+
"StatusModule",
|
|
30
|
+
"Study",
|
|
31
|
+
"StudySearchResult",
|
|
32
|
+
"StudyType",
|
|
33
|
+
]
|
pytrials/models/enums.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""Enumerated values from the ClinicalTrials.gov API v2 data model.
|
|
2
|
+
|
|
3
|
+
These mirror the real, case sensitive values the API accepts and returns.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from enum import Enum
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class OverallStatus(str, Enum):
|
|
12
|
+
"""Recruitment status of a study (statusModule.overallStatus)."""
|
|
13
|
+
|
|
14
|
+
ACTIVE_NOT_RECRUITING = "ACTIVE_NOT_RECRUITING"
|
|
15
|
+
COMPLETED = "COMPLETED"
|
|
16
|
+
ENROLLING_BY_INVITATION = "ENROLLING_BY_INVITATION"
|
|
17
|
+
NOT_YET_RECRUITING = "NOT_YET_RECRUITING"
|
|
18
|
+
RECRUITING = "RECRUITING"
|
|
19
|
+
SUSPENDED = "SUSPENDED"
|
|
20
|
+
TERMINATED = "TERMINATED"
|
|
21
|
+
WITHDRAWN = "WITHDRAWN"
|
|
22
|
+
AVAILABLE = "AVAILABLE"
|
|
23
|
+
NO_LONGER_AVAILABLE = "NO_LONGER_AVAILABLE"
|
|
24
|
+
TEMPORARILY_NOT_AVAILABLE = "TEMPORARILY_NOT_AVAILABLE"
|
|
25
|
+
APPROVED_FOR_MARKETING = "APPROVED_FOR_MARKETING"
|
|
26
|
+
WITHHELD = "WITHHELD"
|
|
27
|
+
UNKNOWN = "UNKNOWN"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class Phase(str, Enum):
|
|
31
|
+
"""Clinical trial phase (designModule.phases)."""
|
|
32
|
+
|
|
33
|
+
NA = "NA"
|
|
34
|
+
EARLY_PHASE1 = "EARLY_PHASE1"
|
|
35
|
+
PHASE1 = "PHASE1"
|
|
36
|
+
PHASE2 = "PHASE2"
|
|
37
|
+
PHASE3 = "PHASE3"
|
|
38
|
+
PHASE4 = "PHASE4"
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class StudyType(str, Enum):
|
|
42
|
+
"""Type of study (designModule.studyType)."""
|
|
43
|
+
|
|
44
|
+
INTERVENTIONAL = "INTERVENTIONAL"
|
|
45
|
+
OBSERVATIONAL = "OBSERVATIONAL"
|
|
46
|
+
EXPANDED_ACCESS = "EXPANDED_ACCESS"
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""Models for the /studies search response."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pydantic import BaseModel, ConfigDict, Field
|
|
6
|
+
|
|
7
|
+
from .study import Study
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class StudySearchResult(BaseModel):
|
|
11
|
+
"""The parsed response from a GET /studies search request."""
|
|
12
|
+
|
|
13
|
+
model_config = ConfigDict(populate_by_name=True, extra="allow")
|
|
14
|
+
|
|
15
|
+
studies: list[Study] = Field(default_factory=list)
|
|
16
|
+
next_page_token: str | None = Field(default=None, alias="nextPageToken")
|
|
17
|
+
total_count: int | None = Field(default=None, alias="totalCount")
|
pytrials/models/study.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"""Pydantic v2 models mirroring the ClinicalTrials.gov API v2 study record.
|
|
2
|
+
|
|
3
|
+
The models are intentionally minimal for v0.1 but use the real API field names
|
|
4
|
+
(camelCase) via aliases. Every model allows extra fields so that new API fields
|
|
5
|
+
do not break deserialization, and every field is optional because the API has
|
|
6
|
+
many nullable values.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from pydantic import BaseModel, ConfigDict, Field
|
|
12
|
+
|
|
13
|
+
from .enums import OverallStatus, Phase, StudyType
|
|
14
|
+
|
|
15
|
+
_CONFIG = ConfigDict(populate_by_name=True, extra="allow")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Organization(BaseModel):
|
|
19
|
+
"""Sponsoring or submitting organization (identificationModule.organization)."""
|
|
20
|
+
|
|
21
|
+
model_config = _CONFIG
|
|
22
|
+
|
|
23
|
+
full_name: str | None = Field(default=None, alias="fullName")
|
|
24
|
+
org_class: str | None = Field(default=None, alias="class")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class IdentificationModule(BaseModel):
|
|
28
|
+
"""Identifiers and titles (protocolSection.identificationModule)."""
|
|
29
|
+
|
|
30
|
+
model_config = _CONFIG
|
|
31
|
+
|
|
32
|
+
nct_id: str | None = Field(default=None, alias="nctId")
|
|
33
|
+
brief_title: str | None = Field(default=None, alias="briefTitle")
|
|
34
|
+
official_title: str | None = Field(default=None, alias="officialTitle")
|
|
35
|
+
organization: Organization | None = None
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class StatusModule(BaseModel):
|
|
39
|
+
"""Recruitment status and key dates (protocolSection.statusModule)."""
|
|
40
|
+
|
|
41
|
+
model_config = _CONFIG
|
|
42
|
+
|
|
43
|
+
overall_status: OverallStatus | None = Field(default=None, alias="overallStatus")
|
|
44
|
+
status_verified_date: str | None = Field(default=None, alias="statusVerifiedDate")
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class Sponsor(BaseModel):
|
|
48
|
+
"""A lead sponsor or collaborator."""
|
|
49
|
+
|
|
50
|
+
model_config = _CONFIG
|
|
51
|
+
|
|
52
|
+
name: str | None = None
|
|
53
|
+
org_class: str | None = Field(default=None, alias="class")
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class SponsorCollaboratorsModule(BaseModel):
|
|
57
|
+
"""Lead sponsor and collaborators (protocolSection.sponsorCollaboratorsModule)."""
|
|
58
|
+
|
|
59
|
+
model_config = _CONFIG
|
|
60
|
+
|
|
61
|
+
lead_sponsor: Sponsor | None = Field(default=None, alias="leadSponsor")
|
|
62
|
+
collaborators: list[Sponsor] = Field(default_factory=list)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class ConditionsModule(BaseModel):
|
|
66
|
+
"""Conditions and keywords (protocolSection.conditionsModule)."""
|
|
67
|
+
|
|
68
|
+
model_config = _CONFIG
|
|
69
|
+
|
|
70
|
+
conditions: list[str] = Field(default_factory=list)
|
|
71
|
+
keywords: list[str] = Field(default_factory=list)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class DesignModule(BaseModel):
|
|
75
|
+
"""Study design (protocolSection.designModule)."""
|
|
76
|
+
|
|
77
|
+
model_config = _CONFIG
|
|
78
|
+
|
|
79
|
+
study_type: StudyType | None = Field(default=None, alias="studyType")
|
|
80
|
+
phases: list[Phase] = Field(default_factory=list)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class ProtocolSection(BaseModel):
|
|
84
|
+
"""The protocol section of a study record."""
|
|
85
|
+
|
|
86
|
+
model_config = _CONFIG
|
|
87
|
+
|
|
88
|
+
identification_module: IdentificationModule | None = Field(
|
|
89
|
+
default=None, alias="identificationModule"
|
|
90
|
+
)
|
|
91
|
+
status_module: StatusModule | None = Field(default=None, alias="statusModule")
|
|
92
|
+
sponsor_collaborators_module: SponsorCollaboratorsModule | None = Field(
|
|
93
|
+
default=None, alias="sponsorCollaboratorsModule"
|
|
94
|
+
)
|
|
95
|
+
conditions_module: ConditionsModule | None = Field(
|
|
96
|
+
default=None, alias="conditionsModule"
|
|
97
|
+
)
|
|
98
|
+
design_module: DesignModule | None = Field(default=None, alias="designModule")
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class Study(BaseModel):
|
|
102
|
+
"""A single ClinicalTrials.gov study record."""
|
|
103
|
+
|
|
104
|
+
model_config = _CONFIG
|
|
105
|
+
|
|
106
|
+
protocol_section: ProtocolSection | None = Field(
|
|
107
|
+
default=None, alias="protocolSection"
|
|
108
|
+
)
|
|
109
|
+
has_results: bool = Field(default=False, alias="hasResults")
|
|
110
|
+
|
|
111
|
+
@property
|
|
112
|
+
def nct_id(self) -> str | None:
|
|
113
|
+
"""Convenience accessor for the NCT identifier."""
|
|
114
|
+
protocol = self.protocol_section
|
|
115
|
+
if protocol is None or protocol.identification_module is None:
|
|
116
|
+
return None
|
|
117
|
+
return protocol.identification_module.nct_id
|
|
118
|
+
|
|
119
|
+
@property
|
|
120
|
+
def brief_title(self) -> str | None:
|
|
121
|
+
"""Convenience accessor for the brief title."""
|
|
122
|
+
protocol = self.protocol_section
|
|
123
|
+
if protocol is None or protocol.identification_module is None:
|
|
124
|
+
return None
|
|
125
|
+
return protocol.identification_module.brief_title
|
|
126
|
+
|
|
127
|
+
@property
|
|
128
|
+
def overall_status(self) -> OverallStatus | None:
|
|
129
|
+
"""Convenience accessor for the recruitment status."""
|
|
130
|
+
protocol = self.protocol_section
|
|
131
|
+
if protocol is None or protocol.status_module is None:
|
|
132
|
+
return None
|
|
133
|
+
return protocol.status_module.overall_status
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""The studies module: search and fetch study records."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from ..client import CTGClient
|
|
8
|
+
from ..models.enums import OverallStatus, Phase
|
|
9
|
+
from ..models.search import StudySearchResult
|
|
10
|
+
from ..models.study import Study
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _as_value_list(value: Any) -> list[str]:
|
|
14
|
+
"""Coerce a scalar, enum, or sequence into a list of string values."""
|
|
15
|
+
if value is None:
|
|
16
|
+
return []
|
|
17
|
+
if isinstance(value, (str, OverallStatus, Phase)):
|
|
18
|
+
return [_to_str(value)]
|
|
19
|
+
if isinstance(value, (list, tuple, set)):
|
|
20
|
+
return [_to_str(item) for item in value]
|
|
21
|
+
return [_to_str(value)]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _to_str(value: Any) -> str:
|
|
25
|
+
if isinstance(value, OverallStatus):
|
|
26
|
+
return value.value
|
|
27
|
+
if isinstance(value, Phase):
|
|
28
|
+
return value.value
|
|
29
|
+
return str(value)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class StudiesModule:
|
|
33
|
+
"""Operations against the /studies endpoints."""
|
|
34
|
+
|
|
35
|
+
def __init__(self, client: CTGClient) -> None:
|
|
36
|
+
self._client = client
|
|
37
|
+
|
|
38
|
+
def search(
|
|
39
|
+
self,
|
|
40
|
+
condition: str | None = None,
|
|
41
|
+
status: str | OverallStatus | list[str | OverallStatus] | None = None,
|
|
42
|
+
phase: str | Phase | list[str | Phase] | None = None,
|
|
43
|
+
page_size: int | None = None,
|
|
44
|
+
sort: str | None = None,
|
|
45
|
+
**extra: Any,
|
|
46
|
+
) -> StudySearchResult:
|
|
47
|
+
"""Search studies and return a validated :class:`StudySearchResult`.
|
|
48
|
+
|
|
49
|
+
Maps the friendly keyword arguments onto the real API query parameters:
|
|
50
|
+
``query.cond``, ``filter.overallStatus``, ``aggFilters`` (for phase),
|
|
51
|
+
``pageSize``, ``sort``, and ``countTotal=true``.
|
|
52
|
+
"""
|
|
53
|
+
params: dict[str, Any] = {
|
|
54
|
+
"countTotal": "true",
|
|
55
|
+
"pageSize": page_size if page_size is not None else self._client.default_page_size,
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if condition is not None:
|
|
59
|
+
params["query.cond"] = condition
|
|
60
|
+
|
|
61
|
+
statuses = _as_value_list(status)
|
|
62
|
+
if statuses:
|
|
63
|
+
params["filter.overallStatus"] = ",".join(statuses)
|
|
64
|
+
|
|
65
|
+
phases = _as_value_list(phase)
|
|
66
|
+
if phases:
|
|
67
|
+
agg = ",".join(f"phase:{value}" for value in phases)
|
|
68
|
+
params["aggFilters"] = agg
|
|
69
|
+
|
|
70
|
+
if sort is not None:
|
|
71
|
+
params["sort"] = sort
|
|
72
|
+
|
|
73
|
+
params.update(extra)
|
|
74
|
+
|
|
75
|
+
data = self._client.get("/studies", params=params)
|
|
76
|
+
return StudySearchResult.model_validate(data)
|
|
77
|
+
|
|
78
|
+
def get(self, nct_id: str) -> Study:
|
|
79
|
+
"""Fetch a single study by NCT ID via /studies/{nctId}."""
|
|
80
|
+
data = self._client.get(f"/studies/{nct_id}")
|
|
81
|
+
return Study.model_validate(data)
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pytrials-v2
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A modern, fully-typed Python SDK for the ClinicalTrials.gov API v2.
|
|
5
|
+
Project-URL: Homepage, https://github.com/raami/pytrials-v2
|
|
6
|
+
Project-URL: Repository, https://github.com/raami/pytrials-v2
|
|
7
|
+
Author: Prahlaad Ram
|
|
8
|
+
License: MIT License
|
|
9
|
+
|
|
10
|
+
Copyright (c) 2026 Prahlaad R.
|
|
11
|
+
|
|
12
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
in the Software without restriction, including without limitation the rights
|
|
15
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
furnished to do so, subject to the following conditions:
|
|
18
|
+
|
|
19
|
+
The above copyright notice and this permission notice shall be included in all
|
|
20
|
+
copies or substantial portions of the Software.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
+
SOFTWARE.
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Keywords: api,clinical-trials,clinicaltrials,healthcare,sdk
|
|
31
|
+
Classifier: Development Status :: 3 - Alpha
|
|
32
|
+
Classifier: Intended Audience :: Science/Research
|
|
33
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
34
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
35
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
36
|
+
Classifier: Typing :: Typed
|
|
37
|
+
Requires-Python: >=3.10
|
|
38
|
+
Requires-Dist: httpx>=0.27
|
|
39
|
+
Requires-Dist: pydantic>=2
|
|
40
|
+
Description-Content-Type: text/markdown
|
|
41
|
+
|
|
42
|
+
# pytrials-v2
|
|
43
|
+
|
|
44
|
+
A modern, fully-typed Python SDK for the [ClinicalTrials.gov API v2](https://clinicaltrials.gov/data-api/api).
|
|
45
|
+
|
|
46
|
+
> Status: early development. See [PROJECT_PLAN.md](./PROJECT_PLAN.md) for the full design and roadmap.
|
|
47
|
+
|
|
48
|
+
## Why
|
|
49
|
+
|
|
50
|
+
The ClinicalTrials.gov API v2 (JSON, token pagination, OpenAPI 3.0) launched in 2024, and the old XML v1 API was retired. There is still no well-designed Python SDK for v2. pytrials-v2 aims to be the default: Pydantic-modeled, async-capable, with the ergonomic helpers clinical-trial data consumers actually need.
|
|
51
|
+
|
|
52
|
+
## What it offers
|
|
53
|
+
|
|
54
|
+
- Full Pydantic v2 models for every API response (real autocomplete, no dict-digging)
|
|
55
|
+
- A validating QueryBuilder that catches bad status, phase, and sort values before the request
|
|
56
|
+
- Async auto-pagination over `pageToken`
|
|
57
|
+
- DataFrame integration that flattens the nested study structure for analysis
|
|
58
|
+
- Date normalization across the API's inconsistent formats
|
|
59
|
+
- Built-in rate limiting (50 req/min) and retry with backoff
|
|
60
|
+
|
|
61
|
+
## Quickstart (planned API)
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
from pytrials import ClinicalTrials
|
|
65
|
+
|
|
66
|
+
ctg = ClinicalTrials()
|
|
67
|
+
|
|
68
|
+
results = ctg.studies.search(condition="breast cancer", status=["RECRUITING"], phase=["PHASE3"])
|
|
69
|
+
study = ctg.studies.get("NCT04852770")
|
|
70
|
+
df = ctg.studies.search(condition="diabetes", status=["RECRUITING"]).to_dataframe()
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Roadmap
|
|
74
|
+
|
|
75
|
+
- **v0.1.0 Core**: client, search/get, core models, error handling, PyPI publish
|
|
76
|
+
- **v0.2.0 Ergonomics**: QueryBuilder, async paginator, stats endpoints, rate limiting
|
|
77
|
+
- **v0.3.0 Data science**: DataFrame integration, docs site, 90%+ coverage
|
|
78
|
+
- **v1.0.0 Stable**: full results-section models, CLI, notebook examples
|
|
79
|
+
|
|
80
|
+
## License
|
|
81
|
+
|
|
82
|
+
MIT
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
pytrials/__init__.py,sha256=EJkJJIj10OHXoT2ivbd-Z4B1qTTVrkYM30h_MVD85A8,1638
|
|
2
|
+
pytrials/client.py,sha256=9NU_y-kNtJS6r9FOnwDGYd1LPKLdd_BRYaCne3SJVTQ,2863
|
|
3
|
+
pytrials/errors.py,sha256=57VSlvSXxT81i2oeZgOGM6Q8AjDNjClF_3jYbt_J1wg,805
|
|
4
|
+
pytrials/models/__init__.py,sha256=mOqbyJsCs0zf65GgvT8JJPLGSByMvi3JvUMzzDM-qMM,679
|
|
5
|
+
pytrials/models/enums.py,sha256=p7KBtGtUhaPQvxJd5IWmg55LRtj4tsiB2rFqARubix0,1261
|
|
6
|
+
pytrials/models/search.py,sha256=PLZDBJ6k97hAgl4iVcra9Tp6keYMAUhbDy4WNRK-HXE,536
|
|
7
|
+
pytrials/models/study.py,sha256=zQqW10fjGECmx207SNg2AFtGDSUnnCGvItjnUfdPY1Q,4349
|
|
8
|
+
pytrials/modules/__init__.py,sha256=q_M0t-AmLE4anwWnUErlSTzFj9zzXrn8w7_ap3QHlo4,163
|
|
9
|
+
pytrials/modules/studies.py,sha256=rezDYpz9q7HnHhCv33-oWTPdtk1xncieF4bbm4QTC6I,2586
|
|
10
|
+
pytrials_v2-0.1.0.dist-info/METADATA,sha256=8oJMzqsCL251rzpzAOuZz4jBFIneQnV7Q-w-WgL51SM,3657
|
|
11
|
+
pytrials_v2-0.1.0.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
|
|
12
|
+
pytrials_v2-0.1.0.dist-info/licenses/LICENSE,sha256=0c59Jt9ArzNKAPGxgNSIHwZBph_TAQOcu-9XHSEHc-s,1068
|
|
13
|
+
pytrials_v2-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Prahlaad R.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|