forked from stormliucong/eMERGE-Columbia-Data-Sync-Service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_setup.py
93 lines (81 loc) · 2.94 KB
/
project_setup.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
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
import requests
import json
api_token_file = './api_tokens.json'
with open(api_token_file,'r') as f:
api_conf = json.load(f)
api_key_local = api_conf['api_key_local'] # API token for local
api_key_r4 = api_conf['api_key_r4'] # API token for R4.
cu_local_endpoint = api_conf['local_endpoint'] # local api endpoint
r4_api_endpoint = api_conf['r4_api_endpoint'] # R4 api endpoint
# local Data dictionary export
data = {
'token': api_key_local,
'content': 'metadata',
'format': 'json',
'returnFormat': 'json'
}
r = requests.post(cu_local_endpoint,data=data)
meta_local_json = r.json()
print('HTTP Status: ' + str(r.status_code))
# R4 Data dictionary export
data = {
'token': api_key_r4,
'content': 'metadata',
'format': 'json',
'returnFormat': 'json'
}
r = requests.post(r4_api_endpoint,data=data)
meta_r4_json = r.json()
print('HTTP Status: ' + str(r.status_code))
# remove field already existing.
meta_local_json_field_name_list = [i['field_name'] for i in meta_local_json]
meta_r4_json_deduplicated = [i for i in meta_r4_json if i['field_name'] not in meta_local_json_field_name_list]
# new_json = []
# form_name_current = ""
# for meta_local_field in meta_local_json:
# form_name_new = meta_local_field['form_name']
# if (form_name_new == form_name_current) or form_name_current == "":
# new_json.append(meta_local_field)
# form_name_current = form_name_new
# else:
# for meta_r4_field in meta_r4_json_deduplicated:
# if meta_r4_field['form_name'] == form_name_current:
# new_json.append(meta_r4_field)
# form_name_current = form_name_new
meta_json = meta_local_json + meta_r4_json_deduplicated
# redcap requires form in sequential order.
# meta_json.sort(key=lambda x: x["form_name"]) # this will change the order and re-assign the record id.
ordered_form_names = []
for i in meta_json:
if i['form_name'] not in ordered_form_names:
ordered_form_names.append(i['form_name'])
new_json = []
for i in ordered_form_names:
for j in meta_json:
if j['form_name'] == i:
new_json.append(j)
with open('./test_meta.json','w') as f:
json.dump(new_json,f)
data = {
'token': api_key_local,
'content': 'project',
'format': 'json',
'returnFormat': 'json'
}
r = requests.post(cu_local_endpoint,data=data)
print('HTTP Status: ' + str(r.status_code))
print(r.json())
if 'clone' not in r.json()['project_title']: # avoid mistakely update the project.
# update local data dictionary
# IMPORTANT: need to re-arrange the
data = {
'token': api_key_local,
'content': 'metadata',
'format': 'json',
'returnFormat': 'json',
'data': json.dumps(new_json)
}
r = requests.post(cu_local_endpoint,data=data)
print('HTTP Status: ' + str(r.status_code))
print('HTTP Status: ' + r.content.decode('utf-8'))
# HTTP Status: {"error":"This method cannot be used while the project is in Production status."}