use crate::error::ApiResult;
use crate::io::http::{get, response_body_bytes};
use acorn_schema::pid::SWHID;
use acorn_schema::util::constants::app::SOFTWARE_HERITAGE_RESOLVER_URI;
use color_eyre::eyre::eyre;
pub async fn resolve(identifier: &SWHID) -> ApiResult<serde_json::Value> {
resolve_with(SOFTWARE_HERITAGE_RESOLVER_URI, identifier).await
}
pub(crate) async fn resolve_with(base_uri: &str, identifier: &SWHID) -> ApiResult<serde_json::Value> {
let identifier_str = identifier.to_string();
match identifier_str.is_empty() || identifier.object_id.is_empty() {
| true => Err(eyre!("SWHID resolver requires a valid SWHID")),
| false => {
let encoded = urlencoding::encode(&identifier_str);
let url = format!("{}/{encoded}/", base_uri.trim_end_matches('/'));
response_body_bytes(get(url).send().await, "Software Heritage could not resolve the SWHID")
.await
.and_then(|body| serde_json::from_slice(&body).map_err(|why| eyre!("Software Heritage returned invalid resolver metadata — {why}")))
}
}
}