def create_patient(project_id, location, dataset_id, fhir_store_id):
"""Creates a new Patient resource in a FHIR store.
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
url = "{}/projects/{}/locations/{}".format(base_url, project_id, location)
fhir_store_path = "{}/datasets/{}/fhirStores/{}/fhir/Patient".format(
url, dataset_id, fhir_store_id
)
# Sets required application/fhir+json header on the request
headers = {"Content-Type": "application/fhir+json;charset=utf-8"}
body = {
"name": [{"use": "official", "family": "Smith", "given": ["Darcy"]}],
"gender": "female",
"birthDate": "1970-01-01",
"resourceType": "Patient",
}
response = session.post(fhir_store_path, headers=headers, json=body)
response.raise_for_status()
resource = response.json()
print("Created Patient resource with ID {}".format(resource["id"]))
return response