def get_resource(
project_id,
location,
dataset_id,
fhir_store_id,
resource_type,
resource_id,
):
"""Gets a FHIR resource.
See https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/healthcare/api-client/v1/fhir
before running the sample."""
# Imports Python's built-in "os" module
import os
# Imports the google.auth.transport.requests transport
from google.auth.transport import requests
# Imports a module to allow authentication using a service account
from google.oauth2 import service_account
# Gets credentials from the environment.
credentials = service_account.Credentials.from_service_account_file(
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]
)
scoped_credentials = credentials.with_scopes(
["https://www.googleapis.com/auth/cloud-platform"]
)
# Creates a requests Session object with the credentials.
session = requests.AuthorizedSession(scoped_credentials)
# URL to the Cloud Healthcare API endpoint and version
base_url = "https://healthcare.googleapis.com/v1"
# TODO(developer): Uncomment these lines and replace with your values.
# project_id = 'my-project' # replace with your GCP project ID
# location = 'us-central1' # replace with the parent dataset's location
# dataset_id = 'my-dataset' # replace with the parent dataset's ID
# fhir_store_id = 'my-fhir-store' # replace with the FHIR store ID
# resource_type = 'Patient' # replace with the FHIR resource type
# resource_id = 'b682d-0e-4843-a4a9-78c9ac64' # replace with the FHIR resource's ID
url = "{}/projects/{}/locations/{}".format(base_url, project_id, location)
resource_path = "{}/datasets/{}/fhirStores/{}/fhir/{}/{}".format(
url, dataset_id, fhir_store_id, resource_type, resource_id
)
# Sets required application/fhir+json header on the request
headers = {"Content-Type": "application/fhir+json;charset=utf-8"}
response = session.get(resource_path, headers=headers)
response.raise_for_status()
resource = response.json()
print("Got {} resource:".format(resource["resourceType"]))
print(json.dumps(resource, indent=2))
return resource