From 371a3e49ffe52c2684030f4d3cb669a3aac2b3ca Mon Sep 17 00:00:00 2001 From: Jake Swenson Date: Sat, 15 May 2021 10:01:03 -0700 Subject: adding basic property filters --- src/lib.rs | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 19d79ee..3636ab7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,23 @@ const NOTION_API_VERSION: &'static str = "2021-05-13"; // todo: replace with proper snafu error pub type NotionApiClientError = Box; +trait Identifiable { + // There should only be one way to identify an object + type Type; + fn id(&self) -> &Self::Type; +} + +impl Identifiable for &U +where + U: Identifiable, +{ + type Type = T; + + fn id(&self) -> &Self::Type { + self.id() + } +} + struct NotionApi { client: Client, } @@ -63,16 +80,36 @@ impl NotionApi { .await?) } - pub async fn get_database>( + pub async fn get_database>( &self, database_id: T, ) -> Result> { Ok(NotionApi::make_json_request(self.client.get(format!( "https://api.notion.com/v1/databases/{}", - database_id.as_ref().id() + database_id.id().id() ))) .await?) } + + pub async fn query_database( + &self, + database: D, + query: T, + ) -> Result, NotionApiClientError> + where + T: Into, + D: Identifiable, + { + Ok(NotionApi::make_json_request( + self.client + .post(&format!( + "https://api.notion.com/v1/databases/{database_id}/query", + database_id = database.id() + )) + .json(&query.into()), + ) + .await?) + } } #[cfg(test)] -- cgit v1.2.3