Skip to content

Commit

Permalink
Drop None values when the API only accepts undefined
Browse files Browse the repository at this point in the history
See the discussion in #101 for the rationale regarding the implementation.

Fixes #219
Closes #220 (supersedes)
Related to #218
  • Loading branch information
ramnes committed Dec 25, 2023
1 parent c1faf0a commit 28020cd
Show file tree
Hide file tree
Showing 42 changed files with 539 additions and 438 deletions.
52 changes: 51 additions & 1 deletion notion_client/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,60 @@
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."""
return {key: base[key] for key in keys if key in base and base[key] is not None}
result = {}
for key in keys:
value = base.get(key)
if value is None and key in NOT_NULLABLE:
continue
result[key] = value
return result


def get_url(object_id: str) -> str:
Expand Down
2 changes: 1 addition & 1 deletion tests/cassettes/test_api_response_error.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ interactions:
uri: https://api.notion.com/v1/users
response:
content: '{"object":"error","status":401,"code":"unauthorized","message":"API
token is invalid.","request_id":"f8d8b05a-5cb0-4a90-8a67-db7ac50b3c62"}'
token is invalid.","request_id":"da934f68-ceb1-42a9-b0e4-6f254572d4aa"}'
headers: {}
http_version: HTTP/1.1
status_code: 401
Expand Down
2 changes: 1 addition & 1 deletion tests/cassettes/test_async_api_response_error.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ interactions:
uri: https://api.notion.com/v1/users
response:
content: '{"object":"error","status":401,"code":"unauthorized","message":"API
token is invalid.","request_id":"b5bb6768-751f-43d1-8ed6-03ed8c318f0f"}'
token is invalid.","request_id":"38daaeec-a2c3-4d66-baf8-795cf5bd787e"}'
headers: {}
http_version: HTTP/1.1
status_code: 401
Expand Down
2 changes: 1 addition & 1 deletion tests/cassettes/test_async_client_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ interactions:
uri: https://api.notion.com/v1/users
response:
content: '{"object":"list","results":[{"object":"user","id":"a4f789cc-7bc8-4cf0-82b9-a8ba7d985ecf","name":"Guillaume
Gelin","avatar_url":"https://s3-us-west-2.amazonaws.com/public.notion-static.com/1cddf30d-ef25-4372-a8bd-b510a11e26e8/avatar.jpeg","type":"person","person":{"email":"notion@ramnes.eu"}},{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78","name":"notion-sdk-py","avatar_url":null,"type":"bot","bot":{"owner":{"type":"workspace","workspace":true},"workspace_name":"notion-sdk-py"}}],"next_cursor":null,"has_more":false,"type":"user","user":{},"request_id":"efc82635-6f20-473b-97c7-2086f292dce3"}'
Gelin","avatar_url":"https://s3-us-west-2.amazonaws.com/public.notion-static.com/01d7053d-e135-4f27-bba0-5de532d39296/ramnes3.jpeg","type":"person","person":{"email":"notion@ramnes.eu"}},{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78","name":"notion-sdk-py","avatar_url":null,"type":"bot","bot":{"owner":{"type":"workspace","workspace":true},"workspace_name":"notion-sdk-py"}}],"next_cursor":null,"has_more":false,"type":"user","user":{},"request_id":"b896c45a-4b3c-4c1d-8f74-74c918277563"}'
headers: {}
http_version: HTTP/1.1
status_code: 200
Expand Down
4 changes: 2 additions & 2 deletions tests/cassettes/test_async_client_request_auth.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interactions:
uri: https://api.notion.com/v1/users
response:
content: '{"object":"error","status":401,"code":"unauthorized","message":"API
token is invalid.","request_id":"c9a6f21b-c688-4c0e-9a68-3ff503ccef7f"}'
token is invalid.","request_id":"55e52eed-c17d-4695-8cde-49e01bb83990"}'
headers: {}
http_version: HTTP/1.1
status_code: 401
Expand All @@ -41,7 +41,7 @@ interactions:
uri: https://api.notion.com/v1/users
response:
content: '{"object":"list","results":[{"object":"user","id":"a4f789cc-7bc8-4cf0-82b9-a8ba7d985ecf","name":"Guillaume
Gelin","avatar_url":"https://s3-us-west-2.amazonaws.com/public.notion-static.com/1cddf30d-ef25-4372-a8bd-b510a11e26e8/avatar.jpeg","type":"person","person":{"email":"notion@ramnes.eu"}},{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78","name":"notion-sdk-py","avatar_url":null,"type":"bot","bot":{"owner":{"type":"workspace","workspace":true},"workspace_name":"notion-sdk-py"}}],"next_cursor":null,"has_more":false,"type":"user","user":{},"request_id":"362d4f97-6a25-421c-aad7-6c4e6cc4cb88"}'
Gelin","avatar_url":"https://s3-us-west-2.amazonaws.com/public.notion-static.com/01d7053d-e135-4f27-bba0-5de532d39296/ramnes3.jpeg","type":"person","person":{"email":"notion@ramnes.eu"}},{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78","name":"notion-sdk-py","avatar_url":null,"type":"bot","bot":{"owner":{"type":"workspace","workspace":true},"workspace_name":"notion-sdk-py"}}],"next_cursor":null,"has_more":false,"type":"user","user":{},"request_id":"4230ed69-a603-4427-b878-1417970f557c"}'
headers: {}
http_version: HTTP/1.1
status_code: 200
Expand Down
6 changes: 2 additions & 4 deletions tests/cassettes/test_async_collect_paginated_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ interactions:
method: POST
uri: https://api.notion.com/v1/search
response:
content: '{"object":"list","results":[{"object":"page","id":"95ba0116-6776-4c19-9e45-54e77415f03b","created_time":"2023-06-29T12:47:00.000Z","last_edited_time":"2023-10-19T17:16:00.000Z","created_by":{"object":"user","id":"a4f789cc-7bc8-4cf0-82b9-a8ba7d985ecf"},"last_edited_by":{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78"},"cover":null,"icon":null,"parent":{"type":"workspace","workspace":true},"archived":false,"properties":{"title":{"id":"title","type":"title","title":[]}},"url":"https://www.notion.so/95ba011667764c199e4554e77415f03b","public_url":null},{"object":"page","id":"ecae9769-155a-49e2-bf03-884090bfadf1","created_time":"2023-10-19T17:15:00.000Z","last_edited_time":"2023-10-19T17:15:00.000Z","created_by":{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78"},"last_edited_by":{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78"},"cover":null,"icon":null,"parent":{"type":"page_id","page_id":"95ba0116-6776-4c19-9e45-54e77415f03b"},"archived":false,"properties":{"title":{"id":"title","type":"title","title":[{"type":"text","text":{"content":"Test
Page","link":null},"annotations":{"bold":false,"italic":false,"strikethrough":false,"underline":false,"code":false,"color":"default"},"plain_text":"Test
Page","href":null}]}},"url":"https://www.notion.so/Test-Page-ecae9769155a49e2bf03884090bfadf1","public_url":null}],"next_cursor":null,"has_more":false,"type":"page_or_database","page_or_database":{},"request_id":"a7957e2d-c6a7-4764-96df-f58a68385ce4"}'
content: '{"object":"list","results":[{"object":"page","id":"95ba0116-6776-4c19-9e45-54e77415f03b","created_time":"2023-06-29T12:47:00.000Z","last_edited_time":"2023-12-25T21:51:00.000Z","created_by":{"object":"user","id":"a4f789cc-7bc8-4cf0-82b9-a8ba7d985ecf"},"last_edited_by":{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78"},"cover":null,"icon":null,"parent":{"type":"workspace","workspace":true},"archived":false,"properties":{"title":{"id":"title","type":"title","title":[]}},"url":"https://www.notion.so/95ba011667764c199e4554e77415f03b","public_url":null}],"next_cursor":null,"has_more":false,"type":"page_or_database","page_or_database":{},"request_id":"199e29d6-fd4c-45f5-ad45-cad7bdcde213"}'
headers: {}
http_version: HTTP/1.1
status_code: 200
Expand All @@ -49,7 +47,7 @@ interactions:
method: POST
uri: https://api.notion.com/v1/search
response:
content: '{"object":"list","results":[],"next_cursor":null,"has_more":false,"type":"page_or_database","page_or_database":{},"request_id":"1bb2c909-18ad-4a62-8dfa-89577ca94624"}'
content: '{"object":"list","results":[],"next_cursor":null,"has_more":false,"type":"page_or_database","page_or_database":{},"request_id":"2b07b4e1-ef07-4bfc-9765-b3e44f5201c8"}'
headers: {}
http_version: HTTP/1.1
status_code: 200
Expand Down
6 changes: 2 additions & 4 deletions tests/cassettes/test_async_iterate_paginated_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ interactions:
method: POST
uri: https://api.notion.com/v1/search
response:
content: '{"object":"list","results":[{"object":"page","id":"95ba0116-6776-4c19-9e45-54e77415f03b","created_time":"2023-06-29T12:47:00.000Z","last_edited_time":"2023-10-19T17:16:00.000Z","created_by":{"object":"user","id":"a4f789cc-7bc8-4cf0-82b9-a8ba7d985ecf"},"last_edited_by":{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78"},"cover":null,"icon":null,"parent":{"type":"workspace","workspace":true},"archived":false,"properties":{"title":{"id":"title","type":"title","title":[]}},"url":"https://www.notion.so/95ba011667764c199e4554e77415f03b","public_url":null},{"object":"page","id":"ecae9769-155a-49e2-bf03-884090bfadf1","created_time":"2023-10-19T17:15:00.000Z","last_edited_time":"2023-10-19T17:15:00.000Z","created_by":{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78"},"last_edited_by":{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78"},"cover":null,"icon":null,"parent":{"type":"page_id","page_id":"95ba0116-6776-4c19-9e45-54e77415f03b"},"archived":false,"properties":{"title":{"id":"title","type":"title","title":[{"type":"text","text":{"content":"Test
Page","link":null},"annotations":{"bold":false,"italic":false,"strikethrough":false,"underline":false,"code":false,"color":"default"},"plain_text":"Test
Page","href":null}]}},"url":"https://www.notion.so/Test-Page-ecae9769155a49e2bf03884090bfadf1","public_url":null}],"next_cursor":null,"has_more":false,"type":"page_or_database","page_or_database":{},"request_id":"32e6b848-a747-46cf-93e2-af5ea04087ac"}'
content: '{"object":"list","results":[{"object":"page","id":"95ba0116-6776-4c19-9e45-54e77415f03b","created_time":"2023-06-29T12:47:00.000Z","last_edited_time":"2023-12-25T21:51:00.000Z","created_by":{"object":"user","id":"a4f789cc-7bc8-4cf0-82b9-a8ba7d985ecf"},"last_edited_by":{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78"},"cover":null,"icon":null,"parent":{"type":"workspace","workspace":true},"archived":false,"properties":{"title":{"id":"title","type":"title","title":[]}},"url":"https://www.notion.so/95ba011667764c199e4554e77415f03b","public_url":null}],"next_cursor":null,"has_more":false,"type":"page_or_database","page_or_database":{},"request_id":"597ada3b-af65-47db-9044-4e58eeced105"}'
headers: {}
http_version: HTTP/1.1
status_code: 200
Expand All @@ -49,7 +47,7 @@ interactions:
method: POST
uri: https://api.notion.com/v1/search
response:
content: '{"object":"list","results":[],"next_cursor":null,"has_more":false,"type":"page_or_database","page_or_database":{},"request_id":"49b7fb70-e2d0-4e61-b4aa-b52a7cec2a16"}'
content: '{"object":"list","results":[],"next_cursor":null,"has_more":false,"type":"page_or_database","page_or_database":{},"request_id":"c3a5ab6b-78c0-4c52-b5fd-b12eb19e71bf"}'
headers: {}
http_version: HTTP/1.1
status_code: 200
Expand Down
24 changes: 12 additions & 12 deletions tests/cassettes/test_blocks_children_create.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
interactions:
- request:
body: '{"parent": {"page_id": "95ba011667764c199e4554e77415f03b"}, "properties":
{"title": [{"text": {"content": "Test 2023-10-19 19:15:42.948193"}}]}, "children":
[]}'
{"title": [{"text": {"content": "Test 2023-12-25 22:50:27.560795"}}]}, "children":
[], "icon": null, "cover": null}'
headers:
accept:
- '*/*'
Expand All @@ -13,7 +13,7 @@ interactions:
connection:
- keep-alive
content-length:
- '160'
- '189'
content-type:
- application/json
host:
Expand All @@ -23,9 +23,9 @@ interactions:
method: POST
uri: https://api.notion.com/v1/pages
response:
content: '{"object":"page","id":"8cdca001-83f3-489a-925f-3111990cb922","created_time":"2023-10-19T17:15:00.000Z","last_edited_time":"2023-10-19T17:15:00.000Z","created_by":{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78"},"last_edited_by":{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78"},"cover":null,"icon":null,"parent":{"type":"page_id","page_id":"95ba0116-6776-4c19-9e45-54e77415f03b"},"archived":false,"properties":{"title":{"id":"title","type":"title","title":[{"type":"text","text":{"content":"Test
2023-10-19 19:15:42.948193","link":null},"annotations":{"bold":false,"italic":false,"strikethrough":false,"underline":false,"code":false,"color":"default"},"plain_text":"Test
2023-10-19 19:15:42.948193","href":null}]}},"url":"https://www.notion.so/Test-2023-10-19-19-15-42-948193-8cdca00183f3489a925f3111990cb922","public_url":null,"request_id":"3bb8feee-65df-419e-8e63-79b15bae04d0"}'
content: '{"object":"page","id":"0aba0902-a874-4f6c-b31b-797e810a4ae0","created_time":"2023-12-25T21:50:00.000Z","last_edited_time":"2023-12-25T21:50:00.000Z","created_by":{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78"},"last_edited_by":{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78"},"cover":null,"icon":null,"parent":{"type":"page_id","page_id":"95ba0116-6776-4c19-9e45-54e77415f03b"},"archived":false,"properties":{"title":{"id":"title","type":"title","title":[{"type":"text","text":{"content":"Test
2023-12-25 22:50:27.560795","link":null},"annotations":{"bold":false,"italic":false,"strikethrough":false,"underline":false,"code":false,"color":"default"},"plain_text":"Test
2023-12-25 22:50:27.560795","href":null}]}},"url":"https://www.notion.so/Test-2023-12-25-22-50-27-560795-0aba0902a8744f6cb31b797e810a4ae0","public_url":null,"request_id":"3727d5fb-23c8-4c8c-bba5-916e4fdc6ed9"}'
headers: {}
http_version: HTTP/1.1
status_code: 200
Expand All @@ -50,11 +50,11 @@ interactions:
notion-version:
- '2022-06-28'
method: PATCH
uri: https://api.notion.com/v1/blocks/8cdca001-83f3-489a-925f-3111990cb922/children
uri: https://api.notion.com/v1/blocks/0aba0902-a874-4f6c-b31b-797e810a4ae0/children
response:
content: '{"object":"list","results":[{"object":"block","id":"30c03277-bf1c-407a-96f2-c3684912f556","parent":{"type":"page_id","page_id":"8cdca001-83f3-489a-925f-3111990cb922"},"created_time":"2023-10-19T17:15:00.000Z","last_edited_time":"2023-10-19T17:15:00.000Z","created_by":{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78"},"last_edited_by":{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78"},"has_children":false,"archived":false,"type":"paragraph","paragraph":{"rich_text":[{"type":"text","text":{"content":"I''m
content: '{"object":"list","results":[{"object":"block","id":"60d42169-2d25-41df-a04b-67ce684bddfd","parent":{"type":"page_id","page_id":"0aba0902-a874-4f6c-b31b-797e810a4ae0"},"created_time":"2023-12-25T21:50:00.000Z","last_edited_time":"2023-12-25T21:50:00.000Z","created_by":{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78"},"last_edited_by":{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78"},"has_children":false,"archived":false,"type":"paragraph","paragraph":{"rich_text":[{"type":"text","text":{"content":"I''m
a paragraph.","link":null},"annotations":{"bold":false,"italic":false,"strikethrough":false,"underline":false,"code":false,"color":"default"},"plain_text":"I''m
a paragraph.","href":null}],"color":"default"}}],"next_cursor":null,"has_more":false,"type":"block","block":{},"request_id":"7cc95e93-aae0-4dc8-bba7-b96ab49747a2"}'
a paragraph.","href":null}],"color":"default"}}],"next_cursor":null,"has_more":false,"type":"block","block":{},"request_id":"5ca58bc6-a798-458c-8696-6a0f1587e398"}'
headers: {}
http_version: HTTP/1.1
status_code: 200
Expand All @@ -74,10 +74,10 @@ interactions:
notion-version:
- '2022-06-28'
method: DELETE
uri: https://api.notion.com/v1/blocks/8cdca001-83f3-489a-925f-3111990cb922
uri: https://api.notion.com/v1/blocks/0aba0902-a874-4f6c-b31b-797e810a4ae0
response:
content: '{"object":"block","id":"8cdca001-83f3-489a-925f-3111990cb922","parent":{"type":"page_id","page_id":"95ba0116-6776-4c19-9e45-54e77415f03b"},"created_time":"2023-10-19T17:15:00.000Z","last_edited_time":"2023-10-19T17:15:00.000Z","created_by":{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78"},"last_edited_by":{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78"},"has_children":true,"archived":true,"type":"child_page","child_page":{"title":"Test
2023-10-19 19:15:42.948193"},"request_id":"41284b00-e0d4-4237-9182-d1de14ba56ef"}'
content: '{"object":"block","id":"0aba0902-a874-4f6c-b31b-797e810a4ae0","parent":{"type":"page_id","page_id":"95ba0116-6776-4c19-9e45-54e77415f03b"},"created_time":"2023-12-25T21:50:00.000Z","last_edited_time":"2023-12-25T21:50:00.000Z","created_by":{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78"},"last_edited_by":{"object":"user","id":"7775f3a3-893f-43fa-b625-460c61094c78"},"has_children":true,"archived":true,"type":"child_page","child_page":{"title":"Test
2023-12-25 22:50:27.560795"},"request_id":"6b158f9b-f727-47d6-b414-902b28c8974e"}'
headers: {}
http_version: HTTP/1.1
status_code: 200
Expand Down
Loading

0 comments on commit 28020cd

Please sign in to comment.