use color_eyre::eyre::Result;
pub async fn main_server_request(
host: Option<&String>,
port: Option<u16>,
url_path: &str,
) -> Result<String> {
let url_string = make_main_server_url(host, port, url_path);
println!("DEBUG main_server_request() request: {url_string}");
let response: reqwest::Response = match reqwest::Client::builder()
.build()?
.get(&url_string)
.header("Accept", "application/json")
.send()
.await
{
Ok(response) => response,
Err(e) => {
println!("Unable to access dweb server - is it running?");
println!("\nIf not, you can start it in another terminal using 'dweb serve' or by starting the dweb app if you have that installed.");
return Err(e.into());
}
};
match response.text().await {
Ok(body) => Ok(body),
Err(e) => {
println!("Failed to get text from server response: {e}");
Err(e.into())
}
}
}
pub fn make_main_server_url(host: Option<&String>, port: Option<u16>, url_path: &str) -> String {
let default_host = crate::web::LOCALHOST_STR.to_string();
let host = host.unwrap_or(&default_host);
let port = port.unwrap_or(crate::web::SERVER_PORTS_MAIN_PORT);
format!("http://{host}:{port}{url_path}")
}