From 3df01c4389efae0737ee2ffc67fb6d06694170be Mon Sep 17 00:00:00 2001 From: Nick Zana Date: Sun, 14 Mar 2021 11:58:11 -0400 Subject: [PATCH] Add support for no auth to examples Changes client setup in every example so that it checks whether both the TUSER and TPASS environment variables are set and, if not, assumes no basic auth. --- examples/session-get.rs | 11 ++++++----- examples/torrent-action.rs | 11 ++++++----- examples/torrent-add.rs | 11 ++++++----- examples/torrent-get.rs | 11 ++++++----- examples/torrent-remove.rs | 11 ++++++----- examples/torrent-set-location.rs | 11 ++++++----- 6 files changed, 36 insertions(+), 30 deletions(-) diff --git a/examples/session-get.rs b/examples/session-get.rs index db180ce..e8b4193 100644 --- a/examples/session-get.rs +++ b/examples/session-get.rs @@ -10,11 +10,12 @@ async fn main() -> Result<()> { dotenv().ok(); env_logger::init(); let url = env::var("TURL")?; - let basic_auth = BasicAuth { - user: env::var("TUSER")?, - password: env::var("TPWD")?, - }; - let client = TransClient::with_auth(&url, basic_auth); + let client; + if let (Ok(user), Ok(password)) = (env::var("TUSER"), env::var("TPWD")) { + client = TransClient::with_auth(&url, BasicAuth {user, password}); + } else { + client = TransClient::new(&url); + } let response: Result> = client.session_get().await; match response { Ok(_) => println!("Yay!"), diff --git a/examples/torrent-action.rs b/examples/torrent-action.rs index e9aadc7..d247756 100644 --- a/examples/torrent-action.rs +++ b/examples/torrent-action.rs @@ -11,11 +11,12 @@ async fn main() -> Result<()> { dotenv().ok(); env_logger::init(); let url = env::var("TURL")?; - let basic_auth = BasicAuth { - user: env::var("TUSER")?, - password: env::var("TPWD")?, - }; - let client = TransClient::with_auth(&url, basic_auth); + let client; + if let (Ok(user), Ok(password)) = (env::var("TUSER"), env::var("TPWD")) { + client = TransClient::with_auth(&url, BasicAuth {user, password}); + } else { + client = TransClient::new(&url); + } let res1: RpcResponse = client.torrent_action(TorrentAction::Start, vec![Id::Id(1)]).await?; println!("Start result: {:?}", &res1.is_ok()); let res2: RpcResponse = client.torrent_action(TorrentAction::Stop, vec![Id::Id(1)]).await?; diff --git a/examples/torrent-add.rs b/examples/torrent-add.rs index e8f75ef..d8e327a 100644 --- a/examples/torrent-add.rs +++ b/examples/torrent-add.rs @@ -11,11 +11,12 @@ async fn main() -> Result<()> { dotenv().ok(); env_logger::init(); let url = env::var("TURL")?; - let basic_auth = BasicAuth { - user: env::var("TUSER")?, - password: env::var("TPWD")?, - }; - let client = TransClient::with_auth(&url, basic_auth); + let client; + if let (Ok(user), Ok(password)) = (env::var("TUSER"), env::var("TPWD")) { + client = TransClient::with_auth(&url, BasicAuth {user, password}); + } else { + client = TransClient::new(&url); + } let add: TorrentAddArgs = TorrentAddArgs { filename: Some("https://releases.ubuntu.com/20.04/ubuntu-20.04-desktop-amd64.iso.torrent".to_string()), ..TorrentAddArgs::default() diff --git a/examples/torrent-get.rs b/examples/torrent-get.rs index 41342ab..01c6b45 100644 --- a/examples/torrent-get.rs +++ b/examples/torrent-get.rs @@ -11,11 +11,12 @@ async fn main() -> Result<()> { dotenv().ok(); env_logger::init(); let url = env::var("TURL")?; - let basic_auth = BasicAuth { - user: env::var("TUSER")?, - password: env::var("TPWD")?, - }; - let client = TransClient::with_auth(&url, basic_auth); + let client; + if let (Ok(user), Ok(password)) = (env::var("TUSER"), env::var("TPWD")) { + client = TransClient::with_auth(&url, BasicAuth {user, password}); + } else { + client = TransClient::new(&url); + } let res: RpcResponse> = client.torrent_get(None, None).await?; let names: Vec<&String> = res diff --git a/examples/torrent-remove.rs b/examples/torrent-remove.rs index 898e886..9b8e2cc 100644 --- a/examples/torrent-remove.rs +++ b/examples/torrent-remove.rs @@ -11,11 +11,12 @@ async fn main() -> Result<()> { dotenv().ok(); env_logger::init(); let url = env::var("TURL")?; - let basic_auth = BasicAuth { - user: env::var("TUSER")?, - password: env::var("TPWD")?, - }; - let client = TransClient::with_auth(&url, basic_auth); + let client; + if let (Ok(user), Ok(password)) = (env::var("TUSER"), env::var("TPWD")) { + client = TransClient::with_auth(&url, BasicAuth {user, password}); + } else { + client = TransClient::new(&url); + } let res: RpcResponse = client.torrent_remove(vec![Id::Id(1)], false).await?; println!("Remove result: {:?}", &res.is_ok()); diff --git a/examples/torrent-set-location.rs b/examples/torrent-set-location.rs index 61a204d..881f25c 100644 --- a/examples/torrent-set-location.rs +++ b/examples/torrent-set-location.rs @@ -11,11 +11,12 @@ async fn main() -> Result<()> { dotenv().ok(); env_logger::init(); let url = env::var("TURL")?; - let basic_auth = BasicAuth { - user: env::var("TUSER")?, - password: env::var("TPWD")?, - }; - let client = TransClient::with_auth(&url, basic_auth); + let client; + if let (Ok(user), Ok(password)) = (env::var("TUSER"), env::var("TPWD")) { + client = TransClient::with_auth(&url, BasicAuth {user, password}); + } else { + client = TransClient::new(&url); + } let res: RpcResponse = client.torrent_set_location( vec![Id::Id(1)], String::from("/new/location"),