-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulk-import-ci.py
249 lines (201 loc) · 9.14 KB
/
bulk-import-ci.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
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
import json
import os
import requests
import subprocess
import sys
import platform
formsServerEndpoint = "https://smartforms.csiro.au/api/fhir"
implementationGuideOutputDirectory = "output"
# Define assembly instructions and
assemblyInstructionsReference = "Questionnaire/AssemblyInstructions"
assembledQuestionnaireReference = (
"Questionnaire/AboriginalTorresStraitIslanderHealthCheck"
)
if platform.system() == "Windows":
# ANSI doesn't work on Windows
HEADER = ''
ERROR_RED = ''
WARNING_YELLOW = ''
OK_GREEN = ''
INFO_BLUE = ''
END_C = ''
else:
# Define colors for console output
HEADER = '\033[95m'
ERROR_RED = '\033[91m'
WARNING_YELLOW = '\033[93m'
OK_GREEN = '\033[92m'
INFO_BLUE = '\033[94m'
END_C = '\033[0m'
HEADERS = {"Content-Type": "application/json"}
# Print out more information if an error occurred
def printErrorAdditionalDetails(e):
print(f"{ERROR_RED}*** Additional error details: ***{END_C}")
if e.response is not None:
print(f"{ERROR_RED}Status Code: {e.response.status_code}{END_C}")
print(f"{ERROR_RED}Reason: {e.response.reason}{END_C}")
print(f"{ERROR_RED}Response Text: {e.response.text}{END_C}")
else:
print(f"{ERROR_RED}No additional error information available.{END_C}")
print()
# Get questionnaire resources from a defined output directory
def getQuestionnairesFromLocalIg():
questionnaires = {}
implementationGuide = None
for file in os.listdir(implementationGuideOutputDirectory):
if file.startswith("Questionnaire") and file.endswith(".json"):
file_path = os.path.join(implementationGuideOutputDirectory, file)
# Open and process the JSON file
with open(file_path, "r", encoding="utf-8") as json_file:
resource = json.load(json_file)
# Construct reference name
reference = file.replace(
"Questionnaire-", "Questionnaire/"
).replace(".json", "")
# Store reference and resource in dict
questionnaires[reference] = resource
if file.startswith("ImplementationGuide-") and file.endswith(".json"):
file_path = os.path.join(implementationGuideOutputDirectory, file)
# Open and process the JSON file
with open(file_path, "r", encoding="utf-8") as json_file:
implementationGuide = json.load(json_file)
return questionnaires, implementationGuide
# update server's root and subquestionnaire with PUT requests
def updateRootAndSubquestionnaires(questionnaires, implementationGuide):
# Iterate all resources in implementation guide
for resource in implementationGuide["definition"]["resource"]:
reference = resource["reference"]["reference"]
if reference.startswith("Questionnaire"):
# skip assembled questionnaire's reference because it doesn't exist yet
if reference == assembledQuestionnaireReference:
continue
# use resources' reference from IG as key to access earlier stored questionaires
questionnaireToBeUpdated = questionnaires[reference]
try:
response = requests.put(
f"{formsServerEndpoint}/{reference}",
json=questionnaireToBeUpdated,
headers=HEADERS,
)
response.raise_for_status()
# Check the response status code
if str(response.status_code).startswith("2"):
print(
f"{OK_GREEN}PUT request for {reference} successful at {formsServerEndpoint}: {response.status_code} OK{END_C}"
)
else:
print(
f"{ERROR_RED}ERROR: PUT request for {reference} failed at {formsServerEndpoint}: {response.status_code} ERR {END_C}"
)
except KeyError as e:
print(
f"{ERROR_RED}ERROR: Fail to find {reference} in local files, questionnaire not updated in server. {END_C}"
)
printErrorAdditionalDetails(e)
except requests.exceptions.HTTPError as e:
print(f"{ERROR_RED}ERROR: HTTP ERROR THROWN:{END_C}", e, '\n')
printErrorAdditionalDetails(e)
except Exception as e:
print(f"{ERROR_RED}ERROR: An error occurred, details:{END_C}", e, '\n')
printErrorAdditionalDetails(e)
def assembleQuestionnaire(questionnaires):
rootQuestionnaire = questionnaires[assemblyInstructionsReference]
assembleInputParams = {
"resourceType": "Parameters",
"parameter": [{"name": "questionnaire", "resource": rootQuestionnaire}],
}
try:
response = requests.post(
f"{formsServerEndpoint}/Questionnaire/$assemble", json=assembleInputParams
)
response.raise_for_status()
# Check the response status code
if str(response.status_code).startswith("2"):
print(
f"{OK_GREEN}POST request Questionnaire/$assemble successful at {formsServerEndpoint}: {response.status_code} OK{END_C}"
)
assembleOutputParams = response.json()
# Return bare questionnaire
if assembleOutputParams["resourceType"] == "Questionnaire":
return assembleOutputParams
# Output params is not a bare questionnaire
try:
assembledQuestionnaire = assembleOutputParams["parameter"][0][
"resource"
]
return assembledQuestionnaire
except Exception as e:
print(
f"{ERROR_RED}ERROR: Unable to retrieve assembled questionnaire from output parameters{END_C}",
e, '\n'
)
printErrorAdditionalDetails(e)
else:
print(
f"{ERROR_RED}ERROR: $assemble failed with status code at {formsServerEndpoint}: {response.status_code} ERR {END_C}"
)
except requests.exceptions.HTTPError as e:
print(f"{ERROR_RED}ERROR: HTTP ERROR THROWN:{END_C}",e, '\n')
printErrorAdditionalDetails(e)
except Exception as e:
print(f"{ERROR_RED}ERROR: An error occurred, details:", e, '\n')
printErrorAdditionalDetails(e)
return None
# update assembled questionnaire on server
def updateAssembledQuestionnaire(questionnaire):
headers = {"Content-Type": "application/json"}
questionnaire["id"] = assembledQuestionnaireReference.replace("Questionnaire/", "")
try:
response = requests.put(
f"{formsServerEndpoint}/{assembledQuestionnaireReference}",
json=questionnaire,
headers=HEADERS,
)
response.raise_for_status()
# Check the response status code
if str(response.status_code).startswith("2"):
print(
f"{OK_GREEN}PUT request for {assembledQuestionnaireReference} successful at {formsServerEndpoint}: {response.status_code} OK{END_C}"
)
else:
print(
f"{ERROR_RED}ERROR: PUT request for {assembledQuestionnaireReference} failed at {formsServerEndpoint}: {response.status_code} ERR{END_C}"
)
except requests.exceptions.HTTPError as e:
print(f"{ERROR_RED}ERROR: HTTP ERROR THROWN:{END_C}",e, '\n')
printErrorAdditionalDetails(e)
except Exception as e:
print(f"{ERROR_RED}ERROR: An error occurred, details:", e, '\n')
printErrorAdditionalDetails(e)
# Main function
def main():
if len(sys.argv) != 2 or (sys.argv[1] != "skip" and sys.argv[1] != "build"):
print(f"Run {INFO_BLUE}python bulk-import-ci.py build{END_C} to run _genonce.sh before running the bulk import.")
print(f"Run {INFO_BLUE}python bulk-import-ci.py skip{END_C} to run the bulk import only.")
return
if (sys.argv[1] == "skip"):
print(f"{HEADER}Skipping IG build...{END_C}")
else:
print(f"{HEADER}Running _genonce.sh...{END_C}")
subprocess.run(["./_genonce.sh"], shell=True)
print(f"New IG built locally in /output.\n")
# get questionnaires from directory
print(f"{HEADER}Reading questionnaires from /output...{END_C}")
questionnaires, implementationGuide = getQuestionnairesFromLocalIg()
if len(questionnaires) == 0:
print(f"{WARNING_YELLOW}WARNING: No questionnaires found, exiting.{END_C}")
return
# update server's root and subquestionnaire with PUT requests
print(f"{HEADER}Uploading subquestionnaires...{END_C}")
updateRootAndSubquestionnaires(questionnaires, implementationGuide)
# assemble questionnaire
print(f"{HEADER}Asssembling subquestionnaires...{END_C}")
assembledQuestionnaire = assembleQuestionnaire(questionnaires)
# update assembled questionnaire
print(f"{HEADER}Uploading assembled questionnaire...{END_C}")
if assembledQuestionnaire != None:
updateAssembledQuestionnaire(assembledQuestionnaire)
print(f"{HEADER}Bulk import done. Exiting.{END_C}")
# Entry point of script
if __name__ == "__main__":
main()