aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 7a65f31fbc9503370ce173dc0aa6eda6a7e9fd91 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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(())
    }
}