use color_eyre::eyre::{eyre, Error, Result};
use crate::history::HistoryAddress;
use crate::web::name::RecognisedName;
use crate::web::request::main_server_request;
pub const DWEB_API_ROUTE_V0: &str = "/dweb-0"; pub const DWEB_ANT_API_ROUTE_V0: &str = "/dweb-0";
pub const ANT_API_ROUTE_V0: &str = "/ant-0";
pub const DWEB_API_ROUTE: &str = DWEB_API_ROUTE_V0;
pub const DWEB_ANT_API_ROUTE: &str = DWEB_ANT_API_ROUTE_V0;
pub const ANT_API_ROUTE: &str = ANT_API_ROUTE_V0;
pub async fn name_register(
dweb_name: &str,
history_address: HistoryAddress,
host: Option<&String>,
port: Option<u16>,
) -> Result<()> {
let url_path = format!(
"{DWEB_API_ROUTE}/name-register/{dweb_name}/{}",
history_address.to_hex()
);
match main_server_request(host, port, &url_path).await {
Ok(_json_value) => Ok(()),
Err(e) => Err(eyre!(Into::<Error>::into(e))),
}
}
pub async fn name_list(host: Option<&String>, port: Option<u16>) -> Result<Vec<RecognisedName>> {
let url_path = format!("{DWEB_API_ROUTE}/name-list");
match main_server_request(host, port, &url_path).await {
Ok(json) => {
let vec: Vec<RecognisedName> = serde_json::from_str(&json)?;
Ok(vec)
}
Err(e) => Err(eyre!(e)),
}
}