beda-software / fhir-py

FHIR Client for python
MIT License
174 stars 32 forks source link

fetch_all() Creating Infinite Loop. #47

Closed kylejbrk closed 4 years ago

kylejbrk commented 4 years ago

Not sure if this problem is specific to the FHIR Server I am pulling from, but supplying a page number will always return a bundle with resources. The current fetch_all() code uses this:

while True:
    new_resources = await self.page(page).fetch()
        if not new_resources:
            break

Since my server always returns something regardless of the page number, it gets stuck in the loop above. The way I've gotten around this in the past is to refer to the next link in the Bundle .

I suppose a quick and dirty solution would be something like the following:

async def fetch(self):
    bundle_data = await self.client._fetch_resource(
        self.resource_type, self.params
    )
    bundle_resource_type = bundle_data.get('resourceType', None)

    ...

    next_link = None
    for link in bundle_data.get('link', []):
        if link['relation'] == 'next':
            next_link = link['url']

    return resources, next_link

async def fetch_all(self):
    page = 1
    resources = []

    while True:
        new_resources, next_link = await self.page(page).fetch()
        resources.extend(new_resources)

        if not next_link:
            break

        page += 1

    return resources
mkizesov commented 4 years ago

Sure, I think it's a bug. Thank you for pointing this out, @kylejbrk . We'll fix it soon

kylejbrk commented 4 years ago

I tried digging for the page parameter in the docs, but I couldn't find it. It doesn't look like my client supports it at all, hence why it's getting stuck in the loop.

So perhaps the page parameter is not part of the FHIR standard?

mkizesov commented 4 years ago

Yep, of course. As far as I know now (according to this https://www.hl7.org/fhir/http.html#paging), FHIR standard doesn't specify which parameters should be used for paging. Client just have to use a link provided in "next" relation from a response (if exists).

mkizesov commented 4 years ago

Done.