Skip to content

Commit 5493837

Browse files
authored
Merge pull request #26 from minrk/more-apis
expose some more API endpoints as client methods
2 parents a5da879 + b852f18 commit 5493837

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

jupyter_health/ch_client.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@ def _api_request(
123123
# return None for empty response body
124124
return None
125125

126-
def _list_api_request(self, path, **kwargs):
126+
def _list_api_request(self, path: str, **kwargs) -> Generator[dict[str, Any]]:
127127
"""Get a list from an /api/v1 endpoint"""
128128
r: dict = self._api_request(path, **kwargs)
129129
yield from r["results"]
130130
# TODO: handle pagination fields
131131

132-
def _fhir_list_api_request(self, path, **kwargs):
132+
def _fhir_list_api_request(self, path: str, **kwargs) -> Generator[dict[str, Any]]:
133133
"""Get a list from a fhir endpoint"""
134134
r: dict = self._api_request(path, fhir=True, **kwargs)
135135
for entry in r["entry"]:
@@ -146,13 +146,46 @@ def get_user(self) -> dict[str, Any]:
146146
"""Get the current user"""
147147
return cast(dict[str, Any], self._api_request("users/profile"))
148148

149-
def list_patients(self) -> Generator[dict[str, Any]]:
150-
"""Return list of patient ids
149+
def get_patient(self, id: int) -> dict[str, Any]:
150+
"""Get a single patient by id"""
151+
return cast(dict[str, Any], self._api_request(f"patients/{id}"))
151152

152-
These are the keys that may be passed to e.g. fetch_data
153+
def list_patients(self) -> Generator[dict[str, dict[str, Any]]]:
154+
"""Return iterator of patients
155+
156+
Patient ids are the keys that may be passed to e.g. fetch_data
153157
"""
154158
return self._list_api_request("patients")
155159

160+
def get_patient_consents(self, patient_id: int) -> dict[str, Any]:
161+
"""Return patient consent status"""
162+
return cast(
163+
dict[str, Any], self._api_request(f"patients/{patient_id}/consents")
164+
)
165+
166+
def get_study(self, id: int) -> dict[str, Any]:
167+
"""Get a single study by id"""
168+
return cast(dict[str, Any], self._api_request(f"studies/{id}"))
169+
170+
def list_studies(self) -> Generator[dict[str, dict[str, Any]]]:
171+
"""Return iterator of studies
172+
173+
Only returns studies I have access to (i.e. owned by my organization(s))
174+
"""
175+
return self._list_api_request("studies")
176+
177+
def get_organization(self, id: int) -> dict[str, Any]:
178+
"""Get a single organization by id"""
179+
return cast(dict[str, Any], self._api_request(f"organizations/{id}"))
180+
181+
def list_organizations(self) -> Generator[dict[str, dict[str, Any]]]:
182+
"""Return iterator of all organizations
183+
184+
Includes all organizations, including those of which I am not a member.
185+
The ROOT organization has `id=0`.
186+
"""
187+
return self._list_api_request("organizations")
188+
156189
def list_observations(
157190
self,
158191
patient_id: str | None = None,
@@ -175,7 +208,7 @@ def list_observations(
175208
if code:
176209
if isinstance(code, Code):
177210
code = code.value
178-
if "|" not in code:
211+
if code is None or "|" not in code:
179212
# no code system specified, default to openmhealth
180213
code = f"https://w3id.org/openmhealth|{code}"
181214
params["code"] = code

jupyter_health/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pandas as pd
99

1010

11-
def flatten_dict(d: dict, prefix: str = "") -> dict:
11+
def flatten_dict(d: dict | list, prefix: str = "") -> dict:
1212
"""flatten a nested dictionary into
1313
1414
adds nested keys to flat key names, so

0 commit comments

Comments
 (0)