use crate::{
errors::NetworkError,
mion::{
cgis::{do_simple_request, encode_url_parameters},
proto::cgis::MionCGIErrors,
},
};
use reqwest::{Client, Method};
use std::{fmt::Display, net::Ipv4Addr, ops::Deref};
pub async fn get_vdd2(mion_ip: Ipv4Addr) -> Result<String, NetworkError> {
get_vdd2_with_raw_client(&Client::default(), mion_ip).await
}
pub async fn get_vdd2_with_raw_client(
client: &Client,
mion_ip: Ipv4Addr,
) -> Result<String, NetworkError> {
let body_as_string = do_raw_signal_http_request(client, mion_ip, &[("sig", "VDD2")]).await?;
let start_tag_location = body_as_string
.find("<body>")
.map(|num| num + 6)
.ok_or_else(|| MionCGIErrors::HtmlResponseMissingBody(body_as_string.clone()))?;
let body_without_start_tag = body_as_string.split_at(start_tag_location).1;
let end_tag_location = body_without_start_tag
.find("</body>")
.ok_or_else(|| MionCGIErrors::HtmlResponseMissingBody(body_as_string.clone()))?;
Ok(body_without_start_tag
.split_at(end_tag_location)
.0
.to_owned())
}
pub async fn do_raw_signal_http_request(
client: &Client,
mion_ip: Ipv4Addr,
url_parameters: &[(impl Deref<Target = str>, impl Display)],
) -> Result<String, NetworkError> {
do_simple_request::<String>(
client,
Method::POST,
format!("http://{mion_ip}/signal_get.cgi"),
Some(encode_url_parameters(url_parameters)),
None,
)
.await
}