notion_tools/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//! # Notion Tools
//!
//! `notion-tools` is a library for interacting with the Notion API. It provides a convenient way to
//! perform various operations such as retrieving databases, querying databases, creating pages,
//! updating pages, archiving pages, and appending block children.
//!
//! ## Usage
//!
//! To use this library, you need to set the `NOTION_API_KEY` and `NOTION_DATABASE_ID` environment
//! variables. The `NOTION_API_KEY` is required for authentication, while the `NOTION_DATABASE_ID`
//! is optional and can be set later using the `database` method.
//!
//! ## Implemented endpoints
//! | Endpoint | Implemented | Code |
//! |---|:---:|---|
//! | [Create a Token](https://developers.notion.com/reference/create-a-token) | - | |
//! | [Append block children](https://developers.notion.com/reference/patch-block-children) | ✅ | [`Notion::append_block_children`](Notion) |
//! | [Retrieve a block](https://developers.notion.com/reference/retrieve-a-block) | - | |
//! | [Retrieve block children](https://developers.notion.com/reference/get-block-children) | - | |
//! | [Update a block](https://developers.notion.com/reference/update-a-block) | - | |
//! | [Delete a block](https://developers.notion.com/reference/delete-a-block) | - | |
//! | [Create a page](https://developers.notion.com/reference/post-page) | ✅ | [`Notion::create_a_page`](Notion) |
//! | [Retrieve a page](https://developers.notion.com/reference/retrieve-a-page) | - | |
//! | [Retrieve a page property item](https://developers.notion.com/reference/retrieve-a-page-property-item) | - | |
//! | [Update page properties](https://developers.notion.com/reference/patch-page) | ✅ | [`Notion::update_a_page`](Notion) |
//! | [Archive a page](https://developers.notion.com/reference/archive-a-page) | ✅ | [`Notion::archive_a_page`](Notion) |
//! | [Create a database](https://developers.notion.com/reference/create-a-database) | - | |
//! | [Query a database](https://developers.notion.com/reference/post-database-query) | ✅ | [`Notion::query_database`](Notion) |
//! | [Retrieve a database](https://developers.notion.com/reference/retrieve-a-database) | ✅ | [`Notion::retrieve_a_database`](Notion) |
//! | [Update a database](https://developers.notion.com/reference/update-a-database) | - | |
//! | [List all users](https://developers.notion.com/reference/get-users) | - | |
//! | [Retrieve a user](https://developers.notion.com/reference/get-user) | - | |
//! | [Retrieve your token's bot user](https://developers.notion.com/reference/get-self) | - | |
//! | [Create comment](https://developers.notion.com/reference/create-a-comment) | - | |
//! | [Retrieve comments](https://developers.notion.com/reference/retrieve-a-comment) | - | |
//! | [Search by title](https://developers.notion.com/reference/post-search) | - | |
//!
//! ## Build a query filter
//! The `QueryFilter` struct is used to build a query filter for querying a database. The `QueryFilter`
//! struct provides methods for building a filter that can be used to query a database.
//! See the [`QueryFilter`] struct for more information.
//!
//! ## Examples
//!
//! ### Create a page
//!
//! ```rust
//! # use anyhow::Result;
//! # use notion_tools::Notion;
//! # use notion_tools::structs::page::*;
//! # use notion_tools::structs::common::*;
//! # use fxhash::FxHashMap;
//!
//! # #[tokio::main]
//! # async fn main() -> Result<()> {
//! let notion = Notion::new();
//!
//! // Create a page
//! let mut properties: FxHashMap<String, PageProperty> = FxHashMap::default();
//! properties.insert(
//!     "Name".to_string(),
//!     PageProperty::title(RichText::from_str("Sample Page")),
//! );
//! properties.insert(
//!     "Title".to_string(),
//!     PageProperty::rich_text(vec![RichText::from_str("Sample Page")]),
//! );
//! properties.insert("Status".to_string(), PageProperty::status("ToDo"));
//! let mut page = Page::from_properties(properties);
//! page.parent.type_name = ParentType::Database;
//! page.parent.database_id = Some(notion.database_id.clone());
//!
//! let response = notion.create_a_page(&page).await;
//! println!("{:?}", response);
//! # Ok(())
//! # }
//! ```
//!
//! ### Query a database
//!
//! ```rust
//! # use anyhow::Result;
//! # use notion_tools::Notion;
//! # use notion_tools::structs::query_filter::*;
//! # use notion_tools::structs::page::*;
//! # use notion_tools::structs::common::*;
//! #
//! # #[tokio::main]
//! # async fn main() -> Result<()> {
//! let mut notion = Notion::new();
//! notion.database("your_database_id");
//!
//! // Build a query filter
//! let mut filter = QueryFilter::new();
//! filter.args(FilterItem::status(
//!     "Status",
//!     StatusFilterItem::equals("ToDo"),
//! ));
//! // Query a database
//! let response = notion.query_database(filter).await?;
//! println!("{:?}", response);
//! #     Ok(())
//! # }
//! ```
//!
pub mod structs;

