Skip to content

Commit

Permalink
Only consider arguments when they're handed over
Browse files Browse the repository at this point in the history
The idea is to keep `undefined` values when users don't explicitly set
`None`. We still want some hackery to hide `start_cursor=None` from
the API thought, because practicality beats purity. :)
  • Loading branch information
ramnes committed Dec 25, 2023
1 parent 28020cd commit 6fbaf83
Showing 1 changed file with 10 additions and 50 deletions.
60 changes: 10 additions & 50 deletions notion_client/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,13 @@
from urllib.parse import urlparse
from uuid import UUID

NOT_NULLABLE = (
"after",
"ai_block",
"archived",
"audio",
"bookmark",
"breadcrumb",
"bulleted_list_item",
"callout",
"code",
"description",
"discussion_id",
"divider",
"embed",
"equation",
"file",
"filter",
"heading_1",
"heading_2",
"heading_3",
"image",
"is_inline",
"link_to_page",
"numbered_list_item",
"page_size",
"pdf",
"properties",
"query",
"quote",
"sort",
"sorts",
"start_cursor",
"synced_block",
"table",
"table_of_contents",
"table_row",
"template",
"title",
"to_do",
"toggle",
"type",
"video",
)


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:
value = base.get(key)
if value is None and key in NOT_NULLABLE:
continue
result[key] = value
if key in base:
result[key] = base.get(key)
return result


Expand Down Expand Up @@ -83,7 +37,10 @@ def iterate_paginated_api(
next_cursor = kwargs.pop("start_cursor", None)

while True:
response = function(**kwargs, start_cursor=next_cursor)
if next_cursor:
kwargs["start_cursor"] = next_cursor

response = function(**kwargs)
yield response.get("results")

next_cursor = response.get("next_cursor")
Expand All @@ -106,7 +63,10 @@ async def async_iterate_paginated_api(
next_cursor = kwargs.pop("start_cursor", None)

while True:
response = await function(**kwargs, start_cursor=next_cursor)
if next_cursor:
kwargs["start_cursor"] = next_cursor

response = await function(**kwargs)
yield response.get("results")

next_cursor = response.get("next_cursor")
Expand Down

0 comments on commit 6fbaf83

Please sign in to comment.