From a3e0df73185c87e83d8640679cf2a974cce83f8e Mon Sep 17 00:00:00 2001 From: rzmk <30333942+rzmk@users.noreply.github.com> Date: Tue, 9 Jun 2026 14:29:17 -0400 Subject: [PATCH] feat: add full_response feature --- Cargo.toml | 3 +++ README.md | 6 ++++++ src/lib.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index ef09505..a30cbec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,3 +19,6 @@ 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 2436c0f..84596c8 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,12 @@ 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 440cf4a..1398b66 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -99,6 +99,16 @@ 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, @@ -128,6 +138,36 @@ impl CKAN { } } + #[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(); + 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()); + } + } + form = form.file("upload", file_pathbuf).await?; + req_builder = req_builder.multipart(form); + let res = req_builder.send().await?; + Ok(res) + } else { + let res = req_builder.json(&body).send().await?; + Ok(res) + } + } + /// https://docs.ckan.org/en/2.11/api/index.html#ckan.logic.action.get.package_list #[builder] pub async fn package_list(