aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 53e639f6170e112094847457177da8d4940da245 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use crate::models::search::SearchRequest;
use crate::models::{Database, ListResponse};
use std::collections::HashMap;

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::models::search::{FilterProperty, FilterValue, NotionSearch};
    use crate::NotionApi;
    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(())
    }
}