Skip to content

Commit aad1613

Browse files
author
Daniel Bush
committed
feat(SYPL-5699): use get_page
1 parent 20cd662 commit aad1613

File tree

3 files changed

+52
-15
lines changed

3 files changed

+52
-15
lines changed

sypht/client.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,11 @@ def get_annotations(
421421
):
422422
page_iter = fetch_all_pages(
423423
name="get_annotations",
424-
fetch_page=lambda *args, **kwargs: self._get_annotations(*args, **kwargs)[
425-
"annotations"
426-
],
424+
fetch_page=lambda *args, **kwargs: self._get_annotations(*args, **kwargs),
425+
get_page=lambda response: response["annotations"],
427426
)
428427
annotations = []
429-
for page in page_iter(
428+
for response in page_iter(
430429
doc_id=doc_id,
431430
task_id=task_id,
432431
user_id=user_id,
@@ -435,7 +434,7 @@ def get_annotations(
435434
to_date=to_date,
436435
endpoint=endpoint,
437436
):
438-
annotations.extend(page)
437+
annotations.extend(response["annotations"])
439438
return {"annotations": annotations}
440439

441440
def _get_annotations(
@@ -477,14 +476,15 @@ def get_annotations_for_docs(self, doc_ids, endpoint=None):
477476
name="get_annotations_for_docs",
478477
fetch_page=lambda *args, **kwargs: self._get_annotations_for_docs(
479478
*args, **kwargs
480-
)["annotations"],
479+
),
480+
get_page=lambda response: response["annotations"],
481481
)
482482
annotations = []
483-
for page in page_iter(
483+
for response in page_iter(
484484
doc_ids=doc_ids,
485485
endpoint=endpoint,
486486
):
487-
annotations.extend(page)
487+
annotations.extend(response["annotations"])
488488
return {"annotations": annotations}
489489

490490
def _get_annotations_for_docs(self, doc_ids, endpoint=None, offset=0):

sypht/util.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22

33

44
def fetch_all_pages(
5-
name: str, fetch_page: Callable[..., List[Any]], rec_limit=20000
6-
) -> Callable[..., Iterator[List[Any]]]:
7-
"""Returns an iterator that calls fetch_page with an offset that we increment by the number of pages fetched. Stop if page returns empty list."""
5+
name: str,
6+
fetch_page: Callable[..., Any],
7+
get_page: Callable[..., List[Any]] = lambda x: x,
8+
rec_limit=20000,
9+
) -> Callable[..., Iterator[Any]]:
10+
"""Returns an iterator that calls fetch_page with an offset that we increment by the number of pages fetched. Stop if page returns empty list.
11+
12+
:param fetch_page: the api call that takes an offset (zero-based) that fetches a page of results
13+
:param get_page: a function that extracts the page from the response
14+
"""
815

916
def fetch_all_pages(*args, **kwargs) -> Iterator[List[Any]]:
1017
page_count = 0
@@ -17,7 +24,7 @@ def fetch_all_pages(*args, **kwargs) -> Iterator[List[Any]]:
1724
f"fetch_all_pages({name}): fetched more than {rec_limit} items. Consider using adding or adjusting a filter to reduce the number of items fetched."
1825
)
1926
try:
20-
result = fetch_page(
27+
response = fetch_page(
2128
*args,
2229
**kwargs,
2330
offset=page_count - 1,
@@ -26,9 +33,15 @@ def fetch_all_pages(*args, **kwargs) -> Iterator[List[Any]]:
2633
raise Exception(
2734
f"Failed fetching for {name} for offset={page_count - 1}"
2835
) from err
29-
if not result:
36+
try:
37+
page = get_page(response)
38+
except Exception as err:
39+
raise Exception(
40+
f"get_page failed to extract page from response for {name} for offset={page_count - 1}"
41+
) from err
42+
if len(page) == 0:
3043
break
31-
recs += len(result)
32-
yield result
44+
recs += len(page)
45+
yield response
3346

3447
return fetch_all_pages

tests/tests_util.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,30 @@ def fetch_something(offset, pages=1):
2525
assert results == [0, 1, 2, 3, 4]
2626

2727

28+
def test_fetch_all_pages_can_fetch_one_page_with_get_page():
29+
# arrange
30+
page_size = 5
31+
32+
def fetch_something(offset, pages=1):
33+
pages0 = pages - 1
34+
if offset > pages0:
35+
return {"results": []}
36+
start = offset * page_size
37+
page = range(start, start + page_size)
38+
return {"results": list(page)}
39+
40+
# act
41+
page_iter = fetch_all_pages(
42+
name="test1", fetch_page=fetch_something, get_page=lambda resp: resp["results"]
43+
)
44+
results = []
45+
for resp in page_iter(pages=1):
46+
results += resp["results"]
47+
48+
# assert
49+
assert results == [0, 1, 2, 3, 4]
50+
51+
2852
def test_fetch_all_pages_can_fetch_several_pages():
2953
# arrange
3054
page_size = 5

0 commit comments

Comments
 (0)