-
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathbase.py
More file actions
441 lines (372 loc) · 13.9 KB
/
base.py
File metadata and controls
441 lines (372 loc) · 13.9 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
"""The Base class for all REDCap methods"""
from __future__ import annotations
import json
from typing import (
Any,
Dict,
List,
Literal,
Optional,
cast,
overload,
Tuple,
TYPE_CHECKING,
Union,
)
from io import StringIO
from redcap.request import (
_ContentConfig,
_RCRequest,
RedcapError,
FileUpload,
Json,
)
if TYPE_CHECKING:
import pandas as pd
# We're designing class to be lazy by default, and not hit the API unless
# explicitly requested by the user
# return_type type aliases
FileMap = Tuple[bytes, dict]
class Base:
"""Base attributes and methods for the REDCap API"""
def __init__(
self,
url: str,
token: str,
verify_ssl: Union[bool, str] = True,
**request_kwargs,
):
"""Initialize a Project, validate url and token"""
self._validate_url_and_token(url, token)
self._url = url
self._token = token
self.verify_ssl = verify_ssl
self._validate_request_kwargs(**request_kwargs)
self._request_kwargs = request_kwargs
# attributes which require API calls
self._metadata: Optional[Json] = None
self._forms: Optional[List[str]] = None
self._field_names: Optional[List[str]] = None
self._def_field: Optional[str] = None
self._is_longitudinal: Optional[bool] = None
@property
def url(self) -> str:
"""API URL to a REDCap server"""
return self._url
@property
def token(self) -> str:
"""API token to a project"""
return self._token
@property
def metadata(self) -> Json:
"""Project metadata in JSON format"""
if self._metadata is None:
payload = self._initialize_payload("metadata", format_type="json")
self._metadata = cast(Json, self._call_api(payload, return_type="json"))
return self._metadata
@property
def forms(self) -> List[str]:
"""Project form names"""
if self._forms is None:
self._forms = list(set(self._filter_metadata(key="form_name")))
return self._forms
@property
def field_names(self) -> List[str]:
"""Project field names
Note:
These are survey field names, not export field names
"""
if self._field_names is None:
self._field_names = self._filter_metadata(key="field_name")
return self._field_names
@property
def def_field(self) -> str:
"""The 'record_id' field equivalent for a project"""
if self._def_field is None:
self._def_field = self.field_names[0]
return self._def_field
@property
def is_longitudinal(self) -> bool:
"""Whether or not this project is longitudinal"""
if self._is_longitudinal is None:
try:
payload = self._initialize_payload(
content="formEventMapping", format_type="json"
)
self._call_api(payload, return_type="json")
self._is_longitudinal = True
except RedcapError:
# we should only get a error back if there were no events defined
# for the project
self._is_longitudinal = False
return self._is_longitudinal
@staticmethod
def _validate_url_and_token(url: str, token: str) -> None:
"""Run basic validation on user supplied url and token"""
assert url, "Error! REDCap URL is missing"
assert token, "Error! REDCap token is missing"
url_actual_last_5 = url[-5:]
url_expected_last_5 = "/api/"
assert url_actual_last_5 == url_expected_last_5, (
f"Incorrect url format '{ url }', url must end with",
f"{ url_expected_last_5 }",
)
actual_token_len = len(token)
expected_token_len = 32
assert (
actual_token_len == expected_token_len
), f"Incorrect token format token must must be { expected_token_len } characters long"
@staticmethod
def _validate_request_kwargs(**request_kwargs):
"""Run basic validation on user supplied kwargs for requests"""
# list of kwargs hardcoded in _RCRequest.execute(...) and self._call_api(...)
hardcoded_kwargs = [
"url",
"data",
"verify, verify_ssl",
"return_headers",
"files",
"file",
]
unallowed_kwargs = [
kwarg for kwarg in request_kwargs if kwarg in hardcoded_kwargs
]
assert (
len(unallowed_kwargs) == 0
), f"Not allowed to define {unallowed_kwargs} when initiating object"
# pylint: disable=import-outside-toplevel
@staticmethod
def _read_csv(buf: StringIO, **df_kwargs) -> "pd.DataFrame":
"""Wrapper around pandas read_csv that handles EmptyDataError"""
import pandas as pd
from pandas.errors import EmptyDataError
try:
dataframe = pd.read_csv(buf, **df_kwargs)
except EmptyDataError:
dataframe = pd.DataFrame()
return dataframe
# pylint: enable=import-outside-toplevel
@staticmethod
def _lookup_return_type(
format_type: Literal["json", "csv", "xml", "df"],
request_type: Literal["export", "import", "delete"],
import_records_format: Optional[
Literal["count", "ids", "auto_ids", "nothing"]
] = None,
) -> Literal["json", "str", "int", "count_dict", "ids_list", "empty_json"]:
"""Look up a common return types based on format
Non-standard return types will need to be passed directly
to _call_api() via the return_type parameter.
Args:
format_type: The provided format for the API call
request_type:
The type of API request. Exports behave very differently
from imports/deletes
import_records_format:
Format options from the import_records method. We
need to use custom logic, because that method has
different possible return types compared to all other
methods
"""
if format_type in ["csv", "xml", "df"]:
return "str"
if format_type == "json":
if request_type == "export":
return "json"
if request_type in ["import", "delete"] and not import_records_format:
return "int"
if import_records_format in ["count", "auto_ids"]:
return "count_dict"
if import_records_format == "ids":
return "ids_list"
if import_records_format == "nothing":
return "empty_json"
raise ValueError(f"Invalid format_type: { format_type }")
@overload
def _filter_metadata(
self,
key: str,
field_name: None = None,
) -> list: ...
@overload
def _filter_metadata(self, key: str, field_name: str) -> str: ...
def _filter_metadata(self, key: str, field_name: Optional[str] = None):
"""Safely filter project metadata based off requested column and field_name"""
res: Union[list, str]
if field_name:
try:
res = str(
[
row[key]
for row in self.metadata
if row["field_name"] == field_name
][0]
)
except IndexError: # pragma: no cover
print(f"{ key } not in metadata field: { field_name }")
return ""
else:
res = [row[key] for row in self.metadata]
return res
def _initialize_payload(
self,
content: str,
format_type: Optional[Literal["json", "csv", "xml", "df"]] = None,
return_format_type: Optional[Literal["json", "csv", "xml"]] = None,
record_type: Literal["flat", "eav"] = "flat",
) -> Dict[str, Any]:
"""Create the default dictionary for payloads
This can be used as is for simple API requests or added to
for more complex API requests.
Args:
content:
The 'content' parameter documented in the REDCap API.
e.g. 'record', 'metadata', 'file', 'event', etc.
format_type: Format of the data returned for export methods
return_format_type: Format of the data returned for import/delete methods
record_type: The type of records being exported/imported
"""
payload = {"token": self.token, "content": content}
if format_type:
if format_type == "df":
payload["format"] = "csv"
else:
payload["format"] = format_type
if return_format_type:
payload["returnFormat"] = return_format_type
if content == "record":
payload["type"] = record_type
return payload
def _initialize_import_payload(
self,
to_import: Union[List[dict], str, "pd.DataFrame"],
import_format: Literal["json", "csv", "xml", "df"],
return_format_type: Literal["json", "csv", "xml"],
content: str,
) -> Dict[str, Any]:
"""Standardize the data to be imported and add it to the payload
Args:
to_import: array of dicts, csv/xml string, ``pandas.DataFrame``
import_format: Format of incoming data
return_format_type: Format of outgoing (returned) data
content: The kind of data that are imported
Returns:
payload: The initialized payload dictionary and updated format
"""
payload = self._initialize_payload(
content=content, return_format_type=return_format_type
)
if import_format == "df":
to_import = cast("pd.DataFrame", to_import)
buf = StringIO()
has_named_index = to_import.index.name is not None
to_import.to_csv(buf, index=has_named_index)
payload["data"] = buf.getvalue()
buf.close()
import_format = "csv"
elif import_format == "json":
payload["data"] = json.dumps(to_import, separators=(",", ":"))
else:
# don't do anything to csv/xml
to_import = cast("str", to_import)
payload["data"] = to_import
payload["format"] = import_format
return payload
def _return_data(
self,
response: Union[Json, str],
content: Literal[
"arm",
"dag",
"event",
"exportFieldNames",
"fileRepository",
"formEventMapping",
"instrument",
"log",
"metadata",
"participantList",
"project",
"record",
"report",
"user",
"userDagMapping",
"userRole",
"userRoleMapping",
"repeatingFormsEvents",
],
format_type: Literal["json", "csv", "xml", "df"],
df_kwargs: Optional[Dict[str, Any]] = None,
record_type: Literal["flat", "eav"] = "flat",
):
"""Handle returning data for export methods
This mostly just stores the logic for the default
`df_kwargs` value for export methods, when returning
a dataframe.
Args:
response: Output from _call_api
content:
The 'content' parameter for the API call.
Same one used in _initialize_payload
format_type:
The format of the response.
Same one used in _initialize_payload
df_kwargs:
Passed to `pandas.read_csv` to control construction of
returned DataFrame. Different defaults exist for
different content
record_type:
Database output structure type.
Used only for records content
"""
if format_type != "df":
return response
if not df_kwargs:
df_kwargs = {}
if "index_col" not in df_kwargs.keys() and record_type != "eav":
if content == "exportFieldNames":
df_kwargs["index_col"] = "original_field_name"
elif content == "metadata":
df_kwargs["index_col"] = "field_name"
elif content in ["report", "record"]:
if self.is_longitudinal:
df_kwargs["index_col"] = [self.def_field, "redcap_event_name"]
else:
df_kwargs["index_col"] = self.def_field
response = cast(str, response)
buf = StringIO(response)
dataframe = self._read_csv(buf, **df_kwargs)
buf.close()
return dataframe
def _call_api(
self,
payload: Dict[str, Any],
return_type: Literal[
"file_map", "json", "empty_json", "count_dict", "ids_list", "str", "int"
],
file: Optional[FileUpload] = None,
) -> Union[
FileMap, Json, Dict[str, int], List[dict], List[str], int, str, Literal["1"]
]:
"""Make a POST Requst to the REDCap API
Args:
payload: Payload to send in POST request
return_type:
The data type of the return value. Used
primarily for static typing, and developer
understanding of the REDCap API
file:
File data to send with file-related API requests
"""
config = _ContentConfig(
return_empty_json=return_type == "empty_json",
return_bytes=return_type == "file_map",
)
return_headers = return_type == "file_map"
rcr = _RCRequest(url=self.url, payload=payload, config=config)
return rcr.execute(
verify_ssl=self.verify_ssl,
return_headers=return_headers,
file=file,
**self._request_kwargs,
)