notion_api_rs/notion/
client.rs

1use std::str::FromStr;
2use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
3use anyhow::{Result, Error};
4
5const API_V1: &str = "https://api.notion.com/v1";
6const NOTION_VERSION: &str = "2021-08-16";
7
8#[derive(Debug)]
9pub struct Client {
10    pub client: reqwest::Client,
11    pub base_api: String,
12}
13
14pub fn new(token: String) -> Result<Client> {
15    if token.is_empty() {
16        return Err(Error::msg("invalid token"));
17    }
18    let headers = HeaderMap::from_iter(vec![
19        (HeaderName::from_str("Authorization").unwrap(), HeaderValue::from_str(format!("Bearer {}", token).as_str()).unwrap()),
20        (HeaderName::from_str("Notion-Version").unwrap(), HeaderValue::from_str(NOTION_VERSION).unwrap()),
21    ]);
22    let client = reqwest::ClientBuilder::new().default_headers(headers).build().unwrap();
23    Ok(Client {
24        client,
25        base_api: API_V1.to_string(),
26    })
27}