Skip to content

Commit 1005c1c

Browse files
Update Notion API version (#30)
* Adding API for data sources and updating databases for new Notion API version * Updating docs
1 parent f0bfbd8 commit 1005c1c

25 files changed

Lines changed: 818 additions & 489 deletions

conftest.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,24 @@ def cover_url() -> str:
2222

2323
@fixture
2424
def example_page_id() -> str:
25-
return "e439b7a7296d45b98805f24c9cfc2115"
25+
return "2cef2075b1dc803a8ec3d142ac105817"
26+
27+
28+
@fixture(scope="session")
29+
def database_id() -> str:
30+
return "2cef2075b1dc8023b0ece0dbf9b278f7"
31+
32+
33+
@fixture(scope="session")
34+
def data_source_id1() -> str:
35+
return "2cef2075b1dc80bab834000b42b068d7"
36+
37+
38+
@fixture
39+
def data_source_id2() -> str:
40+
return "2cef2075b1dc80048d8d000b1b75df8f"
2641

2742

2843
@fixture
29-
def example_page_id_2() -> str:
30-
return "d5bce0a0fe6248d0a120c6c693d9b597"
44+
def block_id() -> str:
45+
return "2cef2075b1dc80a59d6ad9ed97846ff8"

docs/get_started/data_sources.md

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
## Retrieve a data source
2+
3+
=== "Async"
4+
5+
```python
6+
async def main():
7+
async_api = AsyncNotionAPI(access_token='<NOTION_TOKEN>')
8+
data_source = await async_api.get_data_source(data_source_id='<DATA_SOURCE_ID>')
9+
```
10+
11+
=== "Sync"
12+
13+
```python
14+
api = NotionAPI(access_token='<NOTION_TOKEN>')
15+
data_source = api.get_data_source(data_source_id='<DATA_SOURCE_ID>')
16+
```
17+
18+
## Query
19+
20+
=== "Async"
21+
22+
```python
23+
async def main():
24+
async_api = AsyncNotionAPI(access_token='<NOTION_TOKEN>')
25+
data_source = await async_api.get_data_source(data_source_id='<DATA_SOURCE_ID>')
26+
27+
async for page in data_source.query():
28+
...
29+
```
30+
31+
=== "Sync"
32+
33+
```python
34+
api = NotionAPI(access_token='<NOTION_TOKEN>')
35+
data_source = api.get_data_source(data_source_id='<DATA_SOURCE_ID>')
36+
37+
for page in data_source.query():
38+
...
39+
```
40+
41+
### Filters
42+
43+
You can use filter classes in `python_notion_api.models.filters` to create property filters and pass them to the query.
44+
45+
=== "Async"
46+
47+
```python
48+
from python_notion_api.models.filters import SelectFilter
49+
50+
async def main():
51+
async_api = AsyncNotionAPI(access_token='<NOTION_TOKEN>')
52+
data_source = await async_api.get_data_source(data_source_id='<DATA_SOURCE_ID>')
53+
54+
await for page in data_source.query(
55+
filters=SelectFilter(property='<PROPERTY_NAME / PROPERTY_ID>', equals='<VALUE>')
56+
):
57+
...
58+
```
59+
60+
=== "Sync"
61+
62+
```python
63+
from python_notion_api.models.filters import SelectFilter
64+
65+
api = NotionAPI(access_token='<NOTION_TOKEN>')
66+
data_source = api.get_data_source(data_source_id='<DATA_SOURCE_ID>')
67+
68+
for page in data_source.query(
69+
filters=SelectFilter(property='<PROPERTY_NAME / PROPERTY_ID>', equals='<VALUE>')
70+
):
71+
...
72+
```
73+
74+
'and' and 'or' filters are supported:
75+
76+
=== "Async"
77+
78+
```python
79+
from python_notion_api.models.filters import SelectFilter, or_filter, and_filter
80+
81+
async def main():
82+
async_api = AsyncNotionAPI(access_token='<NOTION_TOKEN>')
83+
data_source = await async_api.get_data_source(data_source_id='<DATA_SOURCE_ID>')
84+
85+
await for page in data_source.query(
86+
filters=or_filter([
87+
SelectFilter(property="Select", equals="xxx"),
88+
and_filter([
89+
NumberFilter(property="Number", greater_than=10),
90+
CheckboxFilter(property="Checkbox", equals=True)
91+
])
92+
])
93+
):
94+
...
95+
```
96+
97+
=== "Sync"
98+
99+
```python
100+
from python_notion_api.models.filters import SelectFilter, or_filter, and_filter
101+
102+
api = NotionAPI(access_token='<NOTION_TOKEN>')
103+
data_source = api.get_data_source(data_source_id='<DATA_SOURCE_ID>')
104+
105+
for page in data_source.query(
106+
filters=or_filter([
107+
SelectFilter(property="Select", equals="xxx"),
108+
and_filter([
109+
NumberFilter(property="Number", greater_than=10),
110+
CheckboxFilter(property="Checkbox", equals=True)
111+
])
112+
])
113+
)
114+
...
115+
```
116+
117+
You can read more on filters [here](https://developers.notion.com/reference/post-database-query-filter){:target="_blank"}
118+
119+
### Sorts
120+
121+
You can use `python_notion_api.models.sorts.Sort` class to create sorts and pass them to the query.
122+
123+
=== "Async"
124+
125+
```python
126+
from python_notion_api.models.sorts import Sort
127+
128+
async def main():
129+
async_api = AsyncNotionAPI(access_token='<NOTION_TOKEN>')
130+
data_source = await async_api.get_data_source(data_source_id='<DATA_SOURCE_ID>')
131+
132+
await for page in data_source.query(
133+
sorts=[
134+
Sort(property="Title"),
135+
Sort(property="Date", descending=True)
136+
]
137+
):
138+
```
139+
140+
=== "Sync"
141+
142+
```python
143+
from python_notion_api.models.sorts import Sort
144+
145+
api = NotionAPI(access_token='<NOTION_TOKEN>')
146+
data_source = api.get_data_source(data_source_id='<DATA_SOURCE_ID>')
147+
148+
for page in data_source.query(
149+
sorts=[
150+
Sort(property="Title"),
151+
Sort(property="Date", descending=True)
152+
]
153+
)
154+
```

docs/get_started/databases.md

Lines changed: 0 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -14,141 +14,3 @@
1414
api = NotionAPI(access_token='<NOTION_TOKEN>')
1515
database = api.get_database(database_id='<DATABASE_ID>')
1616
```
17-
18-
## Query
19-
20-
=== "Async"
21-
22-
```python
23-
async def main():
24-
async_api = AsyncNotionAPI(access_token='<NOTION_TOKEN>')
25-
database = await async_api.get_database(database_id='<DATABASE_ID>')
26-
27-
async for page in database.query():
28-
...
29-
```
30-
31-
=== "Sync"
32-
33-
```python
34-
api = NotionAPI(access_token='<NOTION_TOKEN>')
35-
database = api.get_database(database_id='<DATABASE_ID>')
36-
37-
for page in database.query():
38-
...
39-
```
40-
41-
### Filters
42-
43-
You can use filter classes in `python_notion_api.models.filters` to create property filters and pass them to the query.
44-
45-
=== "Async"
46-
47-
```python
48-
from python_notion_api.models.filters import SelectFilter
49-
50-
async def main():
51-
async_api = AsyncNotionAPI(access_token='<NOTION_TOKEN>')
52-
database = await async_api.get_database(database_id='<DATABASE_ID>')
53-
54-
await for page in database.query(
55-
filters=SelectFilter(property='<PROPERTY_NAME / PROPERTY_ID>', equals='<VALUE>')
56-
):
57-
...
58-
```
59-
60-
=== "Sync"
61-
62-
```python
63-
from python_notion_api.models.filters import SelectFilter
64-
65-
api = NotionAPI(access_token='<NOTION_TOKEN>')
66-
database = api.get_database(database_id='<DATABASE_ID>')
67-
68-
for page in database.query(
69-
filters=SelectFilter(property='<PROPERTY_NAME / PROPERTY_ID>', equals='<VALUE>')
70-
):
71-
...
72-
```
73-
74-
'and' and 'or' filters are supported:
75-
76-
=== "Async"
77-
78-
```python
79-
from python_notion_api.models.filters import SelectFilter, or_filter, and_filter
80-
81-
async def main():
82-
async_api = AsyncNotionAPI(access_token='<NOTION_TOKEN>')
83-
database = await async_api.get_database(database_id='<DATABASE_ID>')
84-
85-
await for page in database.query(
86-
filters=or_filter([
87-
SelectFilter(property="Select", equals="xxx"),
88-
and_filter([
89-
NumberFilter(property="Number", greater_than=10),
90-
CheckboxFilter(property="Checkbox", equals=True)
91-
])
92-
])
93-
):
94-
...
95-
```
96-
97-
=== "Sync"
98-
99-
```python
100-
from python_notion_api.models.filters import SelectFilter, or_filter, and_filter
101-
102-
api = NotionAPI(access_token='<NOTION_TOKEN>')
103-
database = api.get_database(database_id='<DATABASE_ID>')
104-
105-
for page in database.query(
106-
filters=or_filter([
107-
SelectFilter(property="Select", equals="xxx"),
108-
and_filter([
109-
NumberFilter(property="Number", greater_than=10),
110-
CheckboxFilter(property="Checkbox", equals=True)
111-
])
112-
])
113-
)
114-
...
115-
```
116-
117-
You can read more on filters [here](https://developers.notion.com/reference/post-database-query-filter){:target="_blank"}
118-
119-
### Sorts
120-
121-
You can use `python_notion_api.models.sorts.Sort` class to create sorts and pass them to the query.
122-
123-
=== "Async"
124-
125-
```python
126-
from python_notion_api.models.sorts import Sort
127-
128-
async def main():
129-
async_api = AsyncNotionAPI(access_token='<NOTION_TOKEN>')
130-
database = await async_api.get_database(database_id='<DATABASE_ID>')
131-
132-
await for page in database.query(
133-
sorts=[
134-
Sort(property="Title"),
135-
Sort(property="Date", descending=True)
136-
]
137-
):
138-
```
139-
140-
=== "Sync"
141-
142-
```python
143-
from python_notion_api.models.sorts import Sort
144-
145-
api = NotionAPI(access_token='<NOTION_TOKEN>')
146-
database = api.get_database(database_id='<DATABASE_ID>')
147-
148-
for page in database.query(
149-
sorts=[
150-
Sort(property="Title"),
151-
Sort(property="Date", descending=True)
152-
]
153-
)
154-
```

docs/get_started/pages.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
```python
2323
async def main():
2424
async_api = AsyncNotionAPI(access_token='<NOTION_TOKEN>')
25-
database = await async_api.get_database(database_id='<DATABASE_ID>')
25+
data_source = await async_api.get_data_source(data_source_id='<DATA_SOURCE_ID>')
2626

27-
await database.create_page(properties={
27+
await data_source.create_page(properties={
2828
'Number_property': 234,
2929
'Select_property': 'select1',
3030
'Checkbox_property': True,
@@ -36,9 +36,9 @@
3636

3737
```python
3838
api = NotionAPI(access_token='<NOTION_TOKEN>')
39-
database = api.get_database(database_id='<DATABASE_ID>')
39+
data_source = api.get_data_source(data_source_id='<DATA_SOURCE_ID>')
4040

41-
database.create_page(properties={
41+
data_source.create_page(properties={
4242
'Number_property': 234,
4343
'Select_property': 'select1',
4444
'Checkbox_property': True,
@@ -130,8 +130,8 @@ In particular, the values of rollups and formulas may be incorrect when retrieve
130130
To use custom page properties, create a subclass of NotionPage. Define a function to get each custom property (these must return a `PropertyValue`) and define the mapping from Notion property names to the function names.
131131

132132
```python
133-
from python_notion_api.api import NotionPage
134-
from python_notion_api.models import RichTextObject
133+
from python_notion_api.sync_api.api import NotionPage
134+
from python_notion_api.models import RichTextObject, RichTextPropertyItem
135135
from python_notion_api.models.values import RichTextPropertyValue
136136

137137
class MyPage(NotionPage):
@@ -159,14 +159,14 @@ class MyPage(NotionPage):
159159

160160
```
161161

162-
This page class can be passed when querying a database or getting a page.
162+
This page class can be passed when querying a data source or getting a page.
163163

164164
```python
165165
page = api.get_page(page_id='<PAGE_ID>',
166166
cast_cls=MyPage)
167167

168168

169-
for page in database.query(cast_cls=MyPage, filters=NumberFilter(property='Value', equals=1)):
169+
for page in data_source.query(cast_cls=MyPage, filters=NumberFilter(property='Value', equals=1)):
170170
print('Custom processing:', page.get('Value').value)
171171
print('Raw value:', page._direct_get('Value').value)
172172
```

python_notion_api/async_api/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
create_property_iterator,
77
)
88
from python_notion_api.async_api.notion_block import NotionBlock
9+
from python_notion_api.async_api.notion_data_source import NotionDataSource
910
from python_notion_api.async_api.notion_database import NotionDatabase
1011
from python_notion_api.async_api.notion_page import NotionPage
1112

@@ -14,6 +15,7 @@
1415
"NotionBlock",
1516
"NotionPage",
1617
"NotionDatabase",
18+
"NotionDataSource",
1719
"AsyncPropertyItemIterator",
1820
"AsyncRollupPropertyItemIterator",
1921
"AsyncBlockIterator",

0 commit comments

Comments
 (0)