Skip to content

Commit

Permalink
Fix iteration helpers (and make their tests much more solid)
Browse files Browse the repository at this point in the history
Fixes #224
  • Loading branch information
ramnes committed Dec 28, 2023
1 parent 577ad9d commit 1380128
Show file tree
Hide file tree
Showing 5 changed files with 761 additions and 40 deletions.
24 changes: 12 additions & 12 deletions notion_client/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ def pick(base: Dict[Any, Any], *keys: str) -> Dict[Any, Any]:
"""Return a dict composed of key value pairs for keys passed as args."""
result = {}
for key in keys:
if key in base and key != "start_cursor":
result[key] = base.get(key)
if key not in base:
continue
value = base.get(key)
if value is None and key == "start_cursor":
continue
result[key] = value
return result


Expand Down Expand Up @@ -38,7 +42,8 @@ def iterate_paginated_api(

while True:
response = function(**kwargs, start_cursor=next_cursor)
yield response.get("results")
for result in response.get("results"):
yield result

next_cursor = response.get("next_cursor")
if not response.get("has_more") or not next_cursor:
Expand All @@ -47,10 +52,7 @@ def iterate_paginated_api(

def collect_paginated_api(function: Callable[..., Any], **kwargs: Any) -> List[Any]:
"""Collect all the results of paginating an API into a list."""
results = []
for partial_res in iterate_paginated_api(function, **kwargs):
results += partial_res
return results
return [result for result in iterate_paginated_api(function, **kwargs)]


async def async_iterate_paginated_api(
Expand All @@ -61,7 +63,8 @@ async def async_iterate_paginated_api(

while True:
response = await function(**kwargs, start_cursor=next_cursor)
yield response.get("results")
for result in response.get("results"):
yield result

next_cursor = response.get("next_cursor")
if (not response["has_more"]) | (next_cursor is None):
Expand All @@ -72,10 +75,7 @@ async def async_collect_paginated_api(
function: Callable[..., Awaitable[Any]], **kwargs: Any
) -> List[Any]:
"""Collect asynchronously all the results of paginating an API into a list."""
results = []
async for partial_res in async_iterate_paginated_api(function, **kwargs):
results += partial_res
return results
return [result async for result in async_iterate_paginated_api(function, **kwargs)]


def is_full_block(response: Dict[Any, Any]) -> bool:
Expand Down
1 change: 1 addition & 0 deletions requirements/tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
pytest
pytest-asyncio
pytest-cov
pytest-timeout
pytest-vcr
Loading

0 comments on commit 1380128

Please sign in to comment.