rubygoogle-api-clientgoogle-healthcare-api

Healthcare API (Ruby client) returning null


I'm trying to fetch a Patient from the FHIR store via the Ruby client and it always returns null.

I am successful when querying via CURL. Here is the CURL command I'm running (full path redacted):

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"

This returns the proper FHIR Patient resource.

My Ruby code looks like:

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

I'm not getting any authentication errors. The CURL example returns the appropriate result, while the Ruby client example returns null.

Any ideas?


Solution

  • The Ruby library automatically tries to parse the response as JSON. Since the responses from the Healthcare API (or any FHIR server) is Content-Type: application/fhir+json, this isn't recognized by the Ruby library, and it just returns nil for the parsed response.

    I got this to work by using the skip_deserialization option for the API call (docs), so instead you should try

    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)
    

    You would actually have to parse the response yourself anyways, because the Ruby response type for these calls is Google::Apis::HealthcareV1::HttpBody, which is essentially just a wrapper around a raw JSON object.