diff --git a/connectbox/examples/devices.rs b/connectbox/examples/devices.rs new file mode 100644 index 0000000..490f695 --- /dev/null +++ b/connectbox/examples/devices.rs @@ -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(()) +} diff --git a/connectbox/src/error.rs b/connectbox/src/error.rs index 28a89d4..6d5cff6 100644 --- a/connectbox/src/error.rs +++ b/connectbox/src/error.rs @@ -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), diff --git a/connectbox/src/lib.rs b/connectbox/src/lib.rs index e27ce9a..d563d56 100644 --- a/connectbox/src/lib.rs +++ b/connectbox/src/lib.rs @@ -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) -> 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=")