notion-database 2.0.dev0__py3-none-any.whl → 2.0.dev1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- notion_database/__init__.py +69 -3
- notion_database/api/blocks.py +141 -0
- notion_database/api/comments.py +73 -0
- notion_database/api/databases.py +209 -0
- notion_database/api/pages.py +183 -0
- notion_database/api/search.py +142 -0
- notion_database/api/users.py +78 -0
- notion_database/client.py +84 -0
- notion_database/const/__init__.py +46 -1
- notion_database/exceptions.py +51 -0
- notion_database/http/client.py +109 -0
- notion_database/models/__init__.py +20 -0
- notion_database/models/blocks.py +528 -0
- notion_database/models/filters.py +420 -0
- notion_database/models/icons.py +70 -0
- notion_database/models/properties.py +548 -0
- notion_database/models/rich_text.py +160 -0
- notion_database/models/sorts.py +75 -0
- notion_database-2.0.dev1.dist-info/METADATA +355 -0
- notion_database-2.0.dev1.dist-info/RECORD +26 -0
- {notion_database-2.0.dev0.dist-info → notion_database-2.0.dev1.dist-info}/WHEEL +1 -1
- {notion_database-2.0.dev0.dist-info → notion_database-2.0.dev1.dist-info}/top_level.txt +0 -1
- notion_database/NotionDatabase.py +0 -107
- notion_database/components/request.py +0 -58
- notion_database/const/query.py +0 -38
- notion_database/service/block.py +0 -43
- notion_database/service/children.py +0 -520
- notion_database/service/cover.py +0 -43
- notion_database/service/database.py +0 -179
- notion_database/service/icon.py +0 -61
- notion_database/service/page.py +0 -118
- notion_database/service/properties.py +0 -220
- notion_database/service/search.py +0 -78
- notion_database/utils/__init__.py +0 -0
- notion_database/utils/deprecate.py +0 -21
- notion_database-2.0.dev0.dist-info/METADATA +0 -132
- notion_database-2.0.dev0.dist-info/RECORD +0 -28
- tests/__init__.py +0 -1
- tests/test_children.py +0 -286
- tests/test_cover.py +0 -22
- tests/test_icon.py +0 -33
- tests/test_properties.py +0 -245
- /notion_database/{components → api}/__init__.py +0 -0
- /notion_database/{service → http}/__init__.py +0 -0
- {notion_database-2.0.dev0.dist-info → notion_database-2.0.dev1.dist-info/licenses}/LICENSE +0 -0
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Notion Search API – https://developers.notion.com/reference/post-search
|
|
3
|
+
"""
|
|
4
|
+
from typing import Any, Dict, List, Optional
|
|
5
|
+
|
|
6
|
+
from notion_database.http.client import HttpClient
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class SearchAPI:
|
|
10
|
+
"""1-to-1 mapping of the Notion Search endpoint.
|
|
11
|
+
|
|
12
|
+
Reference: https://developers.notion.com/reference/post-search
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
def __init__(self, http: HttpClient) -> None:
|
|
16
|
+
self._http = http
|
|
17
|
+
|
|
18
|
+
def search(
|
|
19
|
+
self,
|
|
20
|
+
query: str = "",
|
|
21
|
+
*,
|
|
22
|
+
filter: Optional[Dict] = None,
|
|
23
|
+
sort: Optional[Dict] = None,
|
|
24
|
+
start_cursor: Optional[str] = None,
|
|
25
|
+
page_size: int = 100,
|
|
26
|
+
) -> Dict:
|
|
27
|
+
"""Search all pages and databases that the integration has access to.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
query: Text to search for. Pass an empty string to list all
|
|
31
|
+
accessible objects.
|
|
32
|
+
filter: Object type filter. ``{"value": "page", "property": "object"}``
|
|
33
|
+
or ``{"value": "database", "property": "object"}``.
|
|
34
|
+
sort: Sort criteria dict, e.g.
|
|
35
|
+
``{"direction": "ascending", "timestamp": "last_edited_time"}``.
|
|
36
|
+
start_cursor: Cursor for pagination.
|
|
37
|
+
page_size: Number of results per page (1-100).
|
|
38
|
+
|
|
39
|
+
Returns:
|
|
40
|
+
Notion list object with ``results``, ``has_more``, and
|
|
41
|
+
``next_cursor`` fields.
|
|
42
|
+
|
|
43
|
+
Reference: https://developers.notion.com/reference/post-search
|
|
44
|
+
"""
|
|
45
|
+
body: Dict[str, Any] = {
|
|
46
|
+
"query": query,
|
|
47
|
+
"page_size": page_size,
|
|
48
|
+
}
|
|
49
|
+
if filter is not None:
|
|
50
|
+
body["filter"] = filter
|
|
51
|
+
if sort is not None:
|
|
52
|
+
body["sort"] = sort
|
|
53
|
+
if start_cursor is not None:
|
|
54
|
+
body["start_cursor"] = start_cursor
|
|
55
|
+
return self._http.post("/search", body)
|
|
56
|
+
|
|
57
|
+
def search_databases(
|
|
58
|
+
self,
|
|
59
|
+
query: str = "",
|
|
60
|
+
*,
|
|
61
|
+
sort: Optional[Dict] = None,
|
|
62
|
+
start_cursor: Optional[str] = None,
|
|
63
|
+
page_size: int = 100,
|
|
64
|
+
) -> Dict:
|
|
65
|
+
"""Search only databases.
|
|
66
|
+
|
|
67
|
+
Convenience wrapper around :meth:`search` with the database filter
|
|
68
|
+
pre-applied.
|
|
69
|
+
|
|
70
|
+
Args:
|
|
71
|
+
query: Text to search for.
|
|
72
|
+
sort: Sort criteria dict.
|
|
73
|
+
start_cursor: Cursor for pagination.
|
|
74
|
+
page_size: Number of results per page.
|
|
75
|
+
|
|
76
|
+
Returns:
|
|
77
|
+
Notion list object containing only database objects.
|
|
78
|
+
"""
|
|
79
|
+
return self.search(
|
|
80
|
+
query,
|
|
81
|
+
filter={"value": "database", "property": "object"},
|
|
82
|
+
sort=sort,
|
|
83
|
+
start_cursor=start_cursor,
|
|
84
|
+
page_size=page_size,
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
def search_pages(
|
|
88
|
+
self,
|
|
89
|
+
query: str = "",
|
|
90
|
+
*,
|
|
91
|
+
sort: Optional[Dict] = None,
|
|
92
|
+
start_cursor: Optional[str] = None,
|
|
93
|
+
page_size: int = 100,
|
|
94
|
+
) -> Dict:
|
|
95
|
+
"""Search only pages.
|
|
96
|
+
|
|
97
|
+
Convenience wrapper around :meth:`search` with the page filter
|
|
98
|
+
pre-applied.
|
|
99
|
+
|
|
100
|
+
Args:
|
|
101
|
+
query: Text to search for.
|
|
102
|
+
sort: Sort criteria dict.
|
|
103
|
+
start_cursor: Cursor for pagination.
|
|
104
|
+
page_size: Number of results per page.
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
Notion list object containing only page objects.
|
|
108
|
+
"""
|
|
109
|
+
return self.search(
|
|
110
|
+
query,
|
|
111
|
+
filter={"value": "page", "property": "object"},
|
|
112
|
+
sort=sort,
|
|
113
|
+
start_cursor=start_cursor,
|
|
114
|
+
page_size=page_size,
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
def search_all(
|
|
118
|
+
self,
|
|
119
|
+
query: str = "",
|
|
120
|
+
*,
|
|
121
|
+
filter: Optional[Dict] = None,
|
|
122
|
+
sort: Optional[Dict] = None,
|
|
123
|
+
) -> List[Dict]:
|
|
124
|
+
"""Search and automatically paginate to return **all** matching results.
|
|
125
|
+
|
|
126
|
+
Args:
|
|
127
|
+
query: Text to search for.
|
|
128
|
+
filter: Object type filter.
|
|
129
|
+
sort: Sort criteria dict.
|
|
130
|
+
|
|
131
|
+
Returns:
|
|
132
|
+
A flat list of all matching Notion objects.
|
|
133
|
+
"""
|
|
134
|
+
results: List[Dict] = []
|
|
135
|
+
cursor: Optional[str] = None
|
|
136
|
+
while True:
|
|
137
|
+
response = self.search(query, filter=filter, sort=sort, start_cursor=cursor)
|
|
138
|
+
results.extend(response.get("results", []))
|
|
139
|
+
if not response.get("has_more"):
|
|
140
|
+
break
|
|
141
|
+
cursor = response.get("next_cursor")
|
|
142
|
+
return results
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Notion Users API – https://developers.notion.com/reference/user
|
|
3
|
+
"""
|
|
4
|
+
from typing import Any, Dict, List, Optional
|
|
5
|
+
|
|
6
|
+
from notion_database.http.client import HttpClient
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class UsersAPI:
|
|
10
|
+
"""1-to-1 mapping of the Notion Users REST endpoints.
|
|
11
|
+
|
|
12
|
+
Reference: https://developers.notion.com/reference/user
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
def __init__(self, http: HttpClient) -> None:
|
|
16
|
+
self._http = http
|
|
17
|
+
|
|
18
|
+
def retrieve(self, user_id: str) -> Dict:
|
|
19
|
+
"""Retrieve a user by ID.
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
user_id: The ID of the user to retrieve.
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
Notion user object.
|
|
26
|
+
|
|
27
|
+
Reference: https://developers.notion.com/reference/get-user
|
|
28
|
+
"""
|
|
29
|
+
return self._http.get(f"/users/{user_id}")
|
|
30
|
+
|
|
31
|
+
def list(
|
|
32
|
+
self,
|
|
33
|
+
*,
|
|
34
|
+
start_cursor: Optional[str] = None,
|
|
35
|
+
page_size: int = 100,
|
|
36
|
+
) -> Dict:
|
|
37
|
+
"""List all users in the workspace.
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
start_cursor: Cursor for pagination.
|
|
41
|
+
page_size: Number of results per page (1-100).
|
|
42
|
+
|
|
43
|
+
Returns:
|
|
44
|
+
Notion list object with ``results``, ``has_more``, and
|
|
45
|
+
``next_cursor`` fields.
|
|
46
|
+
|
|
47
|
+
Reference: https://developers.notion.com/reference/get-users
|
|
48
|
+
"""
|
|
49
|
+
params: Dict[str, Any] = {"page_size": page_size}
|
|
50
|
+
if start_cursor is not None:
|
|
51
|
+
params["start_cursor"] = start_cursor
|
|
52
|
+
return self._http.get("/users", params=params)
|
|
53
|
+
|
|
54
|
+
def list_all(self) -> List[Dict]:
|
|
55
|
+
"""List **all** users in the workspace, automatically paginating.
|
|
56
|
+
|
|
57
|
+
Returns:
|
|
58
|
+
A flat list of all Notion user objects.
|
|
59
|
+
"""
|
|
60
|
+
users: List[Dict] = []
|
|
61
|
+
cursor: Optional[str] = None
|
|
62
|
+
while True:
|
|
63
|
+
response = self.list(start_cursor=cursor)
|
|
64
|
+
users.extend(response.get("results", []))
|
|
65
|
+
if not response.get("has_more"):
|
|
66
|
+
break
|
|
67
|
+
cursor = response.get("next_cursor")
|
|
68
|
+
return users
|
|
69
|
+
|
|
70
|
+
def me(self) -> Dict:
|
|
71
|
+
"""Retrieve the bot user associated with the current integration token.
|
|
72
|
+
|
|
73
|
+
Returns:
|
|
74
|
+
Notion bot user object.
|
|
75
|
+
|
|
76
|
+
Reference: https://developers.notion.com/reference/get-self
|
|
77
|
+
"""
|
|
78
|
+
return self._http.get("/users/me")
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""
|
|
2
|
+
NotionClient – single entry point for the notion-database package.
|
|
3
|
+
"""
|
|
4
|
+
from notion_database.api.blocks import BlocksAPI
|
|
5
|
+
from notion_database.api.comments import CommentsAPI
|
|
6
|
+
from notion_database.api.databases import DatabasesAPI
|
|
7
|
+
from notion_database.api.pages import PagesAPI
|
|
8
|
+
from notion_database.api.search import SearchAPI
|
|
9
|
+
from notion_database.api.users import UsersAPI
|
|
10
|
+
from notion_database.http.client import HttpClient
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class NotionClient:
|
|
14
|
+
"""Notion API client.
|
|
15
|
+
|
|
16
|
+
Provides access to all Notion REST API resources through namespaced
|
|
17
|
+
sub-clients. Each sub-client exposes methods that map 1-to-1 to the
|
|
18
|
+
corresponding Notion API endpoints.
|
|
19
|
+
|
|
20
|
+
Args:
|
|
21
|
+
token: Notion Internal Integration token (``secret_...``).
|
|
22
|
+
|
|
23
|
+
Example::
|
|
24
|
+
|
|
25
|
+
from notion_database import NotionClient, PropertyValue, BlockContent
|
|
26
|
+
|
|
27
|
+
client = NotionClient("secret_xxx")
|
|
28
|
+
|
|
29
|
+
# Retrieve a database
|
|
30
|
+
db = client.databases.retrieve("database-id")
|
|
31
|
+
|
|
32
|
+
# Query with filter and sort
|
|
33
|
+
from notion_database import Filter, Sort
|
|
34
|
+
results = client.databases.query(
|
|
35
|
+
"database-id",
|
|
36
|
+
filter=Filter.select("Status").equals("Active"),
|
|
37
|
+
sorts=[Sort.by_property("Name")],
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
# Create a page
|
|
41
|
+
page = client.pages.create(
|
|
42
|
+
parent={"database_id": "database-id"},
|
|
43
|
+
properties={
|
|
44
|
+
"Name": PropertyValue.title("Hello, Notion 2.0!"),
|
|
45
|
+
"Status": PropertyValue.select("Active"),
|
|
46
|
+
},
|
|
47
|
+
children=[
|
|
48
|
+
BlockContent.heading_1("Introduction"),
|
|
49
|
+
BlockContent.paragraph("This page was created via notion-database 2.0."),
|
|
50
|
+
],
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
# Append blocks to a page
|
|
54
|
+
client.blocks.append_children(
|
|
55
|
+
page["id"],
|
|
56
|
+
children=[BlockContent.divider(), BlockContent.paragraph("The end.")],
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
Attributes:
|
|
60
|
+
databases: :class:`~notion_database.api.databases.DatabasesAPI` –
|
|
61
|
+
retrieve, query, create, update databases.
|
|
62
|
+
pages: :class:`~notion_database.api.pages.PagesAPI` –
|
|
63
|
+
retrieve, create, update, archive pages.
|
|
64
|
+
blocks: :class:`~notion_database.api.blocks.BlocksAPI` –
|
|
65
|
+
retrieve, append, update, delete blocks.
|
|
66
|
+
search: :class:`~notion_database.api.search.SearchAPI` –
|
|
67
|
+
search pages and databases.
|
|
68
|
+
users: :class:`~notion_database.api.users.UsersAPI` –
|
|
69
|
+
retrieve and list workspace users.
|
|
70
|
+
comments: :class:`~notion_database.api.comments.CommentsAPI` –
|
|
71
|
+
retrieve and create comments.
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
def __init__(self, token: str) -> None:
|
|
75
|
+
http = HttpClient(token)
|
|
76
|
+
self.databases: DatabasesAPI = DatabasesAPI(http)
|
|
77
|
+
self.pages: PagesAPI = PagesAPI(http)
|
|
78
|
+
self.blocks: BlocksAPI = BlocksAPI(http)
|
|
79
|
+
self.search: SearchAPI = SearchAPI(http)
|
|
80
|
+
self.users: UsersAPI = UsersAPI(http)
|
|
81
|
+
self.comments: CommentsAPI = CommentsAPI(http)
|
|
82
|
+
|
|
83
|
+
def __repr__(self) -> str:
|
|
84
|
+
return "NotionClient()"
|
|
@@ -1 +1,46 @@
|
|
|
1
|
-
|
|
1
|
+
"""
|
|
2
|
+
Notion color constants.
|
|
3
|
+
"""
|
|
4
|
+
from notion_database.const.color import (
|
|
5
|
+
BLUE,
|
|
6
|
+
BLUE_BACKGROUND,
|
|
7
|
+
BROWN,
|
|
8
|
+
BROWN_BACKGROUND,
|
|
9
|
+
DEFAULT,
|
|
10
|
+
GRAY,
|
|
11
|
+
GRAY_BACKGROUND,
|
|
12
|
+
GREEN,
|
|
13
|
+
GREEN_BACKGROUND,
|
|
14
|
+
ORANGE,
|
|
15
|
+
ORANGE_BACKGROUND,
|
|
16
|
+
PINK,
|
|
17
|
+
PINK_BACKGROUND,
|
|
18
|
+
PURPLE,
|
|
19
|
+
PURPLE_BACKGROUND,
|
|
20
|
+
RED,
|
|
21
|
+
RED_BACKGROUND,
|
|
22
|
+
YELLOW,
|
|
23
|
+
YELLOW_BACKGROUND,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
__all__ = [
|
|
27
|
+
"DEFAULT",
|
|
28
|
+
"GRAY",
|
|
29
|
+
"BROWN",
|
|
30
|
+
"ORANGE",
|
|
31
|
+
"YELLOW",
|
|
32
|
+
"GREEN",
|
|
33
|
+
"BLUE",
|
|
34
|
+
"PURPLE",
|
|
35
|
+
"PINK",
|
|
36
|
+
"RED",
|
|
37
|
+
"GRAY_BACKGROUND",
|
|
38
|
+
"BROWN_BACKGROUND",
|
|
39
|
+
"ORANGE_BACKGROUND",
|
|
40
|
+
"YELLOW_BACKGROUND",
|
|
41
|
+
"GREEN_BACKGROUND",
|
|
42
|
+
"BLUE_BACKGROUND",
|
|
43
|
+
"PURPLE_BACKGROUND",
|
|
44
|
+
"PINK_BACKGROUND",
|
|
45
|
+
"RED_BACKGROUND",
|
|
46
|
+
]
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Notion API exceptions.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class NotionError(Exception):
|
|
7
|
+
"""Base exception for all notion-database errors."""
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class NotionAPIError(NotionError):
|
|
11
|
+
"""Raised when the Notion API returns an error response.
|
|
12
|
+
|
|
13
|
+
Attributes:
|
|
14
|
+
status_code: HTTP status code returned by the API.
|
|
15
|
+
code: Notion error code string (e.g. "validation_error").
|
|
16
|
+
message: Human-readable error message from the API.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
def __init__(self, status_code: int, code: str, message: str) -> None:
|
|
20
|
+
self.status_code = status_code
|
|
21
|
+
self.code = code
|
|
22
|
+
self.message = message
|
|
23
|
+
super().__init__(f"[{status_code}] {code}: {message}")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class NotionValidationError(NotionAPIError):
|
|
27
|
+
"""HTTP 400 – The request body is malformed or missing required fields."""
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class NotionUnauthorizedError(NotionAPIError):
|
|
31
|
+
"""HTTP 401 – The integration token is invalid or missing."""
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class NotionForbiddenError(NotionAPIError):
|
|
35
|
+
"""HTTP 403 – The integration lacks permission for the requested resource."""
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class NotionNotFoundError(NotionAPIError):
|
|
39
|
+
"""HTTP 404 – The requested resource does not exist."""
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class NotionConflictError(NotionAPIError):
|
|
43
|
+
"""HTTP 409 – A conflict occurred (e.g. duplicate transaction ID)."""
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class NotionRateLimitError(NotionAPIError):
|
|
47
|
+
"""HTTP 429 – The request has been rate limited."""
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class NotionInternalError(NotionAPIError):
|
|
51
|
+
"""HTTP 5xx – An internal error occurred on Notion's servers."""
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""
|
|
2
|
+
HTTP client for the Notion API.
|
|
3
|
+
"""
|
|
4
|
+
import json
|
|
5
|
+
from typing import Any, Dict, Optional
|
|
6
|
+
|
|
7
|
+
import requests
|
|
8
|
+
|
|
9
|
+
from notion_database.exceptions import (
|
|
10
|
+
NotionAPIError,
|
|
11
|
+
NotionConflictError,
|
|
12
|
+
NotionForbiddenError,
|
|
13
|
+
NotionInternalError,
|
|
14
|
+
NotionNotFoundError,
|
|
15
|
+
NotionRateLimitError,
|
|
16
|
+
NotionUnauthorizedError,
|
|
17
|
+
NotionValidationError,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
_NOTION_VERSION = "2022-06-28"
|
|
21
|
+
_BASE_URL = "https://api.notion.com/v1"
|
|
22
|
+
|
|
23
|
+
_STATUS_TO_EXCEPTION = {
|
|
24
|
+
400: NotionValidationError,
|
|
25
|
+
401: NotionUnauthorizedError,
|
|
26
|
+
403: NotionForbiddenError,
|
|
27
|
+
404: NotionNotFoundError,
|
|
28
|
+
409: NotionConflictError,
|
|
29
|
+
429: NotionRateLimitError,
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class HttpClient:
|
|
34
|
+
"""Thin HTTP wrapper around the Notion REST API.
|
|
35
|
+
|
|
36
|
+
All methods return the parsed JSON response as a plain ``dict``. On
|
|
37
|
+
non-2xx responses a :class:`~notion_database.exceptions.NotionAPIError`
|
|
38
|
+
subclass is raised with the Notion ``code`` and ``message`` fields
|
|
39
|
+
populated from the response body.
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
def __init__(self, token: str) -> None:
|
|
43
|
+
self._headers: Dict[str, str] = {
|
|
44
|
+
"Authorization": f"Bearer {token}",
|
|
45
|
+
"Content-Type": "application/json",
|
|
46
|
+
"Notion-Version": _NOTION_VERSION,
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
# ------------------------------------------------------------------
|
|
50
|
+
# Public HTTP verbs
|
|
51
|
+
# ------------------------------------------------------------------
|
|
52
|
+
|
|
53
|
+
def get(self, path: str, params: Optional[Dict[str, Any]] = None) -> Dict:
|
|
54
|
+
response = requests.get(
|
|
55
|
+
f"{_BASE_URL}{path}",
|
|
56
|
+
headers=self._headers,
|
|
57
|
+
params=params,
|
|
58
|
+
timeout=60,
|
|
59
|
+
)
|
|
60
|
+
return self._parse(response)
|
|
61
|
+
|
|
62
|
+
def post(self, path: str, body: Optional[Dict] = None) -> Dict:
|
|
63
|
+
response = requests.post(
|
|
64
|
+
f"{_BASE_URL}{path}",
|
|
65
|
+
headers=self._headers,
|
|
66
|
+
data=json.dumps(body or {}),
|
|
67
|
+
timeout=60,
|
|
68
|
+
)
|
|
69
|
+
return self._parse(response)
|
|
70
|
+
|
|
71
|
+
def patch(self, path: str, body: Optional[Dict] = None) -> Dict:
|
|
72
|
+
response = requests.patch(
|
|
73
|
+
f"{_BASE_URL}{path}",
|
|
74
|
+
headers=self._headers,
|
|
75
|
+
data=json.dumps(body or {}),
|
|
76
|
+
timeout=60,
|
|
77
|
+
)
|
|
78
|
+
return self._parse(response)
|
|
79
|
+
|
|
80
|
+
def delete(self, path: str) -> Dict:
|
|
81
|
+
response = requests.delete(
|
|
82
|
+
f"{_BASE_URL}{path}",
|
|
83
|
+
headers=self._headers,
|
|
84
|
+
timeout=60,
|
|
85
|
+
)
|
|
86
|
+
return self._parse(response)
|
|
87
|
+
|
|
88
|
+
# ------------------------------------------------------------------
|
|
89
|
+
# Internal helpers
|
|
90
|
+
# ------------------------------------------------------------------
|
|
91
|
+
|
|
92
|
+
@staticmethod
|
|
93
|
+
def _parse(response: requests.Response) -> Dict:
|
|
94
|
+
if response.ok:
|
|
95
|
+
return response.json()
|
|
96
|
+
|
|
97
|
+
exc_class = _STATUS_TO_EXCEPTION.get(response.status_code, NotionAPIError)
|
|
98
|
+
if response.status_code >= 500:
|
|
99
|
+
exc_class = NotionInternalError
|
|
100
|
+
|
|
101
|
+
try:
|
|
102
|
+
body = response.json()
|
|
103
|
+
code = body.get("code", "unknown_error")
|
|
104
|
+
message = body.get("message", response.text)
|
|
105
|
+
except Exception:
|
|
106
|
+
code = "unknown_error"
|
|
107
|
+
message = response.text
|
|
108
|
+
|
|
109
|
+
raise exc_class(response.status_code, code, message)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Builder helpers for Notion API request bodies.
|
|
3
|
+
"""
|
|
4
|
+
from notion_database.models.blocks import BlockContent
|
|
5
|
+
from notion_database.models.filters import Filter
|
|
6
|
+
from notion_database.models.icons import Cover, Icon
|
|
7
|
+
from notion_database.models.properties import PropertySchema, PropertyValue
|
|
8
|
+
from notion_database.models.rich_text import RichText
|
|
9
|
+
from notion_database.models.sorts import Sort
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"BlockContent",
|
|
13
|
+
"Filter",
|
|
14
|
+
"Cover",
|
|
15
|
+
"Icon",
|
|
16
|
+
"PropertySchema",
|
|
17
|
+
"PropertyValue",
|
|
18
|
+
"RichText",
|
|
19
|
+
"Sort",
|
|
20
|
+
]
|