diff options
author | 2021-05-15 10:01:03 -0700 | |
---|---|---|
committer | 2021-05-15 10:01:03 -0700 | |
commit | 371a3e49ffe52c2684030f4d3cb669a3aac2b3ca (patch) | |
tree | 8d624c21a3faf2cf437a89b1102d7be25fa28fea /src/lib.rs | |
parent | d18843ab949e803e25c48f514b4e25244477c731 (diff) | |
download | notion-371a3e49ffe52c2684030f4d3cb669a3aac2b3ca.tar.gz notion-371a3e49ffe52c2684030f4d3cb669a3aac2b3ca.tar.zst notion-371a3e49ffe52c2684030f4d3cb669a3aac2b3ca.zip |
adding basic property filters
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 41 |
1 files changed, 39 insertions, 2 deletions
@@ -11,6 +11,23 @@ const NOTION_API_VERSION: &'static str = "2021-05-13"; // todo: replace with proper snafu error pub type NotionApiClientError = Box<dyn std::error::Error>; +trait Identifiable { + // There should only be one way to identify an object + type Type; + fn id(&self) -> &Self::Type; +} + +impl<T, U> Identifiable for &U +where + U: Identifiable<Type = T>, +{ + 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<T: AsRef<DatabaseId>>( + pub async fn get_database<T: Identifiable<Type = DatabaseId>>( &self, database_id: T, ) -> Result<Database, Box<dyn std::error::Error>> { 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<D, T>( + &self, + database: D, + query: T, + ) -> Result<ListResponse<Database>, NotionApiClientError> + where + T: Into<SearchRequest>, + D: Identifiable<Type = DatabaseId>, + { + 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)] |