Created
August 8, 2022 04:23
-
-
Save johngrimes/ed83eed1ba77af4bf37b82724b9c6aff to your computer and use it in GitHub Desktop.
Send any number of FHIR JSON resource files to a server as updates (in parallel)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import multiprocessing as mp | |
import sys | |
from math import floor | |
from time import time | |
import requests | |
def update_resource(file): | |
with open(file) as bundle: | |
data = bundle.read() | |
resource = json.loads(data) | |
if resource.get("id") is None: | |
print(f"Resource does not have an ID: {file}") | |
return | |
resource_url = f"{url}/{resource['resourceType']}/{resource['id']}" | |
start = time() | |
print(f"Sending: {file}") | |
response = requests.put(url=resource_url, data=data, headers=headers) | |
print(f"Completed with {response.status_code}: {file} ({time() - start:.3f} s)") | |
if floor(response.status_code / 100) != 2: | |
print(response.text) | |
url = sys.argv[1] | |
files = sys.argv[2:] | |
headers = {"Content-Type": "application/fhir+json", "Accept": "application/fhir+json"} | |
if __name__ == "__main__": | |
overall_start = time() | |
with mp.Pool(mp.cpu_count()) as pool: | |
pool.map(update_resource, files) | |
print(f"Finished in {time() - overall_start:.3f} s.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment