From 56b5cbefe0c88b4cf848ac0e58d0ae6b2ebd0376 Mon Sep 17 00:00:00 2001 From: Tommy Au <75346987+smarttommyau@users.noreply.github.com> Date: Sun, 24 Dec 2023 11:53:27 +0800 Subject: [PATCH 1/2] Allow passing starting cursor in paginated helper Allow passing next_cursor to the paginated api to set the starting cursor. --- notion_client/helpers.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/notion_client/helpers.py b/notion_client/helpers.py index 7b339f5..d4d9028 100644 --- a/notion_client/helpers.py +++ b/notion_client/helpers.py @@ -27,11 +27,9 @@ def get_id(url: str) -> str: def iterate_paginated_api( - function: Callable[..., Any], **kwargs: Any + function: Callable[..., Any], next_cursor = None, **kwargs: Any ) -> Generator[List[Any], None, None]: """Return an iterator over the results of any paginated Notion API.""" - next_cursor = None - while True: response = function(**kwargs, start_cursor=next_cursor) yield response.get("results") @@ -50,11 +48,9 @@ def collect_paginated_api(function: Callable[..., Any], **kwargs: Any) -> List[A async def async_iterate_paginated_api( - function: Callable[..., Awaitable[Any]], **kwargs: Any + function: Callable[..., Awaitable[Any]], next_cursor = None, **kwargs: Any ) -> AsyncGenerator[List[Any], None]: """Return an async iterator over the results of any paginated Notion API.""" - next_cursor = None - while True: response = await function(**kwargs, start_cursor=next_cursor) yield response.get("results") From 8fdadac14cc1572d99ca3c248dc251c2d4061c47 Mon Sep 17 00:00:00 2001 From: ramnes Date: Mon, 25 Dec 2023 21:23:01 +0100 Subject: [PATCH 2/2] Take a slightly different approach --- notion_client/helpers.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/notion_client/helpers.py b/notion_client/helpers.py index d4d9028..2b44bab 100644 --- a/notion_client/helpers.py +++ b/notion_client/helpers.py @@ -27,9 +27,11 @@ def get_id(url: str) -> str: def iterate_paginated_api( - function: Callable[..., Any], next_cursor = None, **kwargs: Any + function: Callable[..., Any], **kwargs: Any ) -> Generator[List[Any], None, None]: """Return an iterator over the results of any paginated Notion API.""" + next_cursor = kwargs.pop("start_cursor", None) + while True: response = function(**kwargs, start_cursor=next_cursor) yield response.get("results") @@ -48,9 +50,11 @@ def collect_paginated_api(function: Callable[..., Any], **kwargs: Any) -> List[A async def async_iterate_paginated_api( - function: Callable[..., Awaitable[Any]], next_cursor = None, **kwargs: Any + function: Callable[..., Awaitable[Any]], **kwargs: Any ) -> AsyncGenerator[List[Any], None]: """Return an async iterator over the results of any paginated Notion API.""" + next_cursor = kwargs.pop("start_cursor", None) + while True: response = await function(**kwargs, start_cursor=next_cursor) yield response.get("results")