@@ -8,8 +8,15 @@ class Pager(object):
88 """
99
1010 def __init__ (self , endpoint , request_opts = None ):
11- self ._endpoint = endpoint .get
11+ if hasattr (endpoint , 'get' ):
12+ # The simpliest case is to take an Endpoint and call its get
13+ self ._endpoint = endpoint .get
14+ else :
15+ # but if they pass a callable then use that instead (used internally)
16+ self ._endpoint = endpoint
17+
1218 self ._options = request_opts
19+ self ._length = None
1320
1421 # If we have options we could be starting on any page, backfill the count
1522 if self ._options :
@@ -20,6 +27,7 @@ def __init__(self, endpoint, request_opts=None):
2027 def __iter__ (self ):
2128 # Fetch the first page
2229 current_item_list , last_pagination_item = self ._endpoint (self ._options )
30+ self ._length = int (last_pagination_item .total_available )
2331
2432 # Get the rest on demand as a generator
2533 while self ._count < last_pagination_item .total_available :
@@ -34,48 +42,18 @@ def __iter__(self):
3442 # The total count on Server changed while fetching exit gracefully
3543 raise StopIteration
3644
37- def _load_next_page (self , last_pagination_item ):
38- next_page = last_pagination_item .page_number + 1
39- opts = RequestOptions (pagenumber = next_page , pagesize = last_pagination_item .page_size )
40- if self ._options is not None :
41- opts .sort , opts .filter = self ._options .sort , self ._options .filter
42- current_item_list , last_pagination_item = self ._endpoint (opts )
43- return current_item_list , last_pagination_item
44-
45- class InternalPager (object ):
46-
47- def __init__ (self , caller , * args , request_opts = None , ** kwargs ):
48- self ._endpoint = caller
49- self ._options = request_opts
50- self ._funcargs = args
51- self ._kwargs = kwargs
52- # If we have options we could be starting on any page, backfill the count
53- if self ._options :
54- self ._count = ((self ._options .pagenumber - 1 ) * self ._options .pagesize )
55- else :
56- self ._count = 0
57-
58- def __iter__ (self ):
59- # Fetch the first page
60- current_item_list , last_pagination_item = self ._endpoint (* self ._funcargs , ** self ._kwargs , req_options = self ._options )
61-
62- # Get the rest on demand as a generator
63- while self ._count < last_pagination_item .total_available :
64- if len (current_item_list ) == 0 :
65- current_item_list , last_pagination_item = self ._load_next_page (last_pagination_item )
66-
67- try :
68- yield current_item_list .pop (0 )
69- self ._count += 1
70-
71- except IndexError :
72- # The total count on Server changed while fetching exit gracefully
73- raise StopIteration
45+ def __len__ (self ):
46+ if not self ._length :
47+ # We have no length yet, so get the first page and then we'll know total size
48+ # TODO This isn't needed if we convert to list
49+ next (self .__iter__ ())
50+ return self ._length
51+ return self ._length
7452
7553 def _load_next_page (self , last_pagination_item ):
7654 next_page = last_pagination_item .page_number + 1
7755 opts = RequestOptions (pagenumber = next_page , pagesize = last_pagination_item .page_size )
7856 if self ._options is not None :
7957 opts .sort , opts .filter = self ._options .sort , self ._options .filter
80- current_item_list , last_pagination_item = self ._endpoint (* self . _funcargs , ** self . _kwargs , req_options = opts )
58+ current_item_list , last_pagination_item = self ._endpoint (opts )
8159 return current_item_list , last_pagination_item
0 commit comments