feat: using Rc's instead of borrows

This commit is contained in:
Bram Dingelstad 2023-03-19 19:56:33 +01:00
parent 0a905f1092
commit 7da26e8959

View file

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