def fetch_things_with_retry(query, chunk_size=100, batch_fn=None, chunks=False, retry_min_wait_ms=500, max_retries=0):
 assert query._sort, 'you must specify the sort order in your query!'
 retrier = functools.partial(exponential_retrier, retry_min_wait_ms=retry_min_wait_ms, max_retries=max_retries)
 orig_rules = deepcopy(query._rules)
 query._limit = chunk_size
 items = retrier((lambda : list(query)))
 done = False
 while (items and (not done)):
  if (len(items) < chunk_size):
   done = True
  after = items[(-1)]
  if batch_fn:
   items = batch_fn(items)
  if chunks:
   (yield items)
  else:
   for i in items:
    (yield i)
  if (not done):
   query._rules = deepcopy(orig_rules)
   query._after(after)
   items = retrier((lambda : list(query)))

def fetch_things_with_retry ( query , chunk_size = 100 , batch_fn = None , chunks = False , retry_min_wait_ms = 500 , max_retries = 0 ) : assert query . _sort , 'you must specify the sort order in your query!' retrier = functools . partial ( exponential_retrier , retry_min_wait_ms = retry_min_wait_ms , max_retries = max_retries ) orig_rules = deepcopy ( query . _rules ) query . _limit = chunk_size items = retrier ( ( lambda : list ( query ) ) ) done = False while ( items and ( not done ) ) : if ( len ( items ) < chunk_size ) : done = True after = items [ ( - 1 ) ] if batch_fn : items = batch_fn ( items ) if chunks : ( yield items ) else : for i in items : ( yield i ) if ( not done ) : query . _rules = deepcopy ( orig_rules ) query . _after ( after ) items = retrier ( ( lambda : list ( query ) ) )
incrementally run query with a limit of chunk_size until there are no results left .