How to use Google Cloud Healthcare API HttpBody class?

238 Views Asked by At

I'm having a hard time using a method setData() of the HttpBody class. I'm passing the parameter to the method as an object, but I recieve an error message.

How do I pass the parameter:

public function create(string $resource, $body)
{

        $client   = $this->googleClient();
        $service  = new CloudHealthcare($client);

        $parent   = "projects/my_project_id/locations/my_location/datasets/my_dataset/fhirStores/repository";

        $httpBody = new HttpBody();
        $httpBody->setContentType('application/fhir+json;charset=utf-8');
        $httpBody->setData([
            "resourceType" => "Patient",
            "id" => "23434",
            "meta" => [
                "versionId" => "12",
                "lastUpdated" => "2014-08-18T15:43:30Z"
            ],
            "text" => [
                "status" => "generated",
                "div" => "<!-- Snipped for Brevity -->"
            ],
            "extension" => [
                [
                    "url" => "http://example.org/consent#trials",
                    "valueCode" => "renal"
                ]
            ],
            "identifier" => [
                [
                    "use" => "usual",
                    "label" => "MRN",
                    "system" => "http://www.goodhealth.org/identifiers/mrn",
                    "value" => "123456"
                ]
            ],
            "name" => [
                [
                    "family" => [
                        "Levin"
                    ],
                    "given" => [
                        "Henry"
                    ],
                    "suffix" => [
                        "The 7th"
                    ]
                ]
            ],
            "gender" => [
                "text" => "Male"
            ],
            "birthDate" => "1932-09-24",
            "active" => true
        ]);

        $data     = $service->projects_locations_datasets_fhirStores_fhir->create($parent, $resource, $httpBody);

        return $data;
}

Following the error message I get. The error says I didn't pass the resourceType field, but it was passed:

Google\Service\Exception: {
"issue": [
{
  "code": "structure",
  "details": {
    "text": "unparseable_resource"
  },
  "diagnostics": "missing required field \"resourceType\"",
  "expression": [
    ""
  ],
  "severity": "error"
}
],
 "resourceType": "OperationOutcome"
 } in file /usr/share/nginx/vendor/google/apiclient/src/Http/REST.php on line 128

How should I pass the parameter to receive the success message? Tkanks!

1

There are 1 best solutions below

1
On

I haven't tried the PHP client libraries specifically, but for most languages you don't want to use the HttpBody class - it's an indication that the method accepts something in the body of the request that is just text from the perspective of the method signature. I would try passing the JSON string directly.