def conditional_patch_resource(
service_account_json, base_url, project_id, cloud_region, dataset_id, fhir_store_id
):
"""
If a resource is found based on the search criteria specified in
the query parameters, updates part of that resource by
applying the operations specified in a JSON Patch document.
"""
url = "{}/projects/{}/locations/{}".format(base_url, project_id, cloud_region)
# The search query in this request updates all Observations
# if the subject of the Observation is a particular patient.
resource_path = "{}/datasets/{}/fhirStores/{}/fhir/Observation".format(
url, dataset_id, fhir_store_id
)
# Make an authenticated API request
session = get_session(service_account_json)
headers = {"Content-Type": "application/json-patch+json"}
body = json.dumps(
[
{
"op": "replace",
"path": "/valueQuantity/value",
# Sets the BPM for all matching Observations to 80. This
# is the portion of the request being patched.
"value": 80,
}
]
)
# The search query is passed in as a query string parameter.
params = {"identifier": "my-code-system|ABC-12345"}
response = session.patch(resource_path, headers=headers, params=params, data=body)
response.raise_for_status()
print(response.url)
resource = response.json()
print(
"Conditionally patched all Observations with the "
"identifier 'my-code-system|ABC-12345' to use a BPM of 80."
)
print(json.dumps(resource, indent=2))
return resource