transmission-rpc/examples/torrent-action.rs
Nick Zana 3df01c4389 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.
2021-03-14 11:58:11 -04:00

27 lines
902 B
Rust

extern crate transmission_rpc;
use dotenv::dotenv;
use std::env;
use transmission_rpc::types::{BasicAuth, Result, RpcResponse};
use transmission_rpc::types::{Id, Nothing, TorrentAction};
use transmission_rpc::TransClient;
#[tokio::main]
async fn main() -> Result<()> {
dotenv().ok();
env_logger::init();
let url = env::var("TURL")?;
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<Nothing> = client.torrent_action(TorrentAction::Start, vec![Id::Id(1)]).await?;
println!("Start result: {:?}", &res1.is_ok());
let res2: RpcResponse<Nothing> = client.torrent_action(TorrentAction::Stop, vec![Id::Id(1)]).await?;
println!("Stop result: {:?}", &res2.is_ok());
Ok(())
}