use crate::structs::block::*;
use crate::structs::common::*;
use crate::structs::database::*;
use crate::structs::page::*;
use crate::structs::query_filter::*;
use anyhow::Result;
use dotenvy::dotenv;
use reqwest as request;

/// Notion API client
#[derive(Debug)]
pub struct Notion {
    /// Notion API key: set from the `NOTION_API_KEY` environment variable
    pub api_key: String,
    /// Notion database ID: set from the `NOTION_DATABASE_ID` environment variable
    pub database_id: String,
}

impl Notion {
    /// Create a new Notion API client.  
    /// environment variables are read from the `.env` file.
    pub fn new() -> Self {
        dotenv().ok();
        let api_key = std::env::var("NOTION_API_KEY").expect("NOTION_API_KEY must be set");
        let database_id = std::env::var("NOTION_DATABASE_ID").unwrap_or("".to_string());

        Notion {
            api_key,
            database_id,
        }
    }

    /// Set your database ID
    pub fn database(&mut self, database_id: &str) -> &mut Self {
        self.database_id = database_id.to_string();
        return self;
    }

    /// # Retrieve a database properties  
    /// ## Return
    /// - [`Database`] struct
    pub async fn retrieve_a_database(&self) -> Result<Database> {
        let url = format!("https://api.notion.com/v1/databases/{}", self.database_id);
        let client = request::Client::new();
        let content = client
            .get(&url)
            .header("Content-Type", "application/json")
            .header("Authorization", format!("Bearer {}", self.api_key))
            .header("Notion-Version", "2022-06-28")
            .send()
            .await?
            .text()
            .await?;

        let mut database = serde_json::from_str::<Database>(&content)?;
        if database.status == 0 {
            database.status = 200;
        }

        return Ok(database);
    }

    /// # Query a database  
    /// ## Arguments:  
    /// - filter: [`QueryFilter`]
    /// ## Return:  
    /// - [`PageResponse`] struct
    pub async fn query_database(&self, filter: QueryFilter) -> Result<PageResponse> {
        let url = format!(
            "https://api.notion.com/v1/databases/{}/query",
            self.database_id
        );
        let query = filter.build();
        let client = request::Client::new();
        let content = client
            .post(&url)
            .header("Content-Type", "application/json")
            .header("Authorization", format!("Bearer {}", self.api_key))
            .header("Notion-Version", "2022-06-28")
            .body(query)
            .send()
            .await?
            .text()
            .await?;

        let mut response = serde_json::from_str::<PageResponse>(&content)?;
        if response.status == 0 {
            response.status = 200;
        }

        return Ok(response);
    }

