医疗保健 API(Ruby 客户)返回 null

Healthcare API (Ruby client) returning null

我正在尝试通过 Ruby 客户端从 FHIR 存储中获取 Patient,它总是 returns null。

我通过CURL查询成功了。这是 CURL 命令,我是 运行(已编辑完整路径):

curl -X GET \
  -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
"https://healthcare.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/datasets/DATASET_ID/fhirStores/FHIR_STORE_ID/fhir/Patient/PATIENT_ID"

这 returns 正确的 FHIR 患者资源。

我的 Ruby 代码如下:

require 'google/apis/healthcare_v1'
require 'googleauth'

service = Google::Apis::HealthcareV1::CloudHealthcareService.new

scope = 'https://www.googleapis.com/auth/cloud-platform'
service.authorization = Google::Auth::ServiceAccountCredentials.make_creds(
  json_key_io: File.open('REDACTED'), 
  scope: scope
)
service.authorization.fetch_access_token!

project_id = REDACTED
location = REDACTED
dataset_id = REDACTED
fhir_store_id = REDACTED
resource_type = 'Patient'
patient_id = REDACTED

name = "projects/#{project_id}/locations/#{location}/datasets/#{dataset_id}/fhirStores/#{fhir_store_id}/fhir/Patient/#{patient_id}"
response = service.read_project_location_dataset_fhir_store_fhir(name)
puts response.to_json

我没有收到任何身份验证错误。 CURL 示例 returns 适当的结果,而 Ruby 客户端示例 returns null.

有什么想法吗?

Ruby 库自动尝试将响应解析为 JSON。由于来自 Healthcare API(或任何 FHIR 服务器)的响应是 Content-Type: application/fhir+json,Ruby 库无法识别它,它只是 returns nil 用于解析的响应。

我通过对 API 调用 (docs) 使用 skip_deserialization 选项使它起作用,所以你应该尝试

require 'json'

name = "projects/#{project_id}/locations/#{location}/datasets/#{dataset_id}/fhirStores/#{fhir_store_id}/fhir/Patient/#{patient_id}"
response = service.read_project_location_dataset_fhir_store_fhir(name, options: {
  skip_deserialization: true,
})

patient = JSON.parse(response)

你实际上必须自己解析响应,因为这些调用的 Ruby 响应类型是 Google::Apis::HealthcareV1::HttpBody,它本质上只是原始 JSON 对象的包装器.