mirror of
https://github.com/dathere/ckanaction.git
synced 2026-07-05 17:12:19 +00:00
feat: headers support, revert new feature changes
This commit is contained in:
parent
a3e0df7318
commit
a0a86219b5
3 changed files with 13 additions and 52 deletions
|
|
@ -19,6 +19,3 @@ thiserror = "2.0.18"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tokio = { version = "1.52.3", features = ["full"] }
|
tokio = { version = "1.52.3", features = ["full"] }
|
||||||
|
|
||||||
[features]
|
|
||||||
full_response = []
|
|
||||||
|
|
|
||||||
|
|
@ -6,12 +6,6 @@ Rust library crate to access [CKAN](https://ckan.org) Action API endpoints throu
|
||||||
cargo add ckanaction
|
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
|
## Examples
|
||||||
|
|
|
||||||
56
src/lib.rs
56
src/lib.rs
|
|
@ -21,6 +21,7 @@ pub enum CKANError {
|
||||||
pub struct CKAN {
|
pub struct CKAN {
|
||||||
url: String,
|
url: String,
|
||||||
token: Option<String>,
|
token: Option<String>,
|
||||||
|
headers: Option<HashMap<String, String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hashmap_to_json(map: &HashMap<&str, serde_json::Value>) -> Result<serde_json::Value, CKANError> {
|
fn hashmap_to_json(map: &HashMap<&str, serde_json::Value>) -> Result<serde_json::Value, CKANError> {
|
||||||
|
|
@ -79,10 +80,11 @@ macro_rules! post {
|
||||||
#[bon]
|
#[bon]
|
||||||
impl CKAN {
|
impl CKAN {
|
||||||
#[builder]
|
#[builder]
|
||||||
pub fn new(url: &str, token: Option<String>) -> Self {
|
pub fn new(url: &str, token: Option<String>, headers: Option<HashMap<String, String>>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
url: url.to_string(),
|
url: url.to_string(),
|
||||||
token,
|
token,
|
||||||
|
headers,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -92,6 +94,11 @@ impl CKAN {
|
||||||
if self.token.is_some() {
|
if self.token.is_some() {
|
||||||
req_builder = req_builder.header("Authorization", self.token.clone().unwrap());
|
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
|
Ok(req_builder
|
||||||
.send()
|
.send()
|
||||||
.await?
|
.await?
|
||||||
|
|
@ -99,16 +106,6 @@ impl CKAN {
|
||||||
.await?)
|
.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]
|
#[builder]
|
||||||
async fn post(
|
async fn post(
|
||||||
&self,
|
&self,
|
||||||
|
|
@ -121,36 +118,11 @@ impl CKAN {
|
||||||
if self.token.is_some() {
|
if self.token.is_some() {
|
||||||
req_builder = req_builder.header("Authorization", self.token.clone().unwrap());
|
req_builder = req_builder.header("Authorization", self.token.clone().unwrap());
|
||||||
}
|
}
|
||||||
if let Some(file_pathbuf) = upload {
|
if let Some(headers) = &self.headers {
|
||||||
let mut form = reqwest::multipart::Form::new();
|
for (name, value) in headers {
|
||||||
if let Some(body_as_value) = body {
|
req_builder = req_builder.header(name, value);
|
||||||
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?.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<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 {
|
if let Some(file_pathbuf) = upload {
|
||||||
let mut form = reqwest::multipart::Form::new();
|
let mut form = reqwest::multipart::Form::new();
|
||||||
if let Some(body_as_value) = body {
|
if let Some(body_as_value) = body {
|
||||||
|
|
@ -160,11 +132,9 @@ impl CKAN {
|
||||||
}
|
}
|
||||||
form = form.file("upload", file_pathbuf).await?;
|
form = form.file("upload", file_pathbuf).await?;
|
||||||
req_builder = req_builder.multipart(form);
|
req_builder = req_builder.multipart(form);
|
||||||
let res = req_builder.send().await?;
|
Ok(req_builder.send().await?.json().await?)
|
||||||
Ok(res)
|
|
||||||
} else {
|
} else {
|
||||||
let res = req_builder.json(&body).send().await?;
|
Ok(req_builder.json(&body).send().await?.json().await?)
|
||||||
Ok(res)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue