beda-software / fhir-py

FHIR Client for python
MIT License
168 stars 31 forks source link

delete resource doesn't work #71

Closed danielcoam closed 3 years ago

danielcoam commented 3 years ago

I can't make the delete() resource function work

This is my remove-patient function

client = AsyncFHIRClient('https://fhir-server', authorization='Bearer my-token')

async def remove_patient(patient_id):
    patients = client.resources('Patient')
    patient = await patients.search(_id=patient_id).get()
    await patient.delete()

The Error I get from the FHIR server:

"description": "Invalid JSON payload received. Unknown name \"_format\": Cannot bind query parameter. Field 'format' could not be found in request message."

It looks like the problem is here : https://github.com/beda-software/fhir-py/blob/master/fhirpy/base/lib.py#L90 When adding the _format=json to the DELETE request

params = params or {}
params['_format'] = 'json' # The Problem
return f'{self.url}/{path.lstrip("/")}?{encode_params(params)}'

When I remove this line it works as expected.

Any help? Couldn't find an open issue for that

ir4y commented 3 years ago

Hi, @danielcoam Thank you for the contribution!

What FHIR server implementation do you use?

P.S. The documentation is confusing. Based on general guidelines http://hl7.org/fhir/http.html#styleguide _format may be passed. However _format doesn't define for DELETE http://hl7.org/fhir/http.html#delete . It looks wrong to me since DELETE may return OperationOutcome in some cases http://hl7.org/fhir/http.html#3.1.0.7.1 and it make sense to define the format of OperationOutcome resource by _format

danielcoam commented 3 years ago

Hi @ir4y Thanks for the response!

I use FHIR-R4 on Google Cloud Healthcare API

It looks (http://hl7.org/fhir/http.html#3.1.0.7.1 ) that in very rare cases when there is no support in a Conditional delete there might be an OperationOutcome response.

But the majority of DELETE operations won't support that.

What I did in our project meanwhile is that:

class CustomizedFHIRAsyncClient(AsyncFHIRClient):
    """
    There is a bug in the library see :https://github.com/beda-software/fhir-py/issues/71
    The only thing we are changing in the `AsyncFHIRClient` is removing the _format=json from DELETE operation
    """

    async def _do_request(self, method, path, data=None, params=None):
        headers = self._build_request_headers()
        url = self._build_request_url(path, params)
        if method.lower() == 'delete':  # The change from the original function
            url = url.split('_format=json')[0][:-1]
        async with aiohttp.request(
                method, url, json=data, headers=headers
        ) as r:
            if 200 <= r.status < 300:
                data = await r.text()
                return json.loads(data, object_hook=AttrDict)

            if r.status == 404 or r.status == 410:
                raise ResourceNotFound(await r.text())

            raise OperationOutcome(await r.text())

But it means we are on a fixed version(1.2.0) of the fhirpy package and we can't get any updates.

It's a major blocking that I think should be fixed in the library (the only thing that's weird for me is that there isn't an open bug already)

ruscoder commented 3 years ago

@danielcoam Now it should work. Please upgrade to 1.2.1