diff --git a/src/lib.rs b/src/lib.rs index 23a14ab..5903eb7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -75,38 +75,32 @@ fn get_http_client(notion_api_key: &str) -> Result { ) } +#[allow(unused)] pub struct Client { - token: String, + http_client: Rc, + pages: Pages, + blocks: Blocks } -impl Client { - pub fn new(notion_api_key: &str) -> Result { - get_http_client(notion_api_key)?; +use std::rc::Rc; +impl<'a> Client { + pub fn new(notion_api_key: &'a str) -> Result { + let http_client = Rc::from(get_http_client(notion_api_key)?); + + Ok( Client { - token: notion_api_key.to_owned(), + http_client: http_client.clone(), + pages: Pages { + http_client: http_client.clone() + }, + blocks: Blocks { + http_client: http_client.clone() + } } ) } - - pub fn pages(&self) -> Pages { - Pages { - token: self.token.to_owned() - } - } - - pub fn blocks(&self) -> Blocks { - Blocks { - token: self.token.to_owned() - } - } - - pub fn databases(&self) -> Databases { - Databases { - token: self.token.to_owned() - } - } } pub struct PageOptions<'a> { @@ -114,14 +108,14 @@ pub struct PageOptions<'a> { } pub struct Pages { - token: String + http_client: Rc } impl Pages { - pub async fn retrieve(self, options: PageOptions<'_>) -> Result { + pub async fn retrieve<'a>(self, options: PageOptions<'a>) -> Result { let url = format!("https://api.notion.com/v1/pages/{page_id}", page_id = options.page_id); - let request = get_http_client(&self.token)? + let request = self.http_client .get(url) .send() .await?; @@ -138,19 +132,19 @@ impl Pages { } pub struct Blocks { - token: String, + http_client: Rc, } impl Blocks { pub fn children(&self) -> BlockChildren { BlockChildren { - token: self.token.to_owned() + http_client: self.http_client.clone() } } } pub struct BlockChildren { - token: String + http_client: Rc, } pub struct BlockChildrenListOptions<'a> { @@ -158,10 +152,10 @@ pub struct BlockChildrenListOptions<'a> { } impl BlockChildren { - pub async fn list(self, options: BlockChildrenListOptions<'_>) -> Result> { + pub async fn list<'a>(self, options: BlockChildrenListOptions<'a>) -> Result> { let url = format!("https://api.notion.com/v1/blocks/{block_id}/children", block_id = options.block_id); - let request = get_http_client(&self.token)? + let request = self.http_client .get(&url) .send() .await?; @@ -180,14 +174,15 @@ impl BlockChildren { } pub struct Databases { - token: String + http_client: Rc, + // token: String } impl Databases { pub async fn query(self, options: DatabaseQueryOptions) -> Result> { let url = format!("https://api.notion.com/v1/databases/{database_id}/query", database_id = options.database_id); - let request = get_http_client(&self.token)? + let request = self.http_client .post(url) .send() .await?; @@ -845,7 +840,7 @@ impl std::fmt::Display for Date { #[serde(try_from = "String", into = "String")] pub enum DateValue { DateTime(DateTime), - Date(chrono::Date) + Date(chrono::NaiveDate) } impl TryFrom for DateValue { @@ -856,8 +851,7 @@ impl TryFrom for DateValue { let value = if ISO_8601_DATE.is_match(&string) { DateValue::Date( DateTime::parse_from_rfc3339(&format!("{string}T00:00:00Z"))? - .date() - .with_timezone(&Utc) + .date_naive() ) } else { DateValue::DateTime( @@ -879,7 +873,11 @@ impl From for String { impl std::fmt::Display for DateValue { fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { let value = match self { - DateValue::Date(date) => date.and_hms_opt(0, 0, 0).unwrap().to_rfc3339(), + DateValue::Date(date) => DateTime::::from_utc( + date.and_hms_opt(0, 0, 0) + .expect("to parse NaiveDate into DateTime "), + Utc + ).to_rfc3339(), DateValue::DateTime(date_time) => date_time.to_rfc3339() }; Ok(write!(formatter, "{}", value)?)