use vastlint_core::ValidationResult;
use crate::result_to_json_object;
const SHARE_ENDPOINT: &str = "https://vastlint.org/api/reports";
pub fn upload(entries: &[(String, ValidationResult)]) -> Result<String, String> {
if entries.is_empty() {
return Err("no results to share".to_owned());
}
let reports_json: Vec<String> = entries
.iter()
.map(|(label, result)| result_to_json_object(label, result))
.collect();
let body = format!(
"{{\"cli_version\":\"{}\",\"reports\":[{}]}}",
env!("CARGO_PKG_VERSION"),
reports_json.join(","),
);
let response = ureq::post(SHARE_ENDPOINT)
.set("Content-Type", "application/json")
.timeout(std::time::Duration::from_secs(10))
.send_string(&body)
.map_err(|e| e.to_string())?;
let text = response.into_string().map_err(|e| e.to_string())?;
extract_url(&text).ok_or_else(|| format!("unexpected response: {}", text))
}
fn extract_url(json: &str) -> Option<String> {
let key = "\"url\":\"";
let start = json.find(key)? + key.len();
let end = json[start..].find('"')? + start;
Some(json[start..end].to_owned())
}