diff options
author | 2021-05-14 07:20:01 -0700 | |
---|---|---|
committer | 2021-05-14 07:20:01 -0700 | |
commit | b22ae2cf8a9e2b6af1a54f79ecdb31ad4d2c31c5 (patch) | |
tree | e6f9e1dcfcf6aed2a09517ce08d276b6ce9e2b79 /src | |
parent | f7ef8d6d7f009990ba0c6be61cca507f097673b6 (diff) | |
download | notion-b22ae2cf8a9e2b6af1a54f79ecdb31ad4d2c31c5.tar.gz notion-b22ae2cf8a9e2b6af1a54f79ecdb31ad4d2c31c5.tar.zst notion-b22ae2cf8a9e2b6af1a54f79ecdb31ad4d2c31c5.zip |
clean code
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 40 | ||||
-rw-r--r-- | src/models.rs | 26 | ||||
-rw-r--r-- | src/models/search.rs | 49 |
3 files changed, 62 insertions, 53 deletions
@@ -1,37 +1,45 @@ +use crate::models::search::SearchRequest; use crate::models::{Database, ListResponse}; use std::collections::HashMap; -use crate::models::search::SearchRequest; mod models; struct NotionApi { - token: String + token: String, } impl NotionApi { /// This method is apparently deprecated - pub async fn list_databases(&self) -> Result<ListResponse<Database>, Box<dyn std::error::Error>> { + pub async fn list_databases( + &self, + ) -> Result<ListResponse<Database>, Box<dyn std::error::Error>> { let client = reqwest::ClientBuilder::new().build()?; - let json = client.get("https://api.notion.com/v1/databases") + let json = client + .get("https://api.notion.com/v1/databases") .bearer_auth(self.token.clone()) .send() .await? - .text().await?; + .text() + .await?; dbg!(&json); let result = serde_json::from_str(&json)?; Ok(result) } - - pub async fn search<T: Into<SearchRequest>>(&self, query: T) -> Result<ListResponse<Database>, Box<dyn std::error::Error>> { + pub async fn search<T: Into<SearchRequest>>( + &self, + query: T, + ) -> Result<ListResponse<Database>, Box<dyn std::error::Error>> { let client = reqwest::ClientBuilder::new().build()?; - let json = client.post("https://api.notion.com/v1/search") + let json = client + .post("https://api.notion.com/v1/search") .bearer_auth(self.token.clone()) .json(&query.into()) .send() .await? - .text().await?; + .text() + .await?; dbg!(serde_json::from_str::<serde_json::Value>(&json)?); let result = serde_json::from_str(&json)?; @@ -42,14 +50,14 @@ impl NotionApi { #[cfg(test)] mod tests { + use crate::models::search::{FilterProperty, FilterValue, NotionSearch}; use crate::NotionApi; - use crate::models::search::{NotionSearch, FilterValue, FilterProperty}; const TEST_TOKEN: &'static str = include_str!(".api_token"); #[tokio::test] async fn list_databases() -> Result<(), Box<dyn std::error::Error>> { let api = NotionApi { - token: TEST_TOKEN.to_string() + token: TEST_TOKEN.to_string(), }; dbg!(api.list_databases().await?); @@ -60,10 +68,16 @@ mod tests { #[tokio::test] async fn search() -> Result<(), Box<dyn std::error::Error>> { let api = NotionApi { - token: TEST_TOKEN.to_string() + token: TEST_TOKEN.to_string(), }; - dbg!(api.search(NotionSearch::Filter {value: FilterValue::Database, property: FilterProperty::Object}).await?); + dbg!( + api.search(NotionSearch::Filter { + value: FilterValue::Database, + property: FilterProperty::Object + }) + .await? + ); Ok(()) } diff --git a/src/models.rs b/src/models.rs index c11c97b..cfb43d9 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,7 +1,7 @@ pub mod search; -use serde::{Serialize, Deserialize}; use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; use std::collections::HashMap; #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] @@ -108,15 +108,11 @@ pub struct Select { pub enum PropertyConfiguration { /// Represents the special Title property required on every database. /// See https://developers.notion.com/reference/database#title-configuration - Title { - id: PropertyId, - }, + Title { id: PropertyId }, /// Represents a Text property /// https://developers.notion.com/reference/database#text-configuration #[serde(rename = "rich_text")] - Text { - id: PropertyId, - }, + Text { id: PropertyId }, /// Represents a Number Property /// See https://developers.notion.com/reference/database#number-configuration Number { @@ -126,20 +122,13 @@ pub enum PropertyConfiguration { }, /// Represents a Select Property /// See https://developers.notion.com/reference/database#select-configuration - Select { - id: PropertyId, - select: Select, - }, + Select { id: PropertyId, select: Select }, /// Represents a Date Property /// See https://developers.notion.com/reference/database#date-configuration - Date { - id: PropertyId - }, + Date { id: PropertyId }, /// Represents a File Property /// See https://developers.notion.com/reference/database#date-configuration - File { - id: PropertyId - }, + File { id: PropertyId }, } #[derive(Serialize, Deserialize, Debug, Eq, PartialEq)] @@ -189,7 +178,7 @@ struct RichTextCommon { #[derive(Serialize, Deserialize, Debug, Eq, PartialEq)] pub struct Link { - url: String + url: String, } #[derive(Serialize, Deserialize, Debug, Eq, PartialEq)] @@ -217,6 +206,5 @@ pub enum RichText { Equation { #[serde(flatten)] rich_text: RichTextCommon, - }, } diff --git a/src/models/search.rs b/src/models/search.rs index cc0cdf4..dcbd206 100644 --- a/src/models/search.rs +++ b/src/models/search.rs @@ -1,11 +1,11 @@ -use serde::{Deserialize, Serialize}; use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Hash, Copy, Clone)] #[serde(rename_all = "snake_case")] pub enum SortDirection { Ascending, - Descending + Descending, } #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Hash, Copy, Clone)] @@ -18,33 +18,32 @@ pub enum SortTimestamp { #[serde(rename_all = "snake_case")] pub enum FilterValue { Page, - Database + Database, } - #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Hash, Copy, Clone)] #[serde(rename_all = "snake_case")] pub enum FilterProperty { - Object + Object, } #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)] pub struct Sort { direction: SortDirection, - timestamp: SortTimestamp + timestamp: SortTimestamp, } #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)] pub struct Filter { value: FilterValue, - property: FilterProperty + property: FilterProperty, } #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone)] #[serde(transparent)] pub struct PagingCursor(String); -#[derive(Serialize, Debug, Eq, PartialEq, Default)] +#[derive(Serialize, Debug, Eq, PartialEq, Default)] pub struct SearchRequest { #[serde(skip_serializing_if = "Option::is_none")] query: Option<String>, @@ -55,35 +54,43 @@ pub struct SearchRequest { #[serde(skip_serializing_if = "Option::is_none")] start_cursor: Option<PagingCursor>, #[serde(skip_serializing_if = "Option::is_none")] - page_size: Option<u8> + page_size: Option<u8>, } #[derive(Debug, Eq, PartialEq)] pub enum NotionSearch { Query(String), - Sort{ + Sort { direction: SortDirection, - timestamp: SortTimestamp + timestamp: SortTimestamp, }, Filter { value: FilterValue, - property: FilterProperty - } + property: FilterProperty, + }, } impl From<NotionSearch> for SearchRequest { fn from(search: NotionSearch) -> Self { match search { - NotionSearch::Query(query) => SearchRequest { query: Some(query), ..Default::default() }, - NotionSearch::Sort { direction, timestamp } => SearchRequest { sort: Some(Sort { - direction, timestamp - }), ..Default::default()}, - NotionSearch::Filter { value, property } => SearchRequest { - filter: Some(Filter { - value, property + NotionSearch::Query(query) => SearchRequest { + query: Some(query), + ..Default::default() + }, + NotionSearch::Sort { + direction, + timestamp, + } => SearchRequest { + sort: Some(Sort { + direction, + timestamp, }), ..Default::default() - } + }, + NotionSearch::Filter { value, property } => SearchRequest { + filter: Some(Filter { value, property }), + ..Default::default() + }, } } } |