def conditional_update_resource(
service_account_json,
base_url,
project_id,
cloud_region,
dataset_id,
fhir_store_id,
patient_id,
encounter_id,
):
"""
If a resource is found based on the search criteria specified in
the query parameters, updates the entire contents of that resource.
"""
url = "{}/projects/{}/locations/{}".format(base_url, project_id, cloud_region)
# The search query in this request updates all Observations
# using the Observation's identifier (ABC-12345 in my-code-system)
# so that their 'status' is 'cancelled'.
resource_path = "{}/datasets/{}/fhirStores/{}/fhir/Observation".format(
url, dataset_id, fhir_store_id
)
# Make an authenticated API request
session = get_session(service_account_json)
body = {
"resourceType": "Observation",
"status": "cancelled",
"subject": {"reference": "Patient/{}".format(patient_id)},
"effectiveDateTime": "2020-01-01T00:00:00+00:00",
"code": {
"coding": [
{
"system": "http://loinc.org",
"code": "8867-4",
"display": "Heart rate",
}
]
},
"valueQuantity": {"value": 55, "unit": "bpm"},
"encounter": {"reference": "Encounter/{}".format(encounter_id)},
}
headers = {"Content-Type": "application/fhir+json;charset=utf-8"}
params = {"identifier": "my-code-system|ABC-12345"}
response = session.put(resource_path, headers=headers, params=params, json=body)
response.raise_for_status()
resource = response.json()
print(
"Conditionally updated Observations with the identifier "
"'my-code-system|ABC-12345' to have a 'status' of "
"'cancelled'."
)
print(json.dumps(resource, indent=2))
return resource