def test_post_bundle_condition(): try: resp1 = requests.post(f"{php}/Bundle", json=bundle([condition_resc, condition_resc2])) assert resp1.status_code == 200 resp2 = requests.get(f"{php}/Condition?patient={patient_id}") assert resp2.status_code == 200 assert resp2.json() == bundle([condition_resc]) finally: requests.delete(f"{php}/resource")
def get_resource(resource_type, patient_id): coll = mongo_client[mongo_database][resource_type] res = coll.find({"subject.reference": f"Patient/{patient_id}"}) records = list(res) for resc in records: del resc["_id"] return bundle(records)
def test_post_observation(): try: resp1 = requests.post(f"{php}/Observation", json=observation_resc) assert resp1.status_code == 200 resp2 = requests.get(f"{php}/Observation?patient={patient_id}") assert resp2.status_code == 200 assert resp2.json() == bundle([observation_resc]) finally: requests.delete(f"{php}/resource")
def test_post_bundle_patient(): try: resp1 = requests.post(f"{php}/Bundle", json=bundle([patient_resc, patient_resc2])) assert resp1.status_code == 200 resp2 = requests.get(f"{php}/Patient/{patient_id}") assert resp2.status_code == 200 assert resp2.json() == patient_resc finally: requests.delete(f"{php}/resource")
def _get_records(ptids, fhir_plugin_id, timestamp): pt_records = [] for ptid in ptids: url_patient = f"{pds_url_base}/{fhir_plugin_id}/Patient/{ptid}" url_condition = f"{pds_url_base}/{fhir_plugin_id}/Condition?patient={ptid}" url_observation = f"{pds_url_base}/{fhir_plugin_id}/Observation?patient={ptid}" val = get(url_patient).bind(lambda patient: get(url_condition).bind( lambda condition: get(url_observation). bind(lambda observation: unbundle(condition).bind( lambda condition_unbundled: unbundle(observation).map( lambda observation_unbundled: bundle([ patient, *condition_unbundled, *observation_unbundled ])))))) pt_records.append(val) return list_traversable_either_applicative.sequence(pt_records)
def handle_requests(requests): rescs = [] for request in requests: method = request["method"] url = request["url"] result = urlsplit(url) pcs = result.path.split("/") qcs = map(lambda x: x.split("="), result.query.split("&")) if pcs[1] == "Patient": rescs.append(_get_patient(pcs[2])) else: patient_id = None for qc in qcs: if qc[0] == "patient": patient_id = qc[1] rescs.append(_get_resource(pcs[1], patient_id)) return Right(bundle(rescs, "batch-response"))
def post_resources(resc_types, patient_ids): patients = [] for patient_id in patient_ids: requests = [] for resc_type in resc_types: if resc_type == "Patient": requests.append({ "url": f"/Patient/{patient_id}", "method": "GET" }) else: requests.append({ "url": f"/{resc_type}?patient={patient_id}", "method": "GET" }) batch = bundle(requests, "batch") patient = _post_batch(batch).value patients.append(patient) return patients
def get_patient_resource_entry_array(json_in_dir, pid: str, resource_name): resource_path = os.path.join(os.path.join(json_in_dir, resource_name)) rescs_filtered = [] if os.path.isdir(resource_path): for root, _, files in os.walk(resource_path): for f in files: pid_fn = os.path.join(root, f) logger.info(f"looking into {pid_fn}") with open(pid_fn, encoding='latin-1') as pid_fp: rescs = unbundle(json.load(pid_fp)).value logger.info(f"rescs = {rescs}") if resource_name == "Patient": rescs_filtered.extend( filter(lambda x: x["id"] == pid, rescs)) else: patient_reference = f"Patient/{pid}" rescs_filtered.extend( filter( lambda x: x["subject"]["reference"] == patient_reference, rescs)) return bundle(rescs_filtered)
def handle_path(path): logger = getLogger(f"{__name__}{os.getpid()}", logging.INFO) logger.info(f"loading {path}") if not dry_run: try: with open(path) as input_stream: obj = json.load(input_stream) except: with open(path, encoding="latin-1") as input_stream: obj = json.load(input_stream) rescs = unbundle(obj).value nrescs = len(rescs) logger.info(f"{nrescs} resources loaded") maxlen = 1024 for i in range(0, nrescs, maxlen): subrescs = rescs[i:min(i + maxlen, nrescs)] subobj = bundle(subrescs) logger.info(f"ingesting {path} {i}") requests.post(f"{base_url}/Bundle", json=subobj) else: logger.info(f"post {base_url}/Bundle")
def _get_records(ptid, fhir_plugin_id, timestamp): url_patient = f"{pds_url_base}/{fhir_plugin_id}/Patient/{ptid}" url_condition = f"{pds_url_base}/{fhir_plugin_id}/Condition?patient={ptid}" url_observation = f"{pds_url_base}/{fhir_plugin_id}/Observation?patient={ptid}" return get(url_patient).bind(lambda patient: get(url_condition).bind(lambda condition: get(url_observation).bind(lambda observation: unbundle(condition).bind(lambda condition_unbundled: unbundle(observation).map(lambda observation_unbundled: bundle([ patient, *condition_unbundled, *observation_unbundled ]))))))