notion_client/
endpoints.rs1use reqwest::{
2 header::{self, HeaderMap, HeaderValue},
3 ClientBuilder,
4};
5
6use crate::NotionClientError;
7
8use self::{
9 blocks::BlocksEndpoint, comments::CommentsEndpoint, databases::DatabasesEndpoint,
10 pages::PagesEndpoint, search::SearchEndpoint, users::UsersEndpoint,
11};
12
13pub mod blocks;
14pub mod comments;
15pub mod databases;
16pub mod pages;
17pub mod search;
18pub mod users;
19
20const NOTION_URI: &str = "https://api.notion.com/v1";
21const NOTION_VERSION: &str = "2022-06-28";
22
23#[derive(Debug, Clone)]
24pub struct Client {
25 pub blocks: BlocksEndpoint,
26 pub comments: CommentsEndpoint,
27 pub databases: DatabasesEndpoint,
28 pub pages: PagesEndpoint,
29 pub search: SearchEndpoint,
30 pub users: UsersEndpoint,
31}
32
33impl Client {
34 pub fn new(
35 token: String,
36 mut builder: Option<ClientBuilder>,
37 ) -> Result<Self, NotionClientError> {
38 let mut headers = HeaderMap::new();
39 headers.insert("Notion-Version", HeaderValue::from_static(NOTION_VERSION));
40 headers.insert("Content-Type", HeaderValue::from_static("application/json"));
41
42 let mut auth_value = HeaderValue::from_str(&format!("Bearer {}", token))
43 .map_err(|e| NotionClientError::InvalidHeader { source: e })?;
44 auth_value.set_sensitive(true);
45 headers.insert(header::AUTHORIZATION, auth_value);
46
47 if builder.is_none() {
48 builder = Some(ClientBuilder::new().default_headers(headers));
49 } else {
50 builder = Some(builder.unwrap().default_headers(headers));
51 }
52
53 let client = builder
54 .unwrap()
55 .build()
56 .map_err(|e| NotionClientError::FailedToBuildRequest { source: e })?;
57
58 Ok(Self {
59 blocks: BlocksEndpoint {
60 client: client.clone(),
61 },
62 comments: CommentsEndpoint {
63 client: client.clone(),
64 },
65 databases: DatabasesEndpoint {
66 client: client.clone(),
67 },
68 pages: PagesEndpoint {
69 client: client.clone(),
70 },
71 search: SearchEndpoint {
72 client: client.clone(),
73 },
74 users: UsersEndpoint {
75 client: client.clone(),
76 },
77 })
78 }
79}