    /// # Create a page
    /// ## Arguments:
    /// - page: [`Page`] struct
    /// ## Return:
    /// - [`Page`] struct
    pub async fn create_a_page(&self, page: &Page) -> Result<Page> {
        let url = "https://api.notion.com/v1/pages";
        let client = request::Client::new();
        let data = serde_json::to_string(page)?;
        let content = client
            .post(url)
            .header("Content-Type", "application/json")
            .header("Authorization", format!("Bearer {}", self.api_key))
            .header("Notion-Version", "2022-06-28")
            .body(data)
            .send()
            .await?
            .text()
            .await?;

        let mut page = serde_json::from_str::<Page>(&content)?;
        if page.status == 0 {
            page.status = 200;
        }
        return Ok(page);
    }

    /// # Update a page
    /// ## Arguments:
    /// - page_id: &str
    /// - page: [`Page`] struct
    /// ## Return:
    /// - [`Page`] struct
    pub async fn update_a_page(&self, page_id: &str, page: &Page) -> Result<Page> {
        let url = format!("https://api.notion.com/v1/pages/{}", page_id);
        let client = request::Client::new();
        let data = serde_json::to_string(page)?;
        let content = client
            .patch(&url)
            .header("Content-Type", "application/json")
            .header("Authorization", format!("Bearer {}", self.api_key))
            .header("Notion-Version", "2022-06-28")
            .body(data)
            .send()
            .await?
            .text()
            .await?;

        let mut page = serde_json::from_str::<Page>(&content)?;
        if page.status == 0 {
            page.status = 200;
        }
        return Ok(page);
    }

    /// # Archive a page
    /// ## Arguments:
    /// - page_id: &str
    /// - parent_id: &str
    /// - parent_type: [`ParentType`]
    /// ## Return:
    /// - [`Page`] struct
    /// ## Note:
    /// - The page will be archived by updating the page with the `archived` field set to `true`.
    pub async fn archive_a_page(
        &self,
        page_id: &str,
        parent_id: &str,
        parent_type: ParentType,
    ) -> Result<Page> {
        let mut page = Page {
            archived: true,
            ..Default::default()
        };

        match parent_type {
            ParentType::Database => {
                page.parent.type_name = parent_type;
                page.parent.database_id = Some(parent_id.to_string());
            }
            ParentType::Page => {
                page.parent.type_name = parent_type;
                page.parent.page_id = Some(parent_id.to_string());
            }
            ParentType::Workspace => {
                page.parent.type_name = parent_type;
                page.parent.workspace_id = Some(parent_id.to_string());
            }
            ParentType::Block => {
                page.parent.type_name = parent_type;
                page.parent.block_id = Some(parent_id.to_string());
            }
        }
        let page = self.update_a_page(page_id, &page).await?;
        return Ok(page);
    }

    /// # Append block children
    /// Because the Notion API only allows appending 100 blocks at a time, this method will split the
    /// blocks into chunks of 100 and append them to the parent block.
    /// ## Arguments:
    /// - parent_id: &str
    /// - blocks: [`BlockBody`]
    /// ## Return:
    /// - [`BlockResponse`] struct
    pub async fn append_block_children(
        &self,
        parent_id: &str,
        blocks: Vec<Block>,
    ) -> Result<BlockResponse> {
        let url = format!("https://api.notion.com/v1/blocks/{}/children", parent_id);
        let client = request::Client::new();
        let mut res_blocks: Vec<Block> = Vec::new();

        for i in (0..blocks.len()).step_by(100) {
            let end_index = std::cmp::min(i + 100, blocks.len());
            let block_body = BlockBody {
                children: blocks[i..end_index].to_vec(),
            };
            let data = serde_json::to_string(&block_body)?;
            let content = client
                .patch(&url)
                .header("Content-Type", "application/json")
                .header("Authorization", format!("Bearer {}", self.api_key))
                .header("Notion-Version", "2022-06-28")
                .body(data)
                .send()
                .await?
                .text()
                .await?;
            let _bby = serde_json::from_str::<BlockResponse>(&content)?;
            res_blocks.extend(_bby.results);
        }

        let res_block = BlockResponse {
            object: "list".to_string(),
            results: res_blocks,
            status: 200,
            ..Default::default()
        };
        return Ok(res_block);
    }
}

#[cfg(test)]
mod tests;