notion_client/endpoints/databases/
create.rs

1pub mod request;
2
3use crate::{
4    endpoints::NOTION_URI,
5    objects::{database::Database, Response},
6    NotionClientError,
7};
8
9use self::request::CreateADatabaseRequest;
10
11use super::DatabasesEndpoint;
12
13impl DatabasesEndpoint {
14    pub async fn create_a_database(
15        &self,
16        request: CreateADatabaseRequest,
17    ) -> Result<Database, NotionClientError> {
18        let json = serde_json::to_string(&request)
19            .map_err(|e| NotionClientError::FailedToSerialize { source: e })?;
20
21        let result = self
22            .client
23            .post(format!("{notion_uri}/databases", notion_uri = NOTION_URI))
24            .body(json)
25            .send()
26            .await
27            .map_err(|e| NotionClientError::FailedToRequest { source: e })?;
28
29        let body = result
30            .text()
31            .await
32            .map_err(|e| NotionClientError::FailedToText { source: e })?;
33
34        let response = serde_json::from_str(&body)
35            .map_err(|e| NotionClientError::FailedToDeserialize { source: e, body })?;
36
37        match response {
38            Response::Success(r) => Ok(r),
39            Response::Error(e) => Err(NotionClientError::InvalidStatusCode { error: e }),
40        }
41    }
42}