Skip to content

Commit 5e5b0fd

Browse files
committed
Fix bug in get_total_category_pages that would sometimes result in an infinit loop
1 parent 8b85025 commit 5e5b0fd

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

loading_sdk/async_api/client.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -576,10 +576,8 @@ async def get_total_category_pages(self, category):
576576
working_page = current_page
577577
current_page *= 2
578578

579-
# Check the page in the middle of highest known working page and
580-
# current page until they have the same page number.
581579
while True:
582-
page = working_page + (current_page - working_page) / 2
580+
page = working_page + math.floor((current_page - working_page) / 2)
583581
headers["page"] = str(page)
584582

585583
async with session.get(url, headers=headers) as response:
@@ -590,9 +588,13 @@ async def get_total_category_pages(self, category):
590588
else:
591589
current_page = page
592590

593-
if math.floor(current_page) == math.floor(working_page):
591+
if current_page - 1 == working_page:
594592
break
595593

596-
total_pages = math.floor(working_page)
594+
total_pages = working_page
597595

598-
return total_pages
596+
return {
597+
"code": 200,
598+
"message": "OK",
599+
"data": {"total_pages": total_pages},
600+
}

loading_sdk/sync_api/client.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -539,10 +539,8 @@ def get_total_category_pages(self, category):
539539
working_page = current_page
540540
current_page *= 2
541541

542-
# Check the page in the middle of highest known working page and
543-
# current page until they have the same page number.
544542
while True:
545-
page = working_page + (current_page - working_page) / 2
543+
page = working_page + math.floor((current_page - working_page) / 2)
546544
headers["page"] = str(page)
547545

548546
response = requests.get(url, headers=headers, timeout=10)
@@ -553,10 +551,10 @@ def get_total_category_pages(self, category):
553551
else:
554552
current_page = page
555553

556-
if math.floor(current_page) == math.floor(working_page):
554+
if current_page - 1 == working_page:
557555
break
558556

559-
total_pages = math.floor(working_page)
557+
total_pages = working_page
560558

561559
return {
562560
"code": 200,

0 commit comments

Comments
 (0)