diff --git a/Cargo.toml b/Cargo.toml index a30cbec..ef09505 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,3 @@ thiserror = "2.0.18" [dev-dependencies] tokio = { version = "1.52.3", features = ["full"] } - -[features] -full_response = [] diff --git a/README.md b/README.md index 84596c8..2436c0f 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,6 @@ Rust library crate to access [CKAN](https://ckan.org) Action API endpoints throu cargo add ckanaction ``` -If you want to return the full [`reqwest::Response`](https://docs.rs/reqwest/latest/reqwest/struct.Response.html) instead of attempting to deserialize the response to JSON by default (e.g. erroneous response such as a Cloudflare firewall), include the `full_response` feature flag so you can add error handling in your code: - -```bash -cargo add ckanaction -F full_response -``` - ![ckanaction IDE hints demo](https://github.com/user-attachments/assets/515d5742-4a33-43b9-9f3f-4795d18579c0) ## Examples diff --git a/src/lib.rs b/src/lib.rs index 1398b66..fe6c388 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,7 @@ pub enum CKANError { pub struct CKAN { url: String, token: Option, + headers: Option>, } fn hashmap_to_json(map: &HashMap<&str, serde_json::Value>) -> Result { @@ -79,10 +80,11 @@ macro_rules! post { #[bon] impl CKAN { #[builder] - pub fn new(url: &str, token: Option) -> Self { + pub fn new(url: &str, token: Option, headers: Option>) -> Self { Self { url: url.to_string(), token, + headers, } } @@ -92,6 +94,11 @@ impl CKAN { if self.token.is_some() { req_builder = req_builder.header("Authorization", self.token.clone().unwrap()); } + if let Some(headers) = &self.headers { + for (name, value) in headers { + req_builder = req_builder.header(name, value); + } + } Ok(req_builder .send() .await? @@ -99,16 +106,6 @@ impl CKAN { .await?) } - #[cfg(feature = "full_response")] - async fn get(&self, endpoint: String) -> Result { - let client = reqwest::Client::new(); - let mut req_builder = client.get(endpoint); - if self.token.is_some() { - req_builder = req_builder.header("Authorization", self.token.clone().unwrap()); - } - Ok(req_builder.send().await?) - } - #[builder] async fn post( &self, @@ -121,35 +118,10 @@ impl CKAN { if self.token.is_some() { req_builder = req_builder.header("Authorization", self.token.clone().unwrap()); } - if let Some(file_pathbuf) = upload { - let mut form = reqwest::multipart::Form::new(); - if let Some(body_as_value) = body { - for entry in body_as_value.as_object().unwrap().iter() { - form = form.text(entry.0.to_owned(), entry.1.as_str().unwrap().to_owned()); - } + if let Some(headers) = &self.headers { + for (name, value) in headers { + req_builder = req_builder.header(name, value); } - form = form.file("upload", file_pathbuf).await?; - req_builder = req_builder.multipart(form); - let res = req_builder.send().await?.json().await?; - Ok(res) - } else { - let res = req_builder.json(&body).send().await?.json().await?; - Ok(res) - } - } - - #[cfg(feature = "full_response")] - #[builder] - async fn post( - &self, - endpoint: String, - body: Option, - upload: Option, - ) -> Result { - let client = reqwest::Client::new(); - let mut req_builder = client.post(endpoint); - if self.token.is_some() { - req_builder = req_builder.header("Authorization", self.token.clone().unwrap()); } if let Some(file_pathbuf) = upload { let mut form = reqwest::multipart::Form::new(); @@ -160,11 +132,9 @@ impl CKAN { } form = form.file("upload", file_pathbuf).await?; req_builder = req_builder.multipart(form); - let res = req_builder.send().await?; - Ok(res) + Ok(req_builder.send().await?.json().await?) } else { - let res = req_builder.json(&body).send().await?; - Ok(res) + Ok(req_builder.json(&body).send().await?.json().await?) } }