-
Notifications
You must be signed in to change notification settings - Fork 22
/
upload.py
148 lines (127 loc) · 4.68 KB
/
upload.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
import json
import logging
import os
import requests
import yaml
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
# The HAPI FHIR server requires dependent objects to be loaded first
# TODO Upgrade the script so that dependencies can be resolved in a
# more automated, elegant fashion (reference detection) vs. this crude way
load_order = [
"ValueSet",
"Device",
"Organization",
"StructureDefinition",
"Medication",
"Practitioner",
"PractitionerRole",
"Patient",
"AllergyIntolerance",
"AdverseReaction",
"Alert",
"MedicationRequest",
"MedicationDispense",
"MedicationAdministration",
"ImagingReference",
"Observation",
"Order",
"OrderResponse",
"Procedure",
"Endpoint",
"ImagingStudy",
"DiagnosticReport",
"Specimen",
"Condition",
"DocumentReference",
]
if len(os.sys.argv) < 3:
raise ValueError("usage:\n\npython upload.py server_config.yml path_for_recursion")
config_file = os.sys.argv[1]
if not os.path.exists(config_file):
raise FileNotFoundError("Please specify a valid yml config file")
else:
with open(config_file, "r") as file:
server = yaml.safe_load(file)
# Test a basic GET against the FHIR server
try:
result = requests.get(
server["url"] + "Patient",
headers={"apikey": server["apikey"]},
params={"_format": server["format"]},
)
result.raise_for_status()
except requests.exceptions.RequestException as err:
log.error(err)
raise SystemExit(err)
fhir = {}
for root, dirs, files in os.walk(os.sys.argv[2]):
for file in files:
if file.endswith(".json"):
data_path = os.path.join(root, file)
try:
log.info("Loading: {}".format(data_path))
with open(data_path, "r") as data_file:
resource = json.load(data_file)
resource_type = resource.get("resourceType", None)
resource_id = resource.get("id", None)
if resource_id is None:
log.error(
"Error reading {}, make sure an ID is specified in the header comment".format(
data_path
)
)
# raise ValueError
fhir.setdefault(resource_type, []).append(resource)
except Exception as e:
log.error(f"Failed to load FHIR resource at {data_path} due to {e}")
# Make sure we have load_order for all of our resource types
missing_keys = set(fhir.keys()) - set(load_order)
if missing_keys:
log.error("Resource Types missing from load order: {}".format(missing_keys))
raise ValueError
for resource_type in load_order:
if resource_type in fhir:
for resource in fhir[resource_type]:
resource_id = resource["id"]
if resource_id is None:
log.error(
"Error reading {}, make sure an ID is specified in the header comment".format(
resource
)
)
raise ValueError
if resource_id == "random":
url = server["url"] + resource_type
try:
log.info("POST - {}".format(url))
result = requests.post(
url,
json=resource,
headers={
"Content-Type": server["format"] + "+fhir",
"apikey": server["apikey"],
},
params={"_format": server["format"]},
)
result.raise_for_status()
except requests.exceptions.RequestException as e:
log.error(f"Failed to load {url} due to {e.response.text}")
else:
url = server["url"] + resource_type + "/" + resource_id
try:
log.info("PUT - {}".format(url))
result = requests.put(
url,
json=resource,
headers={
"Content-Type": server["format"] + "+fhir",
"apikey": server["apikey"],
},
params={"_format": server["format"]},
)
result.raise_for_status()
except requests.exceptions.RequestException as e:
log.error(f"Failed to load {url} due to {e.response.text}")
result_json = result.json()
# log.info("Submission Status: {}".format(result_json["issue"][0]["diagnostics"]))