From 79793b5b653a3c7c27741306114375a803369753 Mon Sep 17 00:00:00 2001 From: Jake Swenson Date: Sun, 16 May 2021 08:04:30 -0700 Subject: [PATCH 1/2] fix: Fixes issue #3, search can return objects --- src/lib.rs | 61 ++++++++++++++++++++++++++++++++++++-------- src/models.rs | 32 ++++++++++++++++++----- src/models/search.rs | 8 +++--- 3 files changed, 80 insertions(+), 21 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0dd8366..7345ef9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,8 +43,11 @@ impl NotionApi { T: DeserializeOwned, { let json = request.send().await?.text().await?; - println!("JSON: {}", json); - dbg!(serde_json::from_str::(&json)?); + #[cfg(test)] + { + println!("JSON: {}", json); + dbg!(serde_json::from_str::(&json)?); + } let result = serde_json::from_str(&json)?; Ok(result) } @@ -61,7 +64,7 @@ impl NotionApi { pub async fn search>( &self, query: T, - ) -> Result, NotionApiClientError> { + ) -> Result, NotionApiClientError> { Ok(NotionApi::make_json_request( self.client .post("https://api.notion.com/v1/search") @@ -108,6 +111,7 @@ mod tests { use crate::models::search::{ DatabaseQuery, FilterCondition, FilterProperty, FilterValue, NotionSearch, TextCondition, }; + use crate::models::Object; use crate::{Identifiable, NotionApi}; fn test_token() -> String { @@ -137,16 +141,33 @@ mod tests { } #[tokio::test] - async fn search() -> Result<(), Box> { + async fn search_databases() -> Result<(), Box> { let api = test_client(); - dbg!( - api.search(NotionSearch::Filter { + let response = api + .search(NotionSearch::Filter { + property: FilterProperty::Object, value: FilterValue::Database, - property: FilterProperty::Object }) - .await? - ); + .await?; + + assert!(response.results.len() > 0); + + Ok(()) + } + + #[tokio::test] + async fn search_pages() -> Result<(), Box> { + let api = test_client(); + + let response = api + .search(NotionSearch::Filter { + property: FilterProperty::Object, + value: FilterValue::Page, + }) + .await?; + + assert!(response.results.len() > 0); Ok(()) } @@ -162,7 +183,16 @@ mod tests { }) .await?; - let db = response.results()[0].clone(); + let db = response + .results() + .iter() + .filter_map(|o| match o { + Object::Database { database } => Some(database), + _ => None, + }) + .next() + .expect("Test expected to find at least one database in notion") + .clone(); // todo: fix this clone issue let db_result = api.get_database(db.clone()).await?; @@ -183,7 +213,16 @@ mod tests { }) .await?; - let db = dbg!(response.results()[0].clone()); + let db = response + .results() + .iter() + .filter_map(|o| match o { + Object::Database { database } => Some(database), + _ => None, + }) + .next() + .expect("Test expected to find at least one database in notion") + .clone(); let pages = api .query_database( diff --git a/src/models.rs b/src/models.rs index 2581e13..40fe1e2 100644 --- a/src/models.rs +++ b/src/models.rs @@ -8,13 +8,14 @@ use crate::models::text::RichText; use serde::{Deserialize, Serialize}; use std::collections::HashMap; +use crate::models::paging::PagingCursor; use crate::Identifiable; pub use chrono::{DateTime, Utc}; pub use serde_json::value::Number; use std::fmt::{Display, Formatter}; #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Copy, Clone)] -#[serde(rename_all = "lowercase")] +#[serde(rename_all = "snake_case")] enum ObjectType { Database, List, @@ -77,9 +78,9 @@ impl Identifiable for Database { #[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)] pub struct ListResponse { - results: Vec, - next_cursor: Option, - has_more: bool, + pub results: Vec, + pub next_cursor: Option, + pub has_more: bool, } impl ListResponse { @@ -136,17 +137,36 @@ pub struct Page { #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)] pub struct Block {} -#[derive(Serialize, Deserialize, Clone)] +#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)] #[serde(tag = "object")] +#[serde(rename_all = "snake_case")] pub enum Object { Database { #[serde(flatten)] database: Database, }, - Page {}, + Page { + #[serde(flatten)] + page: Page, + }, List { + #[serde(flatten)] list: ListResponse, }, + User { + #[serde(flatten)] + user: User, + }, + Block {}, +} + +impl Object { + pub fn is_database(&self) -> bool { + match self { + Object::Database { .. } => true, + _ => false, + } + } } /// A zero-cost wrapper type around a Page ID diff --git a/src/models/search.rs b/src/models/search.rs index 6bf8d8b..8cb4327 100644 --- a/src/models/search.rs +++ b/src/models/search.rs @@ -29,14 +29,14 @@ pub enum FilterProperty { #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)] pub struct Sort { - direction: SortDirection, timestamp: SortTimestamp, + direction: SortDirection, } #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)] pub struct Filter { - value: FilterValue, property: FilterProperty, + value: FilterValue, } #[derive(Serialize, Debug, Eq, PartialEq, Default)] @@ -100,12 +100,12 @@ pub struct DatabaseQuery { pub enum NotionSearch { Query(String), Sort { - direction: SortDirection, timestamp: SortTimestamp, + direction: SortDirection, }, Filter { - value: FilterValue, property: FilterProperty, + value: FilterValue, }, } From e001c429182aa917a40b1175c906c22eda20a5cb Mon Sep 17 00:00:00 2001 From: Jake Swenson Date: Sun, 16 May 2021 08:19:42 -0700 Subject: [PATCH 2/2] fix: clippy error to use matches --- src/models.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/models.rs b/src/models.rs index 40fe1e2..4051787 100644 --- a/src/models.rs +++ b/src/models.rs @@ -162,10 +162,7 @@ pub enum Object { impl Object { pub fn is_database(&self) -> bool { - match self { - Object::Database { .. } => true, - _ => false, - } + matches!(self, Object::Database { .. }) } }