add list devices example
This commit is contained in:
		
							
								
								
									
										21
									
								
								connectbox/examples/devices.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								connectbox/examples/devices.rs
									
									
									
									
									
										Normal 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(()) | ||||||
|  | } | ||||||
| @@ -6,7 +6,7 @@ pub enum Error { | |||||||
|     #[error("session token not found, are you logged in?")] |     #[error("session token not found, are you logged in?")] | ||||||
|     NoSessionToken, |     NoSessionToken, | ||||||
|     #[error("incorrect password")] |     #[error("incorrect password")] | ||||||
|     IncorrectPassword, |     IncorrectCode, | ||||||
|     #[error("unexpected response from the server: {0:?}")] |     #[error("unexpected response from the server: {0:?}")] | ||||||
|     UnexpectedResponse(String), |     UnexpectedResponse(String), | ||||||
|  |  | ||||||
|   | |||||||
| @@ -71,7 +71,6 @@ impl ConnectBox { | |||||||
|         ]; |         ]; | ||||||
|         let req = self.http.post(self.getter_url.clone()).form(&form); |         let req = self.http.post(self.getter_url.clone()).form(&form); | ||||||
|         let resp = req.send().await?.text().await?; |         let resp = req.send().await?.text().await?; | ||||||
|         println!("{resp:?}"); |  | ||||||
|         let obj = quick_xml::de::from_str(&resp)?; |         let obj = quick_xml::de::from_str(&resp)?; | ||||||
|         Ok(obj) |         Ok(obj) | ||||||
|     } |     } | ||||||
| @@ -94,7 +93,7 @@ impl ConnectBox { | |||||||
|         Ok(resp.text().await?) |         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 |         // get the session cookie | ||||||
|         self.http |         self.http | ||||||
|             .get(self.base_url.join("common_page/login.html")?) |             .get(self.base_url.join("common_page/login.html")?) | ||||||
| @@ -104,11 +103,11 @@ impl ConnectBox { | |||||||
|         // log in |         // log in | ||||||
|         let fields = vec![ |         let fields = vec![ | ||||||
|             ("Username".into(), "NULL".into()), |             ("Username".into(), "NULL".into()), | ||||||
|             ("Password".into(), code.into()), |             ("Password".into(), code.as_ref().into()), | ||||||
|         ]; |         ]; | ||||||
|         let response = self.xml_setter(functions::LOGIN, Some(fields)).await?; |         let response = self.xml_setter(functions::LOGIN, Some(fields)).await?; | ||||||
|         if response == "idloginincorrect" { |         if response == "idloginincorrect" { | ||||||
|             return Err(Error::IncorrectPassword); |             return Err(Error::IncorrectCode); | ||||||
|         } |         } | ||||||
|         let sid = response |         let sid = response | ||||||
|             .strip_prefix("successful;SID=") |             .strip_prefix("successful;SID=") | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user