|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import os |
| 4 | +import argparse |
| 5 | +from pprint import pprint |
| 6 | + |
| 7 | +from requests.auth import HTTPBasicAuth |
| 8 | + |
| 9 | +from src.config import ( |
| 10 | + SEARCH_PARAMETER_DIR, |
| 11 | + REINDEX_PAYLOAD, |
| 12 | + BASE_URL, |
| 13 | + REINDEX_ENDPOINT, |
| 14 | + FHIR_APP_ADMIN, |
| 15 | + FHIR_APP_ADMIN_PW |
| 16 | +) |
| 17 | +from src.misc import ( |
| 18 | + read_json, |
| 19 | + send_request |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +def upsert_search_parameters( |
| 24 | + client_id, client_secret, search_parameter_dir=SEARCH_PARAMETER_DIR |
| 25 | +): |
| 26 | + """ |
| 27 | + Read search parameters from file, then upsert them in server |
| 28 | + Last, reindex the resources so that SearchParameters take effect |
| 29 | + """ |
| 30 | + print("Loading search parameters") |
| 31 | + |
| 32 | + for fn in os.listdir(search_parameter_dir): |
| 33 | + if not fn.endswith(".json"): |
| 34 | + continue |
| 35 | + filepath = os.path.join(search_parameter_dir, fn) |
| 36 | + search_param = read_json(filepath) |
| 37 | + |
| 38 | + # Load search parameter |
| 39 | + id_ = search_param["id"] |
| 40 | + endpoint = "/".join( |
| 41 | + part.strip("/") for part in [BASE_URL, "SearchParameter", id_] |
| 42 | + ) |
| 43 | + resp = send_request( |
| 44 | + "put", |
| 45 | + endpoint, |
| 46 | + json=search_param, |
| 47 | + auth=HTTPBasicAuth(client_id, client_secret), |
| 48 | + ) |
| 49 | + print( |
| 50 | + f"PUT {endpoint}" |
| 51 | + ) |
| 52 | + |
| 53 | + # Start reindexing operation |
| 54 | + endpoint = f"{BASE_URL}/$reindex" |
| 55 | + resp = send_request( |
| 56 | + "post", |
| 57 | + endpoint, |
| 58 | + json=REINDEX_PAYLOAD, |
| 59 | + auth=HTTPBasicAuth(client_id, client_secret), |
| 60 | + ) |
| 61 | + pprint(resp.json()) |
| 62 | + |
| 63 | + |
| 64 | +def cli(): |
| 65 | + """ |
| 66 | + CLI for running this script |
| 67 | + """ |
| 68 | + parser = argparse.ArgumentParser( |
| 69 | + description='Load SearchParameters in FHIR server' |
| 70 | + ) |
| 71 | + parser.add_argument( |
| 72 | + "--client_id", |
| 73 | + default=FHIR_APP_ADMIN, |
| 74 | + help="Admin ID to authenticate with FHIR server", |
| 75 | + ) |
| 76 | + parser.add_argument( |
| 77 | + "--client_secret", |
| 78 | + default=FHIR_APP_ADMIN_PW, |
| 79 | + help="Admin secret to authenticate with FHIR server", |
| 80 | + ) |
| 81 | + parser.add_argument( |
| 82 | + "--search_parameter_dir", |
| 83 | + default=SEARCH_PARAMETER_DIR, |
| 84 | + help="Path to dir with SearchParameters", |
| 85 | + ) |
| 86 | + args = parser.parse_args() |
| 87 | + |
| 88 | + upsert_search_parameters( |
| 89 | + args.client_id, args.client_secret, args.search_parameter_dir |
| 90 | + ) |
| 91 | + |
| 92 | + print("✅ Load SearchParameters complete") |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + cli() |
0 commit comments