Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions src/jade/pinserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,57 @@ impl PinServerClient {
where
D: serde::de::DeserializeOwned,
{
let url = match &req.urls {
api::PinServerUrls::Array(urls) => urls.first().ok_or(Error::NoUrlProvided)?,
api::PinServerUrls::Object { url, .. } => url,
// Match Python behavior: use first non-onion URL
let urls = match &req.urls {
api::PinServerUrls::Array(urls) => {
if urls.is_empty() {
return Err(Error::NoUrlProvided);
}
urls.clone()
}
api::PinServerUrls::Object { url, .. } => vec![url.clone()],
};

let res = self.client.post(url).json(&req.data).send().await?;
// Filter out .onion URLs and use the first one, matching Python behavior
let url = urls
.iter()
.find(|url| !url.ends_with(".onion"))
.ok_or(Error::NoUrlProvided)?;

if res.status().is_success() {
res.json().await.map_err(Error::from)
// Match Python: use_json = params.get('accept') in ['json', 'application/json']
let use_json = req.accept == "json" || req.accept == "application/json";

let res = if req.method == "POST" {
if use_json {
// Send as JSON like Python: json.dumps(params['data'])
self.client.post(url).json(&req.data).send().await
} else {
// Send as form data like Python: requests.post(url, data)
self.client.post(url).form(&req.data).send().await
}
} else {
Err(Error::Server(format!("{:?}", res)))
return Err(Error::UnsupportedMethod(req.method.clone()));
};

match res {
Ok(response) if response.status().is_success() => {
response.json().await.map_err(Error::from)
}
Ok(response) => Err(Error::Server(format!(
"HTTP {} from {}: {:?}",
response.status(),
url,
response
))),
Err(e) => Err(Error::Client(e)),
}
}
}

#[derive(Debug)]
pub enum Error {
NoUrlProvided,
UnsupportedMethod(String),
Client(reqwest::Error),
Server(String),
}
Expand Down