client-py icon indicating copy to clipboard operation
client-py copied to clipboard

Reading all paginated results from a bundle

Open bktrinadh opened this issue 8 years ago • 2 comments

Hi,

I am reading observation data of a patient.. The result are paginated.. I want to process paginated results and retrieve all the observations. Is there a way to do that without implementing my own code? I implemented a code but I am not sure if this is right implementation..

def _handle_search(smart, klass, struct):
    bundle = klass.where(struct).perform(smart.server)
    result = list()
    while True:
        logger.info(json.dumps(bundle.as_json(), indent=2))
        entries = [be.resource for be in bundle.entry] if bundle is not None and bundle.entry is not None else None
        logger.info('Retrieved {}/{} entries...'.format(
            len(bundle.entry) if bundle.entry else 0, bundle.total))
        if entries is not None and len(entries) > 0:
            result += entries

        url_params = _get_next_url_params(bundle.link)
        if len(url_params) > 1:
            bundle = klass.where(url_params).perform(smart.server)
        else:
            break

    return result


def _get_next_url_params(links):
    url = None
    for link in links:
        if link.relation == 'next':
            url = link.url

    params = {}
    if url is not None:
        import urlparse
        params = dict(urlparse.parse_qsl(urlparse.urlsplit(url).query))
    return params

Thanks Trinadh..

bktrinadh avatar May 03 '17 09:05 bktrinadh

I'm having the same issue with this, but I am using perform_resources.

kylejbrk avatar Sep 17 '19 14:09 kylejbrk

Duplicate issue here with some additional helpful info: https://github.com/smart-on-fhir/client-py/issues/108

ebrahimebrahim avatar Apr 09 '22 05:04 ebrahimebrahim