From e938912331d7c86b9e8282af996b92f5990c4a03 Mon Sep 17 00:00:00 2001 From: noerog Date: Wed, 3 Jul 2019 14:35:04 -0400 Subject: [PATCH 1/2] Additional FHIR resource methods broken out from original file and also adding new ones --- healthcare/fhir/README.md | 37 +- healthcare/fhir/deleteFhirResource.js | 59 ++++ healthcare/fhir/deleteFhirResourcePurge.js | 59 ++++ healthcare/fhir/fhir_resources.js | 332 +----------------- healthcare/fhir/getFhirResource.js | 59 ++++ healthcare/fhir/getFhirResourceHistory.js | 61 ++++ healthcare/fhir/getFhirStoreCapabilities.js | 55 +++ healthcare/fhir/getPatientEverything.js | 60 ++++ healthcare/fhir/listFhirResourceHistory.js | 59 ++++ healthcare/fhir/resources/bundle.json | 1 + healthcare/fhir/searchFhirResourcesGet.js | 58 +++ healthcare/fhir/searchFhirResourcesPost.js | 59 ++++ .../fhir/system-test/fhir_resources.test.js | 107 +++++- healthcare/fhir/updateFhirResource.js | 63 ++++ 14 files changed, 730 insertions(+), 339 deletions(-) create mode 100644 healthcare/fhir/deleteFhirResource.js create mode 100644 healthcare/fhir/deleteFhirResourcePurge.js create mode 100644 healthcare/fhir/getFhirResource.js create mode 100644 healthcare/fhir/getFhirResourceHistory.js create mode 100644 healthcare/fhir/getFhirStoreCapabilities.js create mode 100644 healthcare/fhir/getPatientEverything.js create mode 100644 healthcare/fhir/listFhirResourceHistory.js create mode 100644 healthcare/fhir/resources/bundle.json create mode 100644 healthcare/fhir/searchFhirResourcesGet.js create mode 100644 healthcare/fhir/searchFhirResourcesPost.js create mode 100644 healthcare/fhir/updateFhirResource.js diff --git a/healthcare/fhir/README.md b/healthcare/fhir/README.md index 06a70e8df2..8b282a84d8 100644 --- a/healthcare/fhir/README.md +++ b/healthcare/fhir/README.md @@ -36,23 +36,35 @@ Run the following command to install the library dependencies for Node.js: ## FHIR resources + Commands: + deleteFhirResource.js Deletes a FHIR resource. + + deleteFhirResourcePurge.js Deletes all historical versions of a FHIR resource. + + getFhirResourceHistory.js Gets the contents of a version of a FHIR resource by version ID. + + getFhirResource.js Gets a FHIR resource. + + getFhirStoreCapabilities.js Gets the capabilities statement for a FHIR store. + + getPatientEverything.js Retrieves all resources in the patient compartment for a Patient resource. + + listFhirResourceHistory.js Lists all the versions of a resource + (including the current version and deleted versions) from the FHIR store. + searchFhirResourcesGet.js Searches for FHIR resources using GET methods. + + searchFhirResourcesPost.js Searches for FHIR resources using the POST method. + + updateFhirResource.js Updates the entire contents of a resource. + + Commands: fhir_resources.js createResource Creates a new resource in a FHIR store. - fhir_resources.js updateResource Updates an existing resource in a FHIR store. - fhir_resources.js patchResource Patches an existing resource in a FHIR store. - fhir_resources.js deleteResource Deletes a FHIR resource or returns NOT_FOUND if it - doesn't exist. - fhir_resources.js getResource Gets a FHIR resource. - - fhir_resources.js searchResourcesGet Searches resources in the given FHIR store using the - searchResources GET method. - fhir_resources.js searchResourcesPost Searches resources in the given FHIR store using the - _search POST method. - fhir_resources.js getPatientEverything Gets all the resources in the patient compartment. - + fhir_resources.js executeBundle Executes all the requests in the given Bundle. + Options: --version Show version number [boolean] @@ -64,3 +76,4 @@ Run the following command to install the library dependencies for Node.js: --help Show help [boolean] For more information, see https://cloud.google.com/healthcare/docs + diff --git a/healthcare/fhir/deleteFhirResource.js b/healthcare/fhir/deleteFhirResource.js new file mode 100644 index 0000000000..7fdc223960 --- /dev/null +++ b/healthcare/fhir/deleteFhirResource.js @@ -0,0 +1,59 @@ +/** + * Copyright 2019, Google, LLC + * Licensed under the Apache License, Version 2.0 (the `License`); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an `AS IS` BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* eslint-disable no-warning-comments */ + +'use strict'; + +function main( + projectId = process.env.GCLOUD_PROJECT, + cloudRegion = 'us-central1', + datasetId, + fhirStoreId, + resourceType, + resourceId +) { + // [START healthcare_delete_fhir_resource] + const {google} = require('googleapis'); + const healthcare = google.healthcare('v1beta1'); + + async function deleteFhirResource() { + const auth = await google.auth.getClient({ + scopes: ['https://www.googleapis.com/auth/cloud-platform'], + }); + google.options({auth}); + + // TODO(developer): uncomment these lines before running the sample + // const cloudRegion = 'us-central1'; + // const projectId = 'adjective-noun-123'; + // const datasetId = 'my-dataset'; + // const fhirStoreId = 'my-fhir-store'; + // const resourceType = 'Patient'; + // const resourceId = '9a664e07-79a4-4c2e-04ed-e996c75484e1; + const name = `projects/${projectId}/locations/${cloudRegion}/datasets/${datasetId}/fhirStores/${fhirStoreId}/fhir/${resourceType}/${resourceId}`; + const request = {name}; + + await healthcare.projects.locations.datasets.fhirStores.fhir.delete( + request + ); + console.log(`Deleted FHIR resource ${resourceType}`); + } + + deleteFhirResource(); + // [END healthcare_delete_fhir_resource] +} + +// node deleteFhirResource.js +main(...process.argv.slice(2)); diff --git a/healthcare/fhir/deleteFhirResourcePurge.js b/healthcare/fhir/deleteFhirResourcePurge.js new file mode 100644 index 0000000000..7d5c6bbeb9 --- /dev/null +++ b/healthcare/fhir/deleteFhirResourcePurge.js @@ -0,0 +1,59 @@ +/** + * Copyright 2019, Google, LLC + * Licensed under the Apache License, Version 2.0 (the `License`); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an `AS IS` BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* eslint-disable no-warning-comments */ + +'use strict'; + +function main( + projectId = process.env.GCLOUD_PROJECT, + cloudRegion = 'us-central1', + datasetId, + fhirStoreId, + resourceType, + resourceId +) { + // [START healthcare_delete_fhir_resource] + const {google} = require('googleapis'); + const healthcare = google.healthcare('v1beta1'); + + async function deleteFhirResource() { + const auth = await google.auth.getClient({ + scopes: ['https://www.googleapis.com/auth/cloud-platform'], + }); + google.options({auth}); + + // TODO(developer): uncomment these lines before running the sample + // const cloudRegion = 'us-central1'; + // const projectId = 'adjective-noun-123'; + // const datasetId = 'my-dataset'; + // const fhirStoreId = 'my-fhir-store'; + // const resourceType = 'Patient'; + // const resourceId = '9a664e07-79a4-4c2e-04ed-e996c75484e1; + const name = `projects/${projectId}/locations/${cloudRegion}/datasets/${datasetId}/fhirStores/${fhirStoreId}/fhir/${resourceType}/${resourceId}`; + const request = {name}; + + await healthcare.projects.locations.datasets.fhirStores.fhir.ResourcePurge( + request + ); + console.log(`Deleted all historical versions of ${resourceType} resource`); + } + + deleteFhirResource(); + // [END healthcare_delete_fhir_resource] +} + +// node deleteFhirResource.js +main(...process.argv.slice(2)); diff --git a/healthcare/fhir/fhir_resources.js b/healthcare/fhir/fhir_resources.js index fee691dc7b..c343dfad5c 100644 --- a/healthcare/fhir/fhir_resources.js +++ b/healthcare/fhir/fhir_resources.js @@ -72,61 +72,15 @@ const createResource = async ( try { const resource = await request(options); - console.log(`Created resource ${resourceType} with ID ${resource.id}.`); + console.log( + `Created FHIR resource ${resourceType} with ID ${resource.id}.` + ); } catch (err) { console.error(err); } }; // [END healthcare_create_fhir_resource] -// [START healthcare_update_fhir_resource] -const updateResource = async ( - token, - projectId, - cloudRegion, - datasetId, - fhirStoreId, - resourceType, - resourceId -) => { - // Token retrieved in callback - // getToken(serviceAccountJson, function(cb) {...}); - // const cloudRegion = 'us-central1'; - // const projectId = 'adjective-noun-123'; - // const datasetId = 'my-dataset'; - // const fhirStoreId = 'my-fhir-store'; - // const resourceType = 'Patient'; - // const resourceId = 'd64a85ae-da1b-4a10-0eb8-cfaf55bdbe3f'; - const parentName = `${BASE_URL}/projects/${projectId}/locations/${cloudRegion}`; - - const resourcePath = `${parentName}/datasets/${datasetId}/fhirStores/${fhirStoreId}/fhir/${resourceType}/${resourceId}`; - - const patientData = { - resourceType: resourceType, - id: resourceId, - active: true, - }; - - const options = { - url: resourcePath, - headers: { - Authorization: `Bearer ${token}`, - 'Content-Type': 'application/fhir+json; charset=utf-8', - }, - body: patientData, - json: true, - method: 'PUT', - }; - - try { - await request(options); - console.log(`Updated ${resourceType} with ID ${resourceId}`); - } catch (err) { - console.error(err); - } -}; -// [END healthcare_update_fhir_resource] - // [START healthcare_patch_fhir_resource] const patchResource = async ( token, @@ -149,7 +103,7 @@ const patchResource = async ( const resourcePath = `${parentName}/datasets/${datasetId}/fhirStores/${fhirStoreId}/fhir/${resourceType}/${resourceId}`; - const patchOperations = [{op: 'replace', path: '/active', value: false}]; + const patchOperations = [{op: 'replace', path: '/active', value: true}]; const options = { url: resourcePath, @@ -164,153 +118,35 @@ const patchResource = async ( try { await request(options); - console.log(`Patched ${resourceType} with ID ${resourceId}`); + console.log(`Patched ${resourceType} resource`); } catch (err) { console.error(err); } }; // [END healthcare_patch_fhir_resource] -// [START healthcare_delete_fhir_resource] -const deleteResource = async ( - token, - projectId, - cloudRegion, - datasetId, - fhirStoreId, - resourceType, - resourceId -) => { - // Token retrieved in callback - // getToken(serviceAccountJson, function(cb) {...}); - // const cloudRegion = 'us-central1'; - // const projectId = 'adjective-noun-123'; - // const datasetId = 'my-dataset'; - // const fhirStoreId = 'my-fhir-store'; - // const resourceType = 'Patient'; - // const resourceId = 'd64a85ae-da1b-4a10-0eb8-cfaf55bdbe3f'; - const parentName = `${BASE_URL}/projects/${projectId}/locations/${cloudRegion}`; - - const resourcePath = `${parentName}/datasets/${datasetId}/fhirStores/${fhirStoreId}/fhir/${resourceType}/${resourceId}`; - - const options = { - url: resourcePath, - headers: { - Authorization: `Bearer ${token}`, - 'Content-Type': 'application/fhir+json; charset=utf-8', - }, - json: true, - method: 'DELETE', - }; - - try { - await request(options); - console.log(`Deleted ${resourceType} with ID ${resourceId}.`); - } catch (err) { - console.error(err); - } -}; -// [END healthcare_delete_fhir_resource] - -// [START healthcare_get_fhir_resource] -const getResource = async ( +// [START healthcare_fhir_execute_bundle] +const executeBundle = async ( token, projectId, cloudRegion, datasetId, fhirStoreId, - resourceType, - resourceId + bundleFile ) => { + const fs = require('fs'); // Token retrieved in callback // getToken(serviceAccountJson, function(cb) {...}); // const cloudRegion = 'us-central1'; // const projectId = 'adjective-noun-123'; // const datasetId = 'my-dataset'; // const fhirStoreId = 'my-fhir-store'; - // const resourceType = 'Patient'; - // const resourceId = 'd64a85ae-da1b-4a10-0eb8-cfaf55bdbe3f'; + // const bundleFile = 'bundle.json'; const parentName = `${BASE_URL}/projects/${projectId}/locations/${cloudRegion}`; - const resourcePath = `${parentName}/datasets/${datasetId}/fhirStores/${fhirStoreId}/fhir/${resourceType}/${resourceId}`; - - const options = { - url: resourcePath, - headers: { - Authorization: `Bearer ${token}`, - 'Content-Type': 'application/fhir+json; charset=utf-8', - }, - json: true, - }; - - try { - const results = await request(options); - console.log( - `Got ${resourceType} resource:\n${JSON.stringify(results, null, 2)}` - ); - } catch (err) { - console.error(err); - } -}; -// [END healthcare_get_fhir_resource] - -// [START healthcare_search_fhir_resources_get] -const searchResourcesGet = async ( - token, - projectId, - cloudRegion, - datasetId, - fhirStoreId, - resourceType -) => { - // Token retrieved in callback - // getToken(serviceAccountJson, function(cb) {...}); - // const cloudRegion = 'us-central1'; - // const projectId = 'adjective-noun-123'; - // const datasetId = 'my-dataset'; - // const fhirStoreId = 'my-fhir-store'; - // const resourceType = 'Patient'; - const parentName = `${BASE_URL}/projects/${projectId}/locations/${cloudRegion}`; - - const resourcesPath = `${parentName}/datasets/${datasetId}/fhirStores/${fhirStoreId}/fhir/${resourceType}`; - - const options = { - url: resourcesPath, - headers: { - Authorization: `Bearer ${token}`, - 'Content-Type': 'application/fhir+json; charset=utf-8', - }, - json: true, - }; - - try { - const results = await request(options); - console.log(JSON.stringify(results, null, 2)); - } catch (err) { - console.error(err); - } -}; -// [END healthcare_search_fhir_resources_get] - -// [START healthcare_search_fhir_resources_post] -const searchResourcesPost = async ( - token, - projectId, - cloudRegion, - datasetId, - fhirStoreId, - resourceType -) => { - // Token retrieved in callback - // getToken(serviceAccountJson, function(cb) {...}); - // const cloudRegion = 'us-central1'; - // const projectId = 'adjective-noun-123'; - // const datasetId = 'my-dataset'; - // const fhirStoreId = 'my-fhir-store'; - // const resourceType = 'Patient'; - const parentName = `${BASE_URL}/projects/${projectId}/locations/${cloudRegion}`; + const resourcesPath = `${parentName}/datasets/${datasetId}/fhirStores/${fhirStoreId}/fhir`; - const resourcesPath = `${parentName}/datasets/${datasetId}/fhirStores/${fhirStoreId}/fhir/${resourceType}/_search`; + const bundle = fs.readFileSync(bundleFile); const options = { url: resourcesPath, @@ -318,56 +154,19 @@ const searchResourcesPost = async ( Authorization: `Bearer ${token}`, 'Content-Type': 'application/fhir+json; charset=utf-8', }, - json: true, + body: bundle, method: 'POST', }; try { const results = await request(options); + console.log('Executed Bundle\n'); console.log(JSON.stringify(results, null, 2)); } catch (err) { console.error(err); } }; -// [END healthcare_search_fhir_resources_post] - -// [START healthcare_fhir_get_patient_everything] -const getPatientEverything = async ( - token, - projectId, - cloudRegion, - datasetId, - fhirStoreId, - resourceId -) => { - // Token retrieved in callback - // getToken(serviceAccountJson, function(cb) {...}); - // const cloudRegion = 'us-central1'; - // const projectId = 'adjective-noun-123'; - // const datasetId = 'my-dataset'; - // const fhirStoreId = 'my-fhir-store'; - // const resourceId = 'd64a85ae-da1b-4a10-0eb8-cfaf55bdbe3f'; - const parentName = `${BASE_URL}/projects/${projectId}/locations/${cloudRegion}`; - - const fhirStorePath = `${parentName}/datasets/${datasetId}/fhirStores/${fhirStoreId}/fhir/Patient/${resourceId}/$everything`; - - const options = { - url: fhirStorePath, - headers: { - authorization: `Bearer ${token}`, - }, - json: true, - }; - - try { - const results = await request(options); - console.log(`Got all resources in patient ${resourceId} compartment:`); - console.log(results); - } catch (err) { - console.error(err); - } -}; -// [END healthcare_fhir_get_patient_everything] +// [END healthcare_fhir_execute_bundle] require(`yargs`) // eslint-disable-line .demand(1) @@ -412,25 +211,6 @@ require(`yargs`) // eslint-disable-line getToken(opts.serviceAccount, cb); } ) - .command( - `updateResource `, - `Updates an existing resource in a FHIR store.`, - {}, - opts => { - const cb = token => { - updateResource( - token, - opts.projectId, - opts.cloudRegion, - opts.datasetId, - opts.fhirStoreId, - opts.resourceType, - opts.resourceId - ); - }; - getToken(opts.serviceAccount, cb); - } - ) .command( `patchResource `, `Patches an existing resource in a FHIR store.`, @@ -451,92 +231,18 @@ require(`yargs`) // eslint-disable-line } ) .command( - `deleteResource `, - `Deletes a FHIR resource or returns NOT_FOUND if it doesn't exist.`, + `executeBundle `, + `Executes all the requests in the given Bundle.`, {}, opts => { const cb = token => { - deleteResource( + executeBundle( token, opts.projectId, opts.cloudRegion, opts.datasetId, opts.fhirStoreId, - opts.resourceType, - opts.resourceId - ); - }; - getToken(opts.serviceAccount, cb); - } - ) - .command( - `getResource `, - `Gets a FHIR resource.`, - {}, - opts => { - const cb = token => { - getResource( - token, - opts.projectId, - opts.cloudRegion, - opts.datasetId, - opts.fhirStoreId, - opts.resourceType, - opts.resourceId - ); - }; - getToken(opts.serviceAccount, cb); - } - ) - .command( - `searchResourcesGet `, - `Searches resources in the given FHIR store using the searchResources GET method.`, - {}, - opts => { - const cb = token => { - searchResourcesGet( - token, - opts.projectId, - opts.cloudRegion, - opts.datasetId, - opts.fhirStoreId, - opts.resourceType - ); - }; - getToken(opts.serviceAccount, cb); - } - ) - .command( - `searchResourcesPost `, - `Searches resources in the given FHIR store using the _search POST method.`, - {}, - opts => { - const cb = token => { - searchResourcesPost( - token, - opts.projectId, - opts.cloudRegion, - opts.datasetId, - opts.fhirStoreId, - opts.resourceType - ); - }; - getToken(opts.serviceAccount, cb); - } - ) - .command( - `getPatientEverything `, - `Gets all the resources in the patient compartment.`, - {}, - opts => { - const cb = token => { - getPatientEverything( - token, - opts.projectId, - opts.cloudRegion, - opts.datasetId, - opts.fhirStoreId, - opts.resourceId + opts.bundleFile ); }; getToken(opts.serviceAccount, cb); diff --git a/healthcare/fhir/getFhirResource.js b/healthcare/fhir/getFhirResource.js new file mode 100644 index 0000000000..811f3edfa4 --- /dev/null +++ b/healthcare/fhir/getFhirResource.js @@ -0,0 +1,59 @@ +/** + * Copyright 2019, Google, LLC + * Licensed under the Apache License, Version 2.0 (the `License`); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an `AS IS` BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* eslint-disable no-warning-comments */ + +'use strict'; + +function main( + projectId = process.env.GCLOUD_PROJECT, + cloudRegion = 'us-central1', + datasetId, + fhirStoreId, + resourceType, + resourceId +) { + // [START healthcare_get_fhir_resource] + const {google} = require('googleapis'); + const healthcare = google.healthcare('v1beta1'); + + async function getFhirResource() { + const auth = await google.auth.getClient({ + scopes: ['https://www.googleapis.com/auth/cloud-platform'], + }); + google.options({auth}); + + // TODO(developer): uncomment these lines before running the sample + // const cloudRegion = 'us-central1'; + // const projectId = 'adjective-noun-123'; + // const datasetId = 'my-dataset'; + // const fhirStoreId = 'my-fhir-store'; + // const resourceType = 'Patient'; + // const resourceId = '16e8a860-33b3-49be-9b03-de979feed14a'; + const name = `projects/${projectId}/locations/${cloudRegion}/datasets/${datasetId}/fhirStores/${fhirStoreId}/fhir/${resourceType}/${resourceId}`; + const request = {name}; + + const resource = await healthcare.projects.locations.datasets.fhirStores.fhir.read( + request + ); + console.log(`Got ${resourceType} resource:\n`, resource.data); + } + + getFhirResource(); + // [END healthcare_get_fhir_resource] +} + +// node getFhirResource.js +main(...process.argv.slice(2)); diff --git a/healthcare/fhir/getFhirResourceHistory.js b/healthcare/fhir/getFhirResourceHistory.js new file mode 100644 index 0000000000..5dad77a976 --- /dev/null +++ b/healthcare/fhir/getFhirResourceHistory.js @@ -0,0 +1,61 @@ +/** + * Copyright 2019, Google, LLC + * Licensed under the Apache License, Version 2.0 (the `License`); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an `AS IS` BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* eslint-disable no-warning-comments */ + +'use strict'; + +function main( + projectId = process.env.GCLOUD_PROJECT, + cloudRegion = 'us-central1', + datasetId, + fhirStoreId, + resourceType, + resourceId, + versionId +) { + // [START healthcare_get_resource_history] + const {google} = require('googleapis'); + const healthcare = google.healthcare('v1beta1'); + + async function getFhirResourceHistory() { + const auth = await google.auth.getClient({ + scopes: ['https://www.googleapis.com/auth/cloud-platform'], + }); + google.options({auth}); + + // TODO(developer): uncomment these lines before running the sample + // const cloudRegion = 'us-central1'; + // const projectId = 'adjective-noun-123'; + // const datasetId = 'my-dataset'; + // const fhirStoreId = 'my-fhir-store'; + // const resourceType = 'Patient'; + // const resourceId = '16e8a860-33b3-49be-9b03-de979feed14a'; + // const versionId = 'MTU2NPg3NDgyNDAxMDc4OTAwMA'; + const name = `projects/${projectId}/locations/${cloudRegion}/datasets/${datasetId}/fhirStores/${fhirStoreId}/fhir/${resourceType}/${resourceId}/_history/${versionId}`; + const request = {name}; + + const resource = await healthcare.projects.locations.datasets.fhirStores.fhir.vread( + request + ); + console.log(JSON.stringify(resource.data, null, 2)); + } + + getFhirResourceHistory(); + // [END healthcare_get_resource_history] +} + +// node getFhirResourceHistory.js +main(...process.argv.slice(2)); diff --git a/healthcare/fhir/getFhirStoreCapabilities.js b/healthcare/fhir/getFhirStoreCapabilities.js new file mode 100644 index 0000000000..c939f8abf9 --- /dev/null +++ b/healthcare/fhir/getFhirStoreCapabilities.js @@ -0,0 +1,55 @@ +/** + * Copyright 2019, Google, LLC + * Licensed under the Apache License, Version 2.0 (the `License`); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an `AS IS` BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* eslint-disable no-warning-comments */ + +'use strict'; + +function main( + projectId = process.env.GCLOUD_PROJECT, + cloudRegion = 'us-central1', + datasetId, + fhirStoreId +) { + // [START healthcare_get_metadata] + const {google} = require('googleapis'); + const healthcare = google.healthcare('v1beta1'); + + async function getFhirStoreCapabilities() { + const auth = await google.auth.getClient({ + scopes: ['https://www.googleapis.com/auth/cloud-platform'], + }); + google.options({auth}); + + // TODO(developer): uncomment these lines before running the sample + // const cloudRegion = 'us-central1'; + // const projectId = 'adjective-noun-123'; + // const datasetId = 'my-dataset'; + // const fhirStoreId = 'my-fhir-store'; + const name = `projects/${projectId}/locations/${cloudRegion}/datasets/${datasetId}/fhirStores/${fhirStoreId}/fhir/metadata`; + const request = {name}; + + const fhirStore = await healthcare.projects.locations.datasets.fhirStores.get( + request + ); + console.log(JSON.stringify(fhirStore.data, null, 2)); + } + + getFhirStoreCapabilities(); + // [END healthcare_get_metadata] +} + +// node getFhirStoreCapabilities.js +main(...process.argv.slice(2)); diff --git a/healthcare/fhir/getPatientEverything.js b/healthcare/fhir/getPatientEverything.js new file mode 100644 index 0000000000..d71e86e690 --- /dev/null +++ b/healthcare/fhir/getPatientEverything.js @@ -0,0 +1,60 @@ +/** + * Copyright 2019, Google, LLC + * Licensed under the Apache License, Version 2.0 (the `License`); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an `AS IS` BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* eslint-disable no-warning-comments */ + +'use strict'; + +function main( + projectId = process.env.GCLOUD_PROJECT, + cloudRegion = 'us-central1', + datasetId, + fhirStoreId, + patientId +) { + // [START healthcare_get_patient_everything] + const {google} = require('googleapis'); + const healthcare = google.healthcare('v1beta1'); + + async function getPatientEverything() { + const auth = await google.auth.getClient({ + scopes: ['https://www.googleapis.com/auth/cloud-platform'], + }); + google.options({auth}); + + // TODO(developer): uncomment these lines before running the sample + // const cloudRegion = 'us-central1'; + // const projectId = 'adjective-noun-123'; + // const datasetId = 'my-dataset'; + // const fhirStoreId = 'my-fhir-store'; + // const patientId = '16e8a860-33b3-49be-9b03-de979feed14a'; + const name = `projects/${projectId}/locations/${cloudRegion}/datasets/${datasetId}/fhirStores/${fhirStoreId}/fhir/Patient/${patientId}`; + const request = {name}; + + const patientEverything = await healthcare.projects.locations.datasets.fhirStores.fhir.PatientEverything( + request + ); + console.log( + `Got all resources in patient ${patientId} compartment:\n`, + JSON.stringify(patientEverything) + ); + } + + getPatientEverything(); + // [END healthcare_get_patient_everything] +} + +// node getPatientEverything.js +main(...process.argv.slice(2)); diff --git a/healthcare/fhir/listFhirResourceHistory.js b/healthcare/fhir/listFhirResourceHistory.js new file mode 100644 index 0000000000..49d40ff03f --- /dev/null +++ b/healthcare/fhir/listFhirResourceHistory.js @@ -0,0 +1,59 @@ +/** + * Copyright 2019, Google, LLC + * Licensed under the Apache License, Version 2.0 (the `License`); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an `AS IS` BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* eslint-disable no-warning-comments */ + +'use strict'; + +function main( + projectId = process.env.GCLOUD_PROJECT, + cloudRegion = 'us-central1', + datasetId, + fhirStoreId, + resourceType, + resourceId +) { + // [START healthcare_list_resource_history] + const {google} = require('googleapis'); + const healthcare = google.healthcare('v1beta1'); + + async function listFhirResourceHistory() { + const auth = await google.auth.getClient({ + scopes: ['https://www.googleapis.com/auth/cloud-platform'], + }); + google.options({auth}); + + // TODO(developer): uncomment these lines before running the sample + // const cloudRegion = 'us-central1'; + // const projectId = 'adjective-noun-123'; + // const datasetId = 'my-dataset'; + // const fhirStoreId = 'my-fhir-store'; + // const resourceType = 'Patient'; + // const resourceId = '16e8a860-33b3-49be-9b03-de979feed14a'; + const name = `projects/${projectId}/locations/${cloudRegion}/datasets/${datasetId}/fhirStores/${fhirStoreId}/fhir/${resourceType}/${resourceId}/_history`; + const request = {name}; + + const resource = await healthcare.projects.locations.datasets.fhirStores.fhir.read( + request + ); + console.log(JSON.stringify(resource.data, null, 2)); + } + + listFhirResourceHistory(); + // [END healthcare_list_resource_history] +} + +// node listFhirResourceHistory.js +main(...process.argv.slice(2)); diff --git a/healthcare/fhir/resources/bundle.json b/healthcare/fhir/resources/bundle.json new file mode 100644 index 0000000000..f1ecb029e6 --- /dev/null +++ b/healthcare/fhir/resources/bundle.json @@ -0,0 +1 @@ +{ "resourceType": "Bundle", "type": "transaction", "entry": [ { "resource": { "resourceType": "Patient", "name": [ { "family": "Smith", "given": [ "Darcy" ] } ], "gender": "female", "address": [ { "line": [ "123 Main St." ], "city": "Anycity", "state": "CA", "postalCode": "12345" } ] }, "request": { "method": "POST" } } ] } diff --git a/healthcare/fhir/searchFhirResourcesGet.js b/healthcare/fhir/searchFhirResourcesGet.js new file mode 100644 index 0000000000..38b9c7bc31 --- /dev/null +++ b/healthcare/fhir/searchFhirResourcesGet.js @@ -0,0 +1,58 @@ +/** + * Copyright 2019, Google, LLC + * Licensed under the Apache License, Version 2.0 (the `License`); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an `AS IS` BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* eslint-disable no-warning-comments */ + +'use strict'; + +function main( + projectId = process.env.GCLOUD_PROJECT, + cloudRegion = 'us-central1', + datasetId, + fhirStoreId, + resourceType +) { + // [START healthcare_search_resources_get] + const {google} = require('googleapis'); + const healthcare = google.healthcare('v1beta1'); + + async function searchFhirResourcesGet() { + const auth = await google.auth.getClient({ + scopes: ['https://www.googleapis.com/auth/cloud-platform'], + }); + google.options({auth}); + + // TODO(developer): uncomment these lines before running the sample + // const cloudRegion = 'us-central1'; + // const projectId = 'adjective-noun-123'; + // const datasetId = 'my-dataset'; + // const fhirStoreId = 'my-fhir-store'; + const parent = `projects/${projectId}/locations/${cloudRegion}/datasets/${datasetId}/fhirStores/${fhirStoreId}/fhir`; + const request = {parent, resourceType}; + + const response = await healthcare.projects.locations.datasets.fhirStores.fhir.search( + request + ); + const resources = response.data.entry; + console.log(`Resources found: ${resources.length}`); + console.log(JSON.stringify(resources, null, 2)); + } + + searchFhirResourcesGet(); + // [END healthcare_search_resources_get] +} + +// node searchFhirResourcesGet.js +main(...process.argv.slice(2)); diff --git a/healthcare/fhir/searchFhirResourcesPost.js b/healthcare/fhir/searchFhirResourcesPost.js new file mode 100644 index 0000000000..1b755f8078 --- /dev/null +++ b/healthcare/fhir/searchFhirResourcesPost.js @@ -0,0 +1,59 @@ +/** + * Copyright 2019, Google, LLC + * Licensed under the Apache License, Version 2.0 (the `License`); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an `AS IS` BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* eslint-disable no-warning-comments */ + +'use strict'; + +function main( + projectId = process.env.GCLOUD_PROJECT, + cloudRegion = 'us-central1', + datasetId, + fhirStoreId, + resourceType +) { + // [START healthcare_search_fhir_resources_post] + const {google} = require('googleapis'); + const healthcare = google.healthcare('v1beta1'); + + async function searchFhirResourcesPost() { + const auth = await google.auth.getClient({ + scopes: ['https://www.googleapis.com/auth/cloud-platform'], + }); + google.options({auth, method: 'POST'}); + + // TODO(developer): uncomment these lines before running the sample + // const cloudRegion = 'us-central1'; + // const projectId = 'adjective-noun-123'; + // const datasetId = 'my-dataset'; + // const fhirStoreId = 'my-fhir-store'; + // const resourceType = 'Patient'; + const parent = `projects/${projectId}/locations/${cloudRegion}/datasets/${datasetId}/fhirStores/${fhirStoreId}/fhir`; + const request = {parent, resourceType}; + + const response = await healthcare.projects.locations.datasets.fhirStores.fhir.search( + request + ); + const resources = response.data.entry; + console.log(`Resources found: ${resources.length}`); + console.log(JSON.stringify(resources, null, 2)); + } + + searchFhirResourcesPost(); + // [END healthcare_search_fhir_resources_post] +} + +// node searchFhirResourcesPost.js +main(...process.argv.slice(2)); diff --git a/healthcare/fhir/system-test/fhir_resources.test.js b/healthcare/fhir/system-test/fhir_resources.test.js index 97c2b52e9c..10762ba231 100644 --- a/healthcare/fhir/system-test/fhir_resources.test.js +++ b/healthcare/fhir/system-test/fhir_resources.test.js @@ -23,7 +23,6 @@ const uuid = require('uuid'); const projectId = process.env.GCLOUD_PROJECT; const cloudRegion = 'us-central1'; -const cmd = 'node fhir_resources.js'; const cwd = path.join(__dirname, '..'); const cwdDatasets = path.join(__dirname, '../../datasets'); const datasetId = `nodejs-docs-samples-test-${uuid.v4()}`.replace(/-/gi, '_'); @@ -31,6 +30,8 @@ const fhirStoreId = `nodejs-docs-samples-test-fhir-store${uuid.v4()}`.replace( /-/gi, '_' ); + +const bundleFile = 'resources/bundle.json'; const resourceType = 'Patient'; let resourceId; @@ -43,7 +44,10 @@ before(async () => { }); after(async () => { try { - await tools.runAsync(`node deleteDataset.js ${datasetId}`, cwdDatasets); + await tools.runAsync( + `node deleteDataset.js ${projectId} ${cloudRegion} ${datasetId}`, + cwdDatasets + ); } catch (err) {} // Ignore error }); @@ -53,19 +57,19 @@ it('should create a FHIR resource', async () => { cwd ); const output = await tools.runAsync( - `${cmd} createResource ${datasetId} ${fhirStoreId} ${resourceType}`, + `node fhir_resources.js createResource ${datasetId} ${fhirStoreId} ${resourceType}`, cwd ); - const createdMessage = new RegExp( - `Created resource ${resourceType} with ID (.*).` + const createdResource = new RegExp( + `Created FHIR resource ${resourceType} with ID (.*).` ); - assert.strictEqual(createdMessage.test(output), true); - [, resourceId] = createdMessage.exec(output); + assert.strictEqual(createdResource.test(output), true); + [, resourceId] = createdResource.exec(output); }); it('should get a FHIR resource', async () => { const output = await tools.runAsync( - `${cmd} getResource ${datasetId} ${fhirStoreId} ${resourceType} ${resourceId}`, + `node getFhirResource.js ${projectId} ${cloudRegion} ${datasetId} ${fhirStoreId} ${resourceType} ${resourceId}`, cwd ); assert.strictEqual( @@ -74,28 +78,103 @@ it('should get a FHIR resource', async () => { ); }); +it('should list and get a FHIR resource history', async () => { + let output = await tools.runAsync( + `node listFhirResourceHistory.js ${projectId} ${cloudRegion} ${datasetId} ${fhirStoreId} ${resourceType} ${resourceId}`, + cwd + ); + assert.ok(output.includes('versionId')); + + // Get the version ID here from the response because it's generated by the server + const formatted = JSON.parse(output); + const { + entry: [ + { + resource: { + meta: {versionId}, + }, + }, + ], + } = formatted; + + output = await tools.runAsync( + `node getFhirResourceHistory.js ${projectId} ${cloudRegion} ${datasetId} ${fhirStoreId} ${resourceType} ${resourceId} ${versionId}`, + cwd + ); + assert.ok(output.includes(versionId)); +}); + +it('should get everything in Patient compartment', async () => { + const output = await tools.runAsync( + `node getPatientEverything.js ${projectId} ${cloudRegion} ${datasetId} ${fhirStoreId} ${resourceId}`, + cwd + ); + assert.strictEqual( + new RegExp(`Got all resources in patient ${resourceId} compartment`).test( + output + ), + true + ); +}); + it('should update a FHIR resource', async () => { const output = await tools.runAsync( - `${cmd} updateResource ${datasetId} ${fhirStoreId} ${resourceType} ${resourceId}`, + `node updateFhirResource.js ${projectId} ${cloudRegion} ${datasetId} ${fhirStoreId} ${resourceType} ${resourceId}`, cwd ); - assert.strictEqual(output, `Updated ${resourceType} with ID ${resourceId}`); + assert.strictEqual( + new RegExp(`Updated ${resourceType} resource`).test(output), + true + ); }); it('should patch a FHIR resource', async () => { const output = await tools.runAsync( - `${cmd} patchResource ${datasetId} ${fhirStoreId} ${resourceType} ${resourceId}`, + `node fhir_resources.js patchResource ${datasetId} ${fhirStoreId} ${resourceType} ${resourceId}`, + cwd + ); + assert.strictEqual(output, `Patched ${resourceType} resource`); +}); + +it('should search for FHIR resources using GET', async () => { + const output = await tools.runAsync( + `node searchFhirResourcesGet.js ${projectId} ${cloudRegion} ${datasetId} ${fhirStoreId} ${resourceType}` + ); + assert.strictEqual(new RegExp('Resources found').test(output), true); +}); + +it('should search for FHIR resources using POST', async () => { + const output = await tools.runAsync( + `node searchFhirResourcesPost.js ${projectId} ${cloudRegion} ${datasetId} ${fhirStoreId} ${resourceType}` + ); + assert.strictEqual(new RegExp('Resources found').test(output), true); +}); + +it('should purge all historical versions of a FHIR resource', async () => { + const output = await tools.runAsync( + `node deleteFhirResourcePurge.js ${projectId} ${cloudRegion} ${datasetId} ${fhirStoreId} ${resourceType} ${resourceId}`, + cwd + ); + assert.strictEqual( + output, + `Deleted all historical versions of ${resourceType} resource` + ); +}); + +it('should execute a Bundle', async () => { + const output = await tools.runAsync( + `node fhir_resources.js executeBundle ${datasetId} ${fhirStoreId} ${bundleFile}`, cwd ); - assert.strictEqual(output, `Patched ${resourceType} with ID ${resourceId}`); + assert.strictEqual(new RegExp('Executed Bundle').test(output), true); }); it('should delete a FHIR resource', async () => { const output = await tools.runAsync( - `${cmd} deleteResource ${datasetId} ${fhirStoreId} ${resourceType} ${resourceId}`, + `node deleteFhirResource.js ${projectId} ${cloudRegion} ${datasetId} ${fhirStoreId} ${resourceType} ${resourceId}`, cwd ); - assert.strictEqual(output, `Deleted ${resourceType} with ID ${resourceId}.`); + assert.strictEqual(output, `Deleted FHIR resource ${resourceType}`); // Clean up await tools.runAsync( diff --git a/healthcare/fhir/updateFhirResource.js b/healthcare/fhir/updateFhirResource.js new file mode 100644 index 0000000000..6068a1a219 --- /dev/null +++ b/healthcare/fhir/updateFhirResource.js @@ -0,0 +1,63 @@ +/** + * Copyright 2019, Google, LLC + * Licensed under the Apache License, Version 2.0 (the `License`); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an `AS IS` BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* eslint-disable no-warning-comments */ + +'use strict'; + +function main( + projectId = process.env.GCLOUD_PROJECT, + cloudRegion = 'us-central1', + datasetId, + fhirStoreId, + resourceType, + resourceId +) { + // [START healthcare_update_fhir_resource] + const {google} = require('googleapis'); + const healthcare = google.healthcare('v1beta1'); + + async function updateFhirResource() { + const auth = await google.auth.getClient({ + scopes: ['https://www.googleapis.com/auth/cloud-platform'], + }); + google.options({ + auth, + data: {resourceType: resourceType, id: resourceId, active: true}, + headers: {'Content-Type': 'application/fhir+json'}, + }); + + // TODO(developer): uncomment these lines before running the sample + // const cloudRegion = 'us-central1'; + // const projectId = 'adjective-noun-123'; + // const datasetId = 'my-dataset'; + // const fhirStoreId = 'my-fhir-store'; + // const resourceType = 'Patient'; + // const resourceId = '16e8a860-33b3-49be-9b03-de979feed14a'; + const name = `projects/${projectId}/locations/${cloudRegion}/datasets/${datasetId}/fhirStores/${fhirStoreId}/fhir/${resourceType}/${resourceId}`; + const request = {name}; + + const resource = await healthcare.projects.locations.datasets.fhirStores.fhir.update( + request + ); + console.log(`Updated ${resourceType} resource:\n`, resource.data); + } + + updateFhirResource(); + // [END healthcare_update_fhir_resource] +} + +// node updateFhirResource.js +main(...process.argv.slice(2)); From 831fe12e02e2c442acef308db4765645b6d5746e Mon Sep 17 00:00:00 2001 From: noerog <32459203+noerog@users.noreply.github.com> Date: Wed, 3 Jul 2019 15:23:14 -0400 Subject: [PATCH 2/2] Fix delete resource purge region tags/method name. --- healthcare/fhir/deleteFhirResourcePurge.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/healthcare/fhir/deleteFhirResourcePurge.js b/healthcare/fhir/deleteFhirResourcePurge.js index 7d5c6bbeb9..88d594d03b 100644 --- a/healthcare/fhir/deleteFhirResourcePurge.js +++ b/healthcare/fhir/deleteFhirResourcePurge.js @@ -25,11 +25,11 @@ function main( resourceType, resourceId ) { - // [START healthcare_delete_fhir_resource] + // [START healthcare_delete_resource_purge] const {google} = require('googleapis'); const healthcare = google.healthcare('v1beta1'); - async function deleteFhirResource() { + async function deleteFhirResourcePurge() { const auth = await google.auth.getClient({ scopes: ['https://www.googleapis.com/auth/cloud-platform'], }); @@ -51,9 +51,9 @@ function main( console.log(`Deleted all historical versions of ${resourceType} resource`); } - deleteFhirResource(); - // [END healthcare_delete_fhir_resource] + deleteFhirResourcePurge(); + // [END healthcare_delete_resource_purge] } -// node deleteFhirResource.js +// node deleteFhirResourcePurge.js main(...process.argv.slice(2));