-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr4.py
42 lines (33 loc) · 1.2 KB
/
r4.py
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
import logging
import json
from redcap import Project, RedcapError
from requests import RequestException
logger = logging.getLogger(__name__)
class R4(Project):
# REDCap variable names
FIELD_RECORD_ID = 'record_id' # Record ID in R4
FIELD_METREE_JSON_FILE = 'metree_import_json_file'
def __init__(self, endpoint, api_token):
self.endpoint = endpoint
self.api_token = api_token
# PyCap expects endpoint to end with '/'
if self.endpoint[-1] != '/':
self.endpoint += '/'
super().__init__(self.endpoint, self.api_token)
def get_metree_json(self, record_id):
""" Get MeTree JSON data file from R4
Params
------
record_id: (str) record ID
Returns
-------
JSON data object if MeTree is available. Otherwise, None
"""
try:
file_response = self.export_file(record=record_id, field=R4.FIELD_METREE_JSON_FILE)
except RedcapError:
# No MeTree JSON file for this participant
logger.debug('No MeTree JSON file for this participant')
return None
# Convert response to JSON object
return json.loads(file_response[0])