Google Healthcare API (基本應用)
===
服務啟動
---
Google Healthcare API包含兩部分:資料集(Dataset)/資料儲存庫(FHIR Data Store),並提供一個FHIR檢視器,可線上查詢FHIR資料內容。

資料匯入
---
系統提供大量資料匯入/匯出/去識別化等功能

資料查詢
---
使用以下命令,應用Postman取得FHIR資料 : `GET <URL>/Patient/{patient_id}`
* URL: https://healthcare.googleapis.com/v1/projects/{project_id}/locations/{location}/datasets/{dataset_id}/fhirStores/{fhir_store_id}/fhir/
* patient_id:可使用FHIR檢視器取得相關資料
* Authorization: "Bearer $(auth gcloud auth print-access-token)"
其中Token使用Google Cloud SDK Shell取得

* Postman的執行結果:

* 官方網站:[建立及管理FHIR資源](https://cloud.google.com/healthcare-api/docs/how-tos/fhir-resources#curl)
python程式開發
---
使用python發展Google Healthcarfe API範例
1. 程式庫匯入
```
from googleapiclient import discovery
from google.oauth2 import service_account
from typing import Any, Dict
from google.auth.transport import requests
import json
```
2. Credential:呼叫Google Healthcare API必須使用Service Account搭配適當的權限。
```
cre_file = <credential json file name>
# create a credentials object from the service account file
credentials = service_account.Credentials.from_service_account_file(cre_file)
scoped_credentials = credentials.with_scopes(
["https://www.googleapis.com/auth/cloud-platform"]
)
```
3. 基本參數設定:包含project id、location、dataset id與fhir store id等GCP資源相關參數設定
```
api_version = "v1"
service_name = "healthcare"
project_id = <project_id> # replace with your GCP project ID
location = <location> # replace with the parent dataset's location
dataset_id = <dataset_id> # replace with the parent dataset's ID
fhir_store_id = <fhir_store_id> # replace with the FHIR store ID
fhir_store_parent = "projects/{}/locations/{}/datasets/{}".format(
project_id, location, dataset_id
)
fhir_store_name = f"{fhir_store_parent}/fhirStores/{fhir_store_id}"
# URL to the Cloud Healthcare API endpoint and version
base_url = "https://healthcare.googleapis.com/v1"
url = f"{base_url}/projects/{project_id}/locations/{location}"
fhir_store_path = "{}/datasets/{}/fhirStores/{}/fhir".format(
url, dataset_id, fhir_store_id
)
```
4. Discovering Healcare API:取得Gogle Healthcare API並執行作業,包含fhir store基本設定查詢(capability)與FHIR資料基本操作(CRUD)
* FHIR Store能力聲明
```
# Instantiates an authorized API client by discovering the Healthcare API
client = discovery.build(service_name, api_version, credentials=scoped_credentials)
fhir_stores = client.projects().locations().datasets().fhirStores()
response = fhir_stores.fhir().capabilities(name=fhir_store_name).execute()
print(json.dumps(response, indent=2))
```
* FHIR資料新增(以Patient為例)
```
patient_body = {
"name": [{"use": "official", "family": "Smith", "given": ["Katie"]}],
"gender": "male",
"birthDate": "2011-03-01",
"resourceType": "Patient",
}
request = (
client.projects()
.locations()
.datasets()
.fhirStores()
.fhir()
.create(parent=fhir_store_name, type="Patient", body=patient_body)
)
# Sets required application/fhir+json header on the googleapiclient.http.HttpRequest.
request.headers["content-type"] = "application/fhir+json;charset=utf-8"
response = request.execute()
print(f"Created Patient resource with ID {response['id']}")
```
* FHIR資料查詢(以Patient為例)
```
resource_type = "Patient"
resource_id = '23b7460b-45d9-f740-323f-ffd776fa5c05'
fhir_resource_path = f"{fhir_store_parent}/fhirStores/{fhir_store_id}/fhir/{resource_type}/{resource_id}"
request = (
client.projects()
.locations()
.datasets()
.fhirStores()
.fhir()
.read(name=fhir_resource_path)
)
print(f"Retrieved {resource_type} resource with ID {resource_id}:")
response = request.execute()
print(json.dumps(response, indent=2))
```
5. FHIR Data Search:進階的FHIR資料搜尋功能
```
# Creates a requests Session object with the credentials.
session = requests.AuthorizedSession(scoped_credentials)
resource_path = f"{fhir_store_path}/Patient/_search?family:exact=Zulauf375"
# Sets required application/fhir+json header on the request
headers = {"Content-Type": "application/fhir+json;charset=utf-8"}
response = session.post(resource_path, headers=headers)
response.raise_for_status()
resources = response.json()
print(
"Using POST request, found a total of {} Patient resources:".format(
resources["total"]
)
)
print(json.dumps(resources, indent=2))
```
6. fhirclient整合,讓資料處理應用更具彈性
```
import fhirclient.models.patient as p
p = resources['entry'][0]['resource']
print(p['name'][0]['family'])
```
Dot Net程式開發
---
1.程式庫匯入
```
#r "nuget:Hl7.Fhir.r4"
#r "nuget:Google.Apis.Auth"
#r "nuget:Google.Apis.Iam.v1"
#r "nuget:Google.Apis.IAMCredentials.v1"
```
2.基本參數設定
```
var api_version = "v1";
var service_name = "healthcare";
//# TODO(developer): Uncomment these lines and replace with your values.
var project_id = "sasabot";
var location = "asia-east1";
var dataset_id = "SasaHealthData";
var fhir_store_id = "sasa-healthcare";
var base_url = "https://healthcare.googleapis.com/v1";
var url = base_url + "/projects/" + project_id + "/locations/" + location + "/datasets/" + dataset_id + "/fhirStores/" + fhir_store_id + "/fhir";
```
3.取得Access Token
```
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
// Load the service account key file
var path = "D:\\Hongyu\\Project\\data\\key\\Google\\sasabot\\";
var jsonPath = path + "sasabot-0ee5855ec32e.json";
GoogleCredential credential = GoogleCredential.FromFileAsync(jsonPath, System.Threading.CancellationToken.None).Result;
credential = credential.CreateScoped(new string[] { "https://www.googleapis.com/auth/cloud-platform" });
var token = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync("", System.Threading.CancellationToken.None);
```
4.呼叫Google Healthcare API
```
// Http request
// use the token to make a request
using System.Net.Http;
using System.Threading.Tasks;
using Hl7.Fhir.Model;
using Hl7.Fhir.Serialization;
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");
var response = await client.GetAsync(url+"/Patient/e2551e8f-c1b4-a79f-0a02-f8ac5a6cfa83");
var content = await response.Content.ReadAsStringAsync();
```
5.結合Firely Dot NET SDK
```
using System.Text.Json;
var json = JsonDocument.Parse(content);
var parser = new FhirJsonParser();
Patient patient = parser.Parse<Patient>(content);
patient.Name[0].GivenElement[0].Value
```