mirror of
https://github.com/dathere/ckanaction.git
synced 2026-07-05 17:12:19 +00:00
feat: add full_response feature
This commit is contained in:
parent
0b8ffbf53c
commit
a3e0df7318
3 changed files with 49 additions and 0 deletions
|
|
@ -19,3 +19,6 @@ thiserror = "2.0.18"
|
|||
|
||||
[dev-dependencies]
|
||||
tokio = { version = "1.52.3", features = ["full"] }
|
||||
|
||||
[features]
|
||||
full_response = []
|
||||
|
|
|
|||
|
|
@ -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
|
||||
```
|
||||
|
||||

|
||||
|
||||
## Examples
|
||||
|
|
|
|||
40
src/lib.rs
40
src/lib.rs
|
|
@ -99,6 +99,16 @@ impl CKAN {
|
|||
.await?)
|
||||
}
|
||||
|
||||
#[cfg(feature = "full_response")]
|
||||
async fn get(&self, endpoint: String) -> Result<reqwest::Response, CKANError> {
|
||||
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<serde_json::Value>,
|
||||
upload: Option<PathBuf>,
|
||||
) -> Result<reqwest::Response, CKANError> {
|
||||
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(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue