client-py
client-py copied to clipboard
Reading all paginated results from a bundle
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..
I'm having the same issue with this, but I am using perform_resources.
Duplicate issue here with some additional helpful info: https://github.com/smart-on-fhir/client-py/issues/108