use crate::{
errors::NetworkError,
mion::{
cgis::{do_simple_request, encode_url_parameters, parse_result_from_body},
proto::cgis::StatusOperation,
},
};
use reqwest::{Client, Method};
use std::{fmt::Display, net::Ipv4Addr, ops::Deref};
pub async fn set_disc_eject_state(mion_ip: Ipv4Addr, disc_in: bool) -> Result<bool, NetworkError> {
set_disc_eject_state_with_raw_client(&Client::default(), mion_ip, disc_in).await
}
pub async fn set_disc_eject_state_with_raw_client(
client: &Client,
mion_ip: Ipv4Addr,
disc_in: bool,
) -> Result<bool, NetworkError> {
let body_as_string = do_raw_status_request(
client,
mion_ip,
&[
("operation", Into::<&str>::into(StatusOperation::Eject)),
("disc", if disc_in { "in" } else { "out" }),
("frommenu", "no"),
],
)
.await?;
Ok(parse_result_from_body(
&body_as_string,
Into::<&str>::into(StatusOperation::Eject),
)?)
}
pub async fn do_raw_status_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}/mion/status.cgi"),
Some(encode_url_parameters(url_parameters)),
None,
)
.await
}