-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredcap_api_import_record.py
More file actions
91 lines (76 loc) · 3.7 KB
/
redcap_api_import_record.py
File metadata and controls
91 lines (76 loc) · 3.7 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
import requests
from configparser import ConfigParser
from typing import Dict, List
# Read config file
config = ConfigParser()
config.read("config.cfg")
# Get config values
redcap_api_url: str = config.get("redcap", "api_url")
redcap_project_token: str = config.get("redcap", "project_token")
# Try different JSON strings:
# Because this REDCap project is longitudinal, the primary key is composite, consisting of
# "ptid" and "redcap_event_name".
# Both are required in the JSON string (sorta... see `json_data_bad3` below).
# -- Good JSON
json_data_good: str = '[{"ptid": "1", "redcap_event_name": "visit_1_arm_1", "maristat": 1},' \
'{"ptid": "2", "redcap_event_name": "visit_1_arm_1", "maristat": "2"}]' # accepts int or str
# >> HTTP Status: 200
# >> JSON Result: ['1', '2']
# >> Note: The result ['1', '2'] is an array of IDs whose records were affected by successful data import.
# -- Bad JSON: Event not defined
json_data_bad1: str = '[{"ptid": "1", "redcap_event_name": "visit_11_arm_1", "maristat": 1},' \
'{"ptid": "2", "redcap_event_name": "visit_12_arm_1", "maristat": 2}]'
# >> HTTP Status: 400
# >> JSON Result: {'error': 'The following values of redcap_event_name are invalid: visit_11_arm_1, visit_12_arm_1'}
# >> Note: Plain English. Doesn't get much better than that.
# -- Bad JSON: No field provided
json_data_bad2: str = '[{"ptid": "1", "redcap_event_name": "visit_1_arm_1"},' \
'{"ptid": "2", "redcap_event_name": "visit_1_arm_1"}]'
# >> HTTP Status: 200
# >> JSON Result: ['1', '2']
# >> Note: There's no error, but no data is imported.
# -- Bad JSON: No `redcap_event_name` field/values provided
json_data_bad3: str = '[{"ptid": "1", "maristat": "4"},' \
'{"ptid": "2", "maristat": "4"}]'
# >> HTTP Status: 200
# >> JSON Result: ['1', '2']
# >> Note: There's no error b/c apparently REDCap defaults to the first longitudinal event,
# i.e., "redcap_event_name": "visit_1_arm_1".
# -- Bad JSON: Nonexistent field/variable
json_data_bad4: str = '[{"ptid": "1", "redcap_event_name": "visit_1_arm_1", "foo": "bar"},' \
'{"ptid": "2", "redcap_event_name": "visit_1_arm_1", "baz": "qux"}]'
# >> HTTP Status: 400
# >> JSON Result: {'error': 'The following fields were not found in the project as real data fields: foo, baz'}
# Note: Plain English.
# -- Bad JSON: Invalid values for a given field
json_data_bad5: str = '[{"ptid": "1", "redcap_event_name": "visit_1_arm_1", "maristat": "01234"},' \
'{"ptid": "2", "redcap_event_name": "visit_1_arm_1", "maristat": "56789"}]'
# >> HTTP Status: 400
# >> JSON Result: {'error': '"1","maristat","01234","The value is not a valid category for maristat"\n
# "2","maristat","56789","The value is not a valid category for maristat"'}
# Note: Plain English.
request_dict: Dict[str, str] = {
'token': redcap_project_token,
'content': "record",
'format': "json", # "xml" (default), "csv", "json", "odm"
'type': "flat", # "flat" (default), "eav"
'forceAutoNumber': "false", # "false" (default), "true"
'overwriteBehavior': "normal", # "normal" (default), "overwrite"
'data': "",
'returnContent': "ids", # "count" (default), "ids", "auto_ids"
'returnFormat': "json" # "json" (default), "csv", "xml"
}
json_data_list: List[str] = [
json_data_good,
json_data_bad1,
json_data_bad2,
json_data_bad3,
json_data_bad4,
json_data_bad5,
]
for json_data in json_data_list:
request_dict['data'] = json_data
r = requests.post(redcap_api_url, data=request_dict, verify=False)
print(f"JSON String: {json_data}")
print(f"HTTP Status: {str(r.status_code)}")
print(f"JSON Result: {r.json()}\n")