-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path_client.py
More file actions
308 lines (262 loc) · 10.2 KB
/
_client.py
File metadata and controls
308 lines (262 loc) · 10.2 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
"""Main client classes for the OMOPHub SDK."""
from __future__ import annotations
from typing import Any
from ._config import (
DEFAULT_BASE_URL,
DEFAULT_MAX_RETRIES,
DEFAULT_TIMEOUT,
)
from ._config import (
api_key as default_api_key,
)
from ._exceptions import AuthenticationError
from ._http import AsyncHTTPClientImpl, SyncHTTPClient
from ._request import AsyncRequest, Request
from .resources.concepts import AsyncConcepts, Concepts
from .resources.domains import AsyncDomains, Domains
from .resources.fhir import AsyncFhir, Fhir
from .resources.hierarchy import AsyncHierarchy, Hierarchy
from .resources.mappings import AsyncMappings, Mappings
from .resources.relationships import AsyncRelationships, Relationships
from .resources.search import AsyncSearch, Search
from .resources.vocabularies import AsyncVocabularies, Vocabularies
class OMOPHub:
"""Synchronous OMOPHub API client.
Example:
>>> import omophub
>>> client = omophub.OMOPHub(api_key="oh_xxxxxxxxx")
>>> concept = client.concepts.get(201826)
>>> print(concept["concept_name"])
"Type 2 diabetes mellitus"
Or using the context manager:
>>> with omophub.OMOPHub(api_key="oh_xxx") as client:
... results = client.search.basic("diabetes")
"""
def __init__(
self,
api_key: str | None = None,
*,
base_url: str = DEFAULT_BASE_URL,
timeout: float = DEFAULT_TIMEOUT,
max_retries: int = DEFAULT_MAX_RETRIES,
vocab_version: str | None = None,
) -> None:
"""Initialize the OMOPHub client.
Args:
api_key: API key for authentication. If not provided, reads from
OMOPHUB_API_KEY environment variable or module-level omophub.api_key.
base_url: Base URL for the API. Defaults to https://api.omophub.com/v1
timeout: Request timeout in seconds. Defaults to 30.
max_retries: Maximum retry attempts for failed requests. Defaults to 3.
vocab_version: Optional vocabulary version (e.g., "2025.1").
If not specified, uses the latest version.
Raises:
AuthenticationError: If no API key is provided.
"""
self._api_key = api_key or default_api_key
if not self._api_key:
raise AuthenticationError(
"API key is required. Provide it as an argument, set the "
"OMOPHUB_API_KEY environment variable, or set omophub.api_key.",
status_code=401,
)
self._base_url = base_url.rstrip("/")
self._timeout = timeout
self._max_retries = max_retries
self._vocab_version = vocab_version
# Initialize HTTP client
self._http_client = SyncHTTPClient(
timeout=timeout,
max_retries=max_retries,
)
# Initialize request handler
self._request: Request[Any] = Request(
http_client=self._http_client,
base_url=self._base_url,
api_key=self._api_key,
vocab_version=self._vocab_version,
)
# Initialize resources
self._concepts: Concepts | None = None
self._search: Search | None = None
self._hierarchy: Hierarchy | None = None
self._relationships: Relationships | None = None
self._mappings: Mappings | None = None
self._vocabularies: Vocabularies | None = None
self._domains: Domains | None = None
self._fhir: Fhir | None = None
@property
def fhir(self) -> Fhir:
"""Access the FHIR resolver resource."""
if self._fhir is None:
self._fhir = Fhir(self._request)
return self._fhir
@property
def concepts(self) -> Concepts:
"""Access the concepts resource."""
if self._concepts is None:
self._concepts = Concepts(self._request)
return self._concepts
@property
def search(self) -> Search:
"""Access the search resource."""
if self._search is None:
self._search = Search(self._request)
return self._search
@property
def hierarchy(self) -> Hierarchy:
"""Access the hierarchy resource."""
if self._hierarchy is None:
self._hierarchy = Hierarchy(self._request)
return self._hierarchy
@property
def relationships(self) -> Relationships:
"""Access the relationships resource."""
if self._relationships is None:
self._relationships = Relationships(self._request)
return self._relationships
@property
def mappings(self) -> Mappings:
"""Access the mappings resource."""
if self._mappings is None:
self._mappings = Mappings(self._request)
return self._mappings
@property
def vocabularies(self) -> Vocabularies:
"""Access the vocabularies resource."""
if self._vocabularies is None:
self._vocabularies = Vocabularies(self._request)
return self._vocabularies
@property
def domains(self) -> Domains:
"""Access the domains resource."""
if self._domains is None:
self._domains = Domains(self._request)
return self._domains
def close(self) -> None:
"""Close the HTTP client and release resources."""
self._http_client.close()
def __enter__(self) -> OMOPHub:
"""Enter context manager."""
return self
def __exit__(self, *args: Any) -> None:
"""Exit context manager and close client."""
self.close()
class AsyncOMOPHub:
"""Asynchronous OMOPHub API client.
Example:
>>> import omophub
>>> async with omophub.AsyncOMOPHub(api_key="oh_xxx") as client:
... concept = await client.concepts.get(201826)
... print(concept["concept_name"])
"""
def __init__(
self,
api_key: str | None = None,
*,
base_url: str = DEFAULT_BASE_URL,
timeout: float = DEFAULT_TIMEOUT,
max_retries: int = DEFAULT_MAX_RETRIES,
vocab_version: str | None = None,
) -> None:
"""Initialize the async OMOPHub client.
Args:
api_key: API key for authentication. If not provided, reads from
OMOPHUB_API_KEY environment variable or module-level omophub.api_key.
base_url: Base URL for the API. Defaults to https://api.omophub.com/v1
timeout: Request timeout in seconds. Defaults to 30.
max_retries: Maximum retry attempts for failed requests. Defaults to 3.
vocab_version: Optional vocabulary version (e.g., "2025.1").
If not specified, uses the latest version.
Raises:
AuthenticationError: If no API key is provided.
"""
self._api_key = api_key or default_api_key
if not self._api_key:
raise AuthenticationError(
"API key is required. Provide it as an argument, set the "
"OMOPHUB_API_KEY environment variable, or set omophub.api_key.",
status_code=401,
)
self._base_url = base_url.rstrip("/")
self._timeout = timeout
self._max_retries = max_retries
self._vocab_version = vocab_version
# Initialize HTTP client
self._http_client = AsyncHTTPClientImpl(
timeout=timeout,
max_retries=max_retries,
)
# Initialize request handler
self._request: AsyncRequest[Any] = AsyncRequest(
http_client=self._http_client,
base_url=self._base_url,
api_key=self._api_key,
vocab_version=self._vocab_version,
)
# Initialize resources
self._concepts: AsyncConcepts | None = None
self._search: AsyncSearch | None = None
self._hierarchy: AsyncHierarchy | None = None
self._relationships: AsyncRelationships | None = None
self._mappings: AsyncMappings | None = None
self._vocabularies: AsyncVocabularies | None = None
self._domains: AsyncDomains | None = None
self._fhir: AsyncFhir | None = None
@property
def fhir(self) -> AsyncFhir:
"""Access the FHIR resolver resource."""
if self._fhir is None:
self._fhir = AsyncFhir(self._request)
return self._fhir
@property
def concepts(self) -> AsyncConcepts:
"""Access the concepts resource."""
if self._concepts is None:
self._concepts = AsyncConcepts(self._request)
return self._concepts
@property
def search(self) -> AsyncSearch:
"""Access the search resource."""
if self._search is None:
self._search = AsyncSearch(self._request)
return self._search
@property
def hierarchy(self) -> AsyncHierarchy:
"""Access the hierarchy resource."""
if self._hierarchy is None:
self._hierarchy = AsyncHierarchy(self._request)
return self._hierarchy
@property
def relationships(self) -> AsyncRelationships:
"""Access the relationships resource."""
if self._relationships is None:
self._relationships = AsyncRelationships(self._request)
return self._relationships
@property
def mappings(self) -> AsyncMappings:
"""Access the mappings resource."""
if self._mappings is None:
self._mappings = AsyncMappings(self._request)
return self._mappings
@property
def vocabularies(self) -> AsyncVocabularies:
"""Access the vocabularies resource."""
if self._vocabularies is None:
self._vocabularies = AsyncVocabularies(self._request)
return self._vocabularies
@property
def domains(self) -> AsyncDomains:
"""Access the domains resource."""
if self._domains is None:
self._domains = AsyncDomains(self._request)
return self._domains
async def close(self) -> None:
"""Close the HTTP client and release resources."""
await self._http_client.close()
async def __aenter__(self) -> AsyncOMOPHub:
"""Enter async context manager."""
return self
async def __aexit__(self, *args: Any) -> None:
"""Exit async context manager and close client."""
await self.close()