diff --git a/src/lib.rs b/src/lib.rs index 62f7e10..e25c09d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,4 @@ use std::collections::HashMap; -use std::sync::Arc; use chrono::{DateTime, NaiveTime, Utc}; use lazy_static::lazy_static; @@ -104,7 +103,7 @@ pub struct SearchOptions<'a> { #[derive(Default)] pub struct ClientBuilder { api_key: Option, - custom_request: Option>, + custom_request: Option>, } impl ClientBuilder { @@ -123,7 +122,7 @@ impl ClientBuilder { + Send + Sync, { - self.custom_request = Some(Arc::new(callback)); + self.custom_request = Some(Box::new(callback)); self } @@ -132,46 +131,18 @@ impl ClientBuilder { pub fn build(self) -> Client { let notion_api_key = self.api_key.expect("api_key to be set"); - let request_handler = - self.custom_request - .unwrap_or(Arc::new(|request_builder: surf::RequestBuilder| { - Box::pin(request_builder) - })); - - let http_client = Arc::from(get_http_client(¬ion_api_key)); - Client { - http_client: http_client.clone(), - request_handler: request_handler.clone(), - - pages: Pages { - http_client: http_client.clone(), - request_handler: request_handler.clone(), - }, - blocks: Blocks { - http_client: http_client.clone(), - request_handler: request_handler.clone(), - }, - databases: Databases { - http_client: http_client.clone(), - request_handler: request_handler.clone(), - }, - users: Users { - http_client: http_client.clone(), - request_handler: request_handler.clone(), - }, + http_client: get_http_client(¬ion_api_key), + request_handler: self.custom_request.unwrap_or(Box::new( + |request_builder: surf::RequestBuilder| Box::pin(request_builder), + )), } } } pub struct Client { - http_client: Arc, - request_handler: Arc, - - pub pages: Pages, - pub blocks: Blocks, - pub databases: Databases, - pub users: Users, + http_client: surf::Client, + request_handler: Box, } impl<'a> Client { @@ -199,19 +170,46 @@ impl<'a> Client { status => Err(Error::Http(status, response)), } } + + pub fn pages(&'a self) -> Pages<'a> { + Pages { + http_client: &self.http_client, + request_handler: &self.request_handler, + } + } + + pub fn blocks(&'a self) -> Blocks<'a> { + Blocks { + http_client: &self.http_client, + request_handler: &self.request_handler, + } + } + + pub fn databases(&'a self) -> Databases<'a> { + Databases { + http_client: &self.http_client, + request_handler: &self.request_handler, + } + } + + pub fn users(&'a self) -> Users<'a> { + Users { + http_client: &self.http_client, + request_handler: &self.request_handler, + } + } } pub struct PageOptions<'a> { pub page_id: &'a str, } -#[derive(Clone)] -pub struct Pages { - http_client: Arc, - request_handler: Arc, +pub struct Pages<'a> { + http_client: &'a surf::Client, + request_handler: &'a Box, } -impl Pages { +impl Pages<'_> { pub async fn retrieve(&self, options: PageOptions<'_>) -> Result { let url = format!( "https://api.notion.com/v1/pages/{page_id}", @@ -228,31 +226,30 @@ impl Pages { } } -#[derive(Clone)] -pub struct Blocks { - http_client: Arc, - request_handler: Arc, +pub struct Blocks<'a> { + http_client: &'a surf::Client, + request_handler: &'a Box, } -impl Blocks { +impl Blocks<'_> { pub fn children(&self) -> BlockChildren { BlockChildren { - http_client: self.http_client.clone(), - request_handler: self.request_handler.clone(), + http_client: self.http_client, + request_handler: self.request_handler, } } } -pub struct BlockChildren { - http_client: Arc, - request_handler: Arc, +pub struct BlockChildren<'a> { + http_client: &'a surf::Client, + request_handler: &'a Box, } pub struct BlockChildrenListOptions<'a> { pub block_id: &'a str, } -impl BlockChildren { +impl BlockChildren<'_> { pub async fn list( &self, options: BlockChildrenListOptions<'_>, @@ -273,13 +270,12 @@ impl BlockChildren { } } -#[derive(Clone)] -pub struct Databases { - http_client: Arc, - request_handler: Arc, +pub struct Databases<'a> { + http_client: &'a surf::Client, + request_handler: &'a Box, } -impl Databases { +impl Databases<'_> { pub async fn query<'a>( &self, options: DatabaseQueryOptions<'a>, @@ -344,7 +340,7 @@ mod tests { #[async_std::test] async fn check_database_query() { - let databases = Client::new() + let _ = Client::new() .api_key("secret_FuhJkAoOVZlk8YUT9ZOeYqWBRRZN6OMISJwhb4dTnud") .build() .search::(SearchOptions { @@ -364,10 +360,10 @@ mod tests { #[async_std::test] async fn test_blocks() { - let blocks = Client::new() + let _ = Client::new() .api_key("secret_FuhJkAoOVZlk8YUT9ZOeYqWBRRZN6OMISJwhb4dTnud") .build() - .blocks + .blocks() .children() .list(BlockChildrenListOptions { block_id: "0d253ab0f751443aafb9bcec14012897", @@ -385,13 +381,12 @@ pub struct DatabaseQueryOptions<'a> { pub start_cursor: Option, } -#[derive(Clone)] -pub struct Users { - http_client: Arc, - request_handler: Arc, +pub struct Users<'a> { + http_client: &'a surf::Client, + request_handler: &'a Box, } -impl Users { +impl Users<'_> { pub async fn get(&self) -> Result> { let url = "https://api.notion.com/v1/users".to_owned();