|
| 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 | + ``` |
0 commit comments