This commit is contained in:
lemonsh 2023-04-30 23:13:59 +02:00
parent 2087004e3a
commit 043559ca7c
2 changed files with 11 additions and 3 deletions

View File

@ -1,6 +1,7 @@
use reqwest::header::ToStrError; use reqwest::header::ToStrError;
use thiserror::Error; use thiserror::Error;
/// The error type used globally by the library.
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum Error { pub enum Error {
#[error("session token not found, are you logged in?")] #[error("session token not found, are you logged in?")]

View File

@ -2,7 +2,7 @@
use std::{borrow::Cow, fmt::Display, sync::Arc}; use std::{borrow::Cow, fmt::Display, sync::Arc};
use error::Error; pub use error::Error;
use reqwest::{ use reqwest::{
cookie::{CookieStore, Jar}, cookie::{CookieStore, Jar},
redirect::Policy, redirect::Policy,
@ -10,11 +10,13 @@ use reqwest::{
}; };
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
pub mod error; mod error;
mod functions; mod functions;
/// Data structures used by the library.
pub mod models; pub mod models;
pub(crate) type Result<T> = std::result::Result<T, error::Error>; // A Result type based on the library's Error
pub type Result<T> = std::result::Result<T, error::Error>;
type Field<'a, 'b> = (Cow<'a, str>, Cow<'b, str>); type Field<'a, 'b> = (Cow<'a, str>, Cow<'b, str>);
@ -30,6 +32,9 @@ pub struct ConnectBox {
} }
impl ConnectBox { impl ConnectBox {
/// Create a new client associated with the specified address. You must call [`login`](Self::login()) before use.
/// * `code` - the router password
/// * `auto_reauth` - whether to automatically re-authenticate when the session expires
pub fn new(address: impl Display, code: String, auto_reauth: bool) -> Result<Self> { pub fn new(address: impl Display, code: String, auto_reauth: bool) -> Result<Self> {
let cookie_store = Arc::new(Jar::default()); let cookie_store = Arc::new(Jar::default());
let http = Client::builder() let http = Client::builder()
@ -144,6 +149,7 @@ impl ConnectBox {
Ok(()) Ok(())
} }
/// Login to the router. This method must be called before using the client.
pub async fn login(&self) -> Result<()> { pub async fn login(&self) -> Result<()> {
// get the session cookie // get the session cookie
self.http self.http
@ -154,6 +160,7 @@ impl ConnectBox {
self._login().await self._login().await
} }
/// Get all devices connected to the router.
pub async fn get_devices(&self) -> Result<models::LanUserTable> { pub async fn get_devices(&self) -> Result<models::LanUserTable> {
self.xml_getter(functions::LAN_TABLE).await self.xml_getter(functions::LAN_TABLE).await
} }