add list devices example

This commit is contained in:
lemonsh 2023-04-29 22:11:56 +02:00
parent 4b91e6f405
commit d600c4d4b7
3 changed files with 25 additions and 5 deletions

View File

@ -0,0 +1,21 @@
use std::env;
use color_eyre::Result;
use connectbox::ConnectBox;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
color_eyre::install()?;
let mut args = env::args().skip(1);
let ip = args.next().expect("no ip specified");
let code = args.next().expect("no code specified");
let connect_box = ConnectBox::new(ip)?;
connect_box.login(&code).await?;
let devices = connect_box.get_devices().await?;
println!("{devices:?}");
Ok(())
}

View File

@ -6,7 +6,7 @@ pub enum Error {
#[error("session token not found, are you logged in?")]
NoSessionToken,
#[error("incorrect password")]
IncorrectPassword,
IncorrectCode,
#[error("unexpected response from the server: {0:?}")]
UnexpectedResponse(String),

View File

@ -71,7 +71,6 @@ impl ConnectBox {
];
let req = self.http.post(self.getter_url.clone()).form(&form);
let resp = req.send().await?.text().await?;
println!("{resp:?}");
let obj = quick_xml::de::from_str(&resp)?;
Ok(obj)
}
@ -94,7 +93,7 @@ impl ConnectBox {
Ok(resp.text().await?)
}
pub async fn login(&self, code: &str) -> Result<()> {
pub async fn login(&self, code: impl AsRef<str>) -> Result<()> {
// get the session cookie
self.http
.get(self.base_url.join("common_page/login.html")?)
@ -104,11 +103,11 @@ impl ConnectBox {
// log in
let fields = vec![
("Username".into(), "NULL".into()),
("Password".into(), code.into()),
("Password".into(), code.as_ref().into()),
];
let response = self.xml_setter(functions::LOGIN, Some(fields)).await?;
if response == "idloginincorrect" {
return Err(Error::IncorrectPassword);
return Err(Error::IncorrectCode);
}
let sid = response
.strip_prefix("successful;SID=")