Skip to content

Commit

Permalink
feat(pagination-Bundle): change iterator to iterable and fix _iter me…
Browse files Browse the repository at this point in the history
…thods
  • Loading branch information
LanaNYC committed Oct 8, 2024
1 parent dc4cd43 commit 6d0dbee
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 33 deletions.
15 changes: 1 addition & 14 deletions fhirclient/models/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,7 @@ def __init__(self, jsondict=None, strict=True):
super(Bundle, self).__init__(jsondict=jsondict, strict=strict)

def __iter__(self):
""" Makes the Bundle itself an iterator by returning an iterator over its entries. """
if self.entry is None:
self._entry_iter = iter([])
else:
self._entry_iter = iter(self.entry)
return self

def __next__(self):
""" Returns the next BundleEntry in the Bundle's entry list using the internal iterator. """
# return next(self._entry_iter)

if not hasattr(self, '_entry_iter'):
self.__iter__()
return next(self._entry_iter)
return iter(self.entry or [])

def elementProperties(self):
js = super(Bundle, self).elementProperties()
Expand Down
26 changes: 7 additions & 19 deletions fhirclient/models/fhirsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ def perform_iter(self, server) -> Iterator['Bundle']:
return iter_pages(self.perform(server))

def perform_resources(self, server) -> list['Resource']:
""" Performs the search by calling `perform`, then extracts all Bundle
entries and returns a list of Resource instances.
""" Performs the search by calling `perform_resources_iter` and returns a list of Resource instances.
:param server: The server against which to perform the search
:returns: A list of Resource instances
Expand All @@ -164,30 +163,19 @@ def perform_resources(self, server) -> list['Resource']:
DeprecationWarning,
)

bundle = self.perform(server)
resources = []
if bundle is not None and bundle.entry is not None:
for entry in bundle.entry:
resources.append(entry.resource)

return resources
return list(self.perform_resources_iter(server))

# Use forward references to avoid circular imports
def perform_resources_iter(self, server) -> Iterator['Resource']:
""" Performs the search by calling `perform`, then extracts all Bundle
entries and returns an iterator of Resource instances.
""" Performs the search by calling `perform_iter` and yields Resource instances
from each Bundle returned by the search.
:param server: The server against which to perform the search
:returns: An iterator of Resource instances
"""
first_bundle = self.perform(server)

if not first_bundle or not first_bundle.entry:
return iter([])

for bundle in iter_pages(first_bundle):
if bundle.entry:
yield from (entry.resource for entry in bundle.entry)
for bundle in self.perform_iter(server):
for entry in bundle:
yield entry.resource


class FHIRSearchParam(object):
Expand Down

0 comments on commit 6d0dbee

Please sign in to comment.