notionrs/client/database/
create_database.rs

1use serde::{Deserialize, Serialize};
2
3use notionrs_types::object::rich_text::RichText;
4
5#[derive(Debug, Default, notionrs_macro::Setter)]
6pub struct CreateDatabaseClient {
7    /// The reqwest http client
8    pub(crate) reqwest_client: reqwest::Client,
9
10    pub(crate) page_id: Option<String>,
11
12    pub(crate) title: Vec<RichText>,
13
14    /// Field that can be added, though not documented in Notion's API documentation.
15    /// Can be used as a description for the database.
16    pub(crate) description: Vec<RichText>,
17
18    pub(crate) properties:
19        std::collections::HashMap<String, notionrs_types::object::data_source::DataSourceProperty>,
20
21    /// This can be configured even though it's not in the official Notion API documentation
22    pub(crate) icon: Option<notionrs_types::object::icon::Icon>,
23
24    /// This can be configured even though it's not in the official Notion API documentation
25    pub(crate) cover: Option<notionrs_types::object::file::File>,
26}
27
28#[derive(Debug, Serialize, Deserialize)]
29pub struct CreateDatabaseRequestBodyPropertyPart {
30    pub(crate) initial_data_source:
31        std::collections::HashMap<String, notionrs_types::object::data_source::DataSourceProperty>,
32}
33
34#[derive(Debug, Serialize, Deserialize)]
35pub struct CreateDatabaseRequestBody {
36    pub(crate) parent: notionrs_types::object::parent::PageParent,
37
38    #[serde(skip_serializing_if = "Vec::is_empty")]
39    pub(crate) title: Vec<RichText>,
40
41    /// Field that can be added, though not documented in Notion's API documentation.
42    /// Can be used as a description for the database.
43    #[serde(skip_serializing_if = "Vec::is_empty")]
44    pub(crate) description: Vec<RichText>,
45
46    pub(crate) properties: CreateDatabaseRequestBodyPropertyPart,
47
48    /// This can be configured even though it's not in the official Notion API documentation
49    #[serde(skip_serializing_if = "Option::is_none")]
50    pub(crate) icon: Option<notionrs_types::object::icon::Icon>,
51
52    /// This can be configured even though it's not in the official Notion API documentation
53    #[serde(skip_serializing_if = "Option::is_none")]
54    pub(crate) cover: Option<notionrs_types::object::file::File>,
55}
56
57impl CreateDatabaseClient {
58    pub async fn send(
59        self,
60    ) -> Result<notionrs_types::object::database::DatabaseResponse, crate::error::Error> {
61        let page_id = self.page_id.unwrap();
62
63        let request_body_struct = CreateDatabaseRequestBody {
64            parent: notionrs_types::object::parent::PageParent::from(page_id),
65            properties: CreateDatabaseRequestBodyPropertyPart {
66                initial_data_source: self.properties,
67            },
68            title: self.title,
69            description: self.description,
70            icon: self.icon,
71            cover: self.cover,
72        };
73
74        let request_body = serde_json::to_string(&request_body_struct)?;
75
76        let url = "https://api.notion.com/v1/databases".to_string();
77
78        let request = self
79            .reqwest_client
80            .post(url)
81            .header("Content-Type", "application/json")
82            .body(request_body);
83
84        let response = request
85            .send()
86            .await
87            .map_err(|e| crate::error::Error::Network(e.to_string()))?;
88
89        if !response.status().is_success() {
90            return Err(crate::error::Error::try_from_response_async(response).await);
91        }
92
93        let body = response
94            .bytes()
95            .await
96            .map_err(|e| crate::error::Error::BodyParse(e.to_string()))?;
97
98        let database: notionrs_types::object::database::DatabaseResponse =
99            serde_json::from_slice::<notionrs_types::object::database::DatabaseResponse>(&body)?;
100
101        Ok(database)
102    }
103}