Skip to content

Commit 3b46290

Browse files
committed
further simplifications on update_next_page
1 parent c9ab628 commit 3b46290

1 file changed

Lines changed: 10 additions & 31 deletions

File tree

python-lib/pagination.py

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -66,29 +66,27 @@ def set_counting_key(self, counting_key):
6666
self.counting_key = counting_key
6767
logger.info("set_counting_key: counting_key set to {}".format(self.counting_key))
6868

69-
def update_next_page_offset(self, data, response_links=None):
70-
self.is_first_batch = False
71-
self.counter += 1
69+
def compute_batch_size(self, data):
7270
self.data_is_list = False
7371
if isinstance(data, list):
7472
self.data_is_list = True
7573
batch_size = len(data)
76-
self.records_to_skip = self.records_to_skip + batch_size
77-
if batch_size == 0:
78-
self.is_last_batch_empty = True
79-
logger.info("update_next_page_offset:data is list:batch_size={}, records_to_skip={}, is_last_batch_empty={}".format(
80-
batch_size, self.records_to_skip, self.is_last_batch_empty
81-
))
82-
return
8374
elif self.counting_key:
8475
extracted_data = get_value_from_path(data, self.counting_key.split("."), can_raise=False)
8576
if extracted_data:
8677
batch_size = len(extracted_data)
8778
else:
8879
batch_size = 0
89-
self.is_last_batch_empty = True
9080
else:
9181
batch_size = 1
82+
if batch_size == 0:
83+
self.is_last_batch_empty = True
84+
return batch_size
85+
86+
def update_next_page_offset(self, data, response_links=None):
87+
self.is_first_batch = False
88+
self.counter += 1
89+
batch_size = self.compute_batch_size(data)
9290
self.records_to_skip = self.records_to_skip + batch_size
9391
logger.info("update_next_page_offset:data_is_list={}, records_to_skip={}, batch_size={}, is_last_batch_empty={}".format(
9492
self.data_is_list, self.records_to_skip, batch_size, self.is_last_batch_empty
@@ -98,26 +96,7 @@ def update_next_page_per_page(self, data, response_links=None):
9896
self.is_first_batch = False
9997
self.counter += 1
10098
self.next_page_number = self.next_page_number + 1
101-
self.data_is_list = False
102-
103-
if isinstance(data, list):
104-
self.data_is_list = True
105-
batch_size = len(data)
106-
if batch_size == 0:
107-
self.is_last_batch_empty = True
108-
logger.info("update_next_page_per_page:data is list:batch_size={}, next_page_number={}, is_last_batch_empty={}".format(
109-
batch_size, self.next_page_number, self.is_last_batch_empty
110-
))
111-
return
112-
elif self.counting_key:
113-
extracted_data = get_value_from_path(data, self.counting_key.split("."), can_raise=False)
114-
if extracted_data:
115-
batch_size = len(extracted_data)
116-
else:
117-
batch_size = 0
118-
self.is_last_batch_empty = True
119-
else:
120-
batch_size = 1
99+
batch_size = self.compute_batch_size(data)
121100
logger.info("update_next_page_per_page:data_is_list={}, next_page_number={}, batch_size={}, is_last_batch_empty={}".format(
122101
self.data_is_list, self.next_page_number, batch_size, self.is_last_batch_empty
123102
))

0 commit comments

Comments
 (0)