diff options
author | 2021-05-14 07:18:20 -0700 | |
---|---|---|
committer | 2021-05-14 07:18:20 -0700 | |
commit | f7ef8d6d7f009990ba0c6be61cca507f097673b6 (patch) | |
tree | 1b58a5a8ca08f8fe8bb2a71ae832db21dd4953c0 /src/lib.rs | |
parent | 5b3816b043f3e97878b04b948241466a2527b94d (diff) | |
download | notion-f7ef8d6d7f009990ba0c6be61cca507f097673b6.tar.gz notion-f7ef8d6d7f009990ba0c6be61cca507f097673b6.tar.zst notion-f7ef8d6d7f009990ba0c6be61cca507f097673b6.zip |
First pass at a working Notion api client from the public beta
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..7a65f31 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,70 @@ +use crate::models::{Database, ListResponse}; +use std::collections::HashMap; +use crate::models::search::SearchRequest; + +mod models; + +struct NotionApi { + token: String +} + +impl NotionApi { + /// This method is apparently deprecated + 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") + .bearer_auth(self.token.clone()) + .send() + .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>> { + let client = reqwest::ClientBuilder::new().build()?; + let json = client.post("https://api.notion.com/v1/search") + .bearer_auth(self.token.clone()) + .json(&query.into()) + .send() + .await? + .text().await?; + + dbg!(serde_json::from_str::<serde_json::Value>(&json)?); + let result = serde_json::from_str(&json)?; + + Ok(result) + } +} + +#[cfg(test)] +mod tests { + 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() + }; + + dbg!(api.list_databases().await?); + + Ok(()) + } + + #[tokio::test] + async fn search() -> Result<(), Box<dyn std::error::Error>> { + let api = NotionApi { + token: TEST_TOKEN.to_string() + }; + + dbg!(api.search(NotionSearch::Filter {value: FilterValue::Database, property: FilterProperty::Object}).await?); + + Ok(()) + } +} |