feat: assume client create is safe + error rename

This commit is contained in:
Bram Dingelstad 2023-03-19 21:26:47 +01:00
parent 7da26e8959
commit 595e62509b

View file

@ -18,35 +18,35 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
HttpError(reqwest::Error), Http(reqwest::Error),
DeserializationError(serde_json::Error, Option<Value>), Deserialization(serde_json::Error, Option<Value>),
HeaderError(reqwest::header::InvalidHeaderValue), Header(reqwest::header::InvalidHeaderValue),
ChronoParseError(chrono::ParseError), ChronoParse(chrono::ParseError),
NoSuchProperty(String), NoSuchProperty(String),
Unknown Unknown
} }
impl From<reqwest::Error> for Error { impl From<reqwest::Error> for Error {
fn from(error: reqwest::Error) -> Self { fn from(error: reqwest::Error) -> Self {
Error::HttpError(error) Error::Http(error)
} }
} }
impl From<reqwest::header::InvalidHeaderValue> for Error { impl From<reqwest::header::InvalidHeaderValue> for Error {
fn from(error: reqwest::header::InvalidHeaderValue) -> Self { fn from(error: reqwest::header::InvalidHeaderValue) -> Self {
Error::HeaderError(error) Error::Header(error)
} }
} }
impl From<serde_json::Error> for Error { impl From<serde_json::Error> for Error {
fn from(error: serde_json::Error) -> Self { fn from(error: serde_json::Error) -> Self {
Error::DeserializationError(error, None) Error::Deserialization(error, None)
} }
} }
impl From<chrono::ParseError> for Error { impl From<chrono::ParseError> for Error {
fn from(error: chrono::ParseError) -> Self { fn from(error: chrono::ParseError) -> Self {
Error::ChronoParseError(error) Error::ChronoParse(error)
} }
} }
@ -62,17 +62,16 @@ fn parse<T: for<'de> Deserialize<'de>>(key: &str, data: &Value) -> Result<T> {
const NOTION_VERSION: &str = "2022-06-28"; const NOTION_VERSION: &str = "2022-06-28";
fn get_http_client(notion_api_key: &str) -> Result<reqwest::Client> { fn get_http_client(notion_api_key: &str) -> reqwest::Client {
let mut headers = HeaderMap::new(); let mut headers = HeaderMap::new();
headers.insert("Authorization", HeaderValue::from_str(&format!("Bearer {notion_api_key}"))?); headers.insert("Authorization", HeaderValue::from_str(&format!("Bearer {notion_api_key}")).expect("bearer token to be parsed into a header"));
headers.insert("Notion-Version", HeaderValue::from_str(NOTION_VERSION)?); headers.insert("Notion-Version", HeaderValue::from_str(NOTION_VERSION).expect("notion version to be parsed into a header"));
headers.insert("Content-Type", HeaderValue::from_static("application/json")); headers.insert("Content-Type", HeaderValue::from_static("application/json"));
Ok(
reqwest::ClientBuilder::new() reqwest::ClientBuilder::new()
.default_headers(headers) .default_headers(headers)
.build()? .build()
) .expect("to build a valid client out of notion_api_key")
} }
#[allow(unused)] #[allow(unused)]
@ -85,11 +84,9 @@ pub struct Client {
use std::rc::Rc; use std::rc::Rc;
impl<'a> Client { impl<'a> Client {
pub fn new(notion_api_key: &'a str) -> Result<Self> { pub fn new(notion_api_key: &'a str) -> Self {
let http_client = Rc::from(get_http_client(notion_api_key)?); let http_client = Rc::from(get_http_client(notion_api_key));
Ok(
Client { Client {
http_client: http_client.clone(), http_client: http_client.clone(),
pages: Pages { pages: Pages {
@ -99,7 +96,6 @@ impl<'a> Client {
http_client: http_client.clone() http_client: http_client.clone()
} }
} }
)
} }
} }
@ -125,7 +121,7 @@ impl Pages {
Err(error) => { Err(error) => {
println!("Error: {error:#?}"); println!("Error: {error:#?}");
println!("Body: {:#?}", request.json::<Value>().await?); println!("Body: {:#?}", request.json::<Value>().await?);
Err(Error::HttpError(error)) Err(Error::Http(error))
} }
} }
} }
@ -167,7 +163,7 @@ impl BlockChildren {
Err(error) => { Err(error) => {
println!("Error: {error:#?}"); println!("Error: {error:#?}");
println!("Body: {:#?}", request.json::<Value>().await?); println!("Body: {:#?}", request.json::<Value>().await?);
Err(Error::HttpError(error)) Err(Error::Http(error))
} }
} }
} }
@ -192,7 +188,7 @@ impl Databases {
Err(error) => { Err(error) => {
println!("Error: {error:#?}"); println!("Error: {error:#?}");
println!("Body: {:#?}", request.json::<Value>().await?); println!("Body: {:#?}", request.json::<Value>().await?);
Err(Error::HttpError(error)) Err(Error::Http(error))
} }
} }
} }