diff options
author | 2021-05-16 13:46:59 -0700 | |
---|---|---|
committer | 2021-05-16 22:17:54 -0700 | |
commit | bb1f98f8e126658a658409fd1b317ae66148cfa6 (patch) | |
tree | ea55d6218ccf21b15a87529c0201f845fc1542ad | |
parent | 2abfa7ef1e5ec11de44ec2c59fb0f0980c6e4908 (diff) | |
download | notion-bb1f98f8e126658a658409fd1b317ae66148cfa6.tar.gz notion-bb1f98f8e126658a658409fd1b317ae66148cfa6.tar.zst notion-bb1f98f8e126658a658409fd1b317ae66148cfa6.zip |
refactor
-rw-r--r-- | src/lib.rs | 32 | ||||
-rw-r--r-- | src/models.rs | 80 |
2 files changed, 108 insertions, 4 deletions
@@ -1,5 +1,5 @@ use crate::models::search::{DatabaseQuery, SearchRequest}; -use crate::models::{Database, DatabaseId, ListResponse, Object, Page, Block}; +use crate::models::{Database, DatabaseId, ListResponse, Object, Page, Block, BlockId}; use reqwest::header::{HeaderMap, HeaderValue}; use reqwest::{header, Client, ClientBuilder, RequestBuilder}; use serde::de::DeserializeOwned; @@ -104,7 +104,7 @@ impl NotionApi { .await?) } - pub async fn get_block_children<T: Identifiable<Type = String>>( + pub async fn get_block_children<T: Identifiable<Type = BlockId>>( &self, block_id: T ) -> Result<ListResponse<Block>, NotionApiClientError> { @@ -215,6 +215,34 @@ mod tests { } #[tokio::test] + async fn get_block_children() -> Result<(), Box<dyn std::error::Error>> { + let api = test_client(); + + let search_response = api + .search(NotionSearch::Filter { + value: FilterValue::Page, + property: FilterProperty::Object, + }) + .await?; + + for page in search_response.results() { + let response = api + .get_block_children(page) + .await?; + } + + + // let db = response.results()[0].clone(); + // + // // todo: fix this clone issue + // let db_result = api.get_database(db.clone()).await?; + // + // assert_eq!(db, db_result); + + Ok(()) + } + + #[tokio::test] async fn query_database() -> Result<(), Box<dyn std::error::Error>> { let api = test_client(); diff --git a/src/models.rs b/src/models.rs index 142a6da..cb29e00 100644 --- a/src/models.rs +++ b/src/models.rs @@ -183,14 +183,90 @@ pub enum BlockType { } #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)] -pub struct Block { +pub struct BlockCommon { id: BlockId, - r#type: BlockType, created_time: DateTime<Utc>, last_edited_time: DateTime<Utc>, has_children: bool, } +#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)] +pub struct TextAndChildren { + text: Vec<RichText>, + children: Option<Vec<Block>>, +} + +#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)] +pub struct Text { + text: Vec<RichText>, +} + +#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)] +pub struct ToDoFields { + text: Vec<RichText>, + checked: bool, + children: Option<Vec<Block>>, +} + +#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)] +pub struct ChildPageFields { + title: String, +} + +#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)] +#[serde(tag = "type")] +#[serde(rename_all = "snake_case")] +pub enum Block { + Paragraph { + #[serde(flatten)] + common: BlockCommon, + paragraph: TextAndChildren + }, + #[serde(rename="heading_1")] + Heading1 { + #[serde(flatten)] + common: BlockCommon, + heading_1: Text, + }, + #[serde(rename="heading_2")] + Heading2 { + #[serde(flatten)] + common: BlockCommon, + heading_2: Text, + }, + #[serde(rename="heading_3")] + Heading3 { + #[serde(flatten)] + common: BlockCommon, + heading_3: Text, + }, + BulletedListItem { + #[serde(flatten)] + common: BlockCommon, + bulleted_list_item: TextAndChildren, + }, + NumberedListItem { + #[serde(flatten)] + common: BlockCommon, + numbered_list_item: TextAndChildren, + }, + ToDo { + #[serde(flatten)] + common: BlockCommon, + to_do: ToDoFields, + }, + Toggle { + #[serde(flatten)] + common: BlockCommon, + toggle: TextAndChildren, + }, + ChildPage { + #[serde(flatten)] + common: BlockCommon, + child_page: ChildPageFields, + }, + Unsupported {} +} #[derive(Eq, Serialize, Deserialize, Clone, Debug, PartialEq)] #[serde(tag = "object")] |