From a5fccf17f6cf477426c17216b1bc7628e0434d79 Mon Sep 17 00:00:00 2001 From: Jake Swenson Date: Sat, 15 May 2021 10:27:52 -0700 Subject: working database queries --- src/lib.rs | 50 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 3636ab7..7c4e454 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ -use crate::models::search::SearchRequest; -use crate::models::{Database, DatabaseId, ListResponse}; +use crate::models::search::{DatabaseQuery, SearchRequest}; +use crate::models::{Database, DatabaseId, ListResponse, Object, Page}; use reqwest::header::{HeaderMap, HeaderValue}; use reqwest::{header, Client, ClientBuilder, RequestBuilder}; use serde::de::DeserializeOwned; @@ -54,6 +54,7 @@ impl NotionApi { T: DeserializeOwned, { let json = request.send().await?.text().await?; + println!("JSON: {}", json); dbg!(serde_json::from_str::(&json)?); let result = serde_json::from_str(&json)?; Ok(result) @@ -71,7 +72,7 @@ impl NotionApi { pub async fn search>( &self, query: T, - ) -> Result, Box> { + ) -> Result, NotionApiClientError> { Ok(NotionApi::make_json_request( self.client .post("https://api.notion.com/v1/search") @@ -83,7 +84,7 @@ impl NotionApi { pub async fn get_database>( &self, database_id: T, - ) -> Result> { + ) -> Result { Ok(NotionApi::make_json_request(self.client.get(format!( "https://api.notion.com/v1/databases/{}", database_id.id().id() @@ -95,9 +96,9 @@ impl NotionApi { &self, database: D, query: T, - ) -> Result, NotionApiClientError> + ) -> Result, NotionApiClientError> where - T: Into, + T: Into, D: Identifiable, { Ok(NotionApi::make_json_request( @@ -114,8 +115,12 @@ impl NotionApi { #[cfg(test)] mod tests { - use crate::models::search::{FilterProperty, FilterValue, NotionSearch}; + use crate::models::search::PropertyCondition::Text; + use crate::models::search::{ + DatabaseQuery, FilterCondition, FilterProperty, FilterValue, NotionSearch, TextCondition, + }; use crate::NotionApi; + const TEST_TOKEN: &'static str = include_str!(".api_token"); fn test_client() -> NotionApi { @@ -165,4 +170,35 @@ mod tests { Ok(()) } + + #[tokio::test] + async fn query_database() -> Result<(), Box> { + let api = test_client(); + + let response = api + .search(NotionSearch::Filter { + value: FilterValue::Database, + property: FilterProperty::Object, + }) + .await?; + + let db = dbg!(response.results()[0].clone()); + + let pages = api + .query_database( + db, + DatabaseQuery { + filter: Some(FilterCondition { + property: "Name".to_string(), + condition: Text(TextCondition::Contains("First".to_string())), + }), + ..Default::default() + }, + ) + .await?; + + assert_eq!(pages.results().len(), 1); + + Ok(()) + } } -- cgit v1.2.3