1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
class ResourceCollection(object):
"""
A class representing results from a search. Iterate over the results by calling items::
results = braintree.Transaction.search("411111")
for transaction in results.items:
print transaction.id
"""
def __init__(self, query, results, method):
self.__page_size = results["search_results"]["page_size"]
self.__ids = results["search_results"]["ids"]
self.__query = query
self.__method = method
@property
def maximum_size(self):
"""
Returns the approximate size of the results. The size is approximate due to race conditions when pulling
back results. Due to its inexact nature, maximum_size should be avoided.
"""
return len(self.__ids)
@property
def first(self):
""" Returns the first item in the results. """
return self.__method(self.__query, self.__ids[0:1])[0]
@property
def items(self):
""" Returns a generator allowing iteration over all of the results. """
for batch in self.__batch_ids():
for item in self.__method(self.__query, batch):
yield item
def __batch_ids(self):
for i in range(0, len(self.__ids), self.__page_size):
yield self.__ids[i:i+self.__page_size]
@staticmethod
def _extract_as_array(results, attribute):
if not attribute in results:
return []
value = results[attribute]
if not isinstance(value, list):
value = [value]
return value
|