diff --git a/examples/blocklist-update.rs b/examples/blocklist-update.rs index ccb4d63..2727947 100644 --- a/examples/blocklist-update.rs +++ b/examples/blocklist-update.rs @@ -12,9 +12,9 @@ async fn main() -> Result<()> { let url = env::var("TURL")?; let mut client; if let (Ok(user), Ok(password)) = (env::var("TUSER"), env::var("TPWD")) { - client = TransClient::with_auth(&url, BasicAuth { user, password }); + client = TransClient::with_auth(url.parse()?, BasicAuth { user, password }); } else { - client = TransClient::new(&url); + client = TransClient::new(url.parse()?); } let response: Result> = client.blocklist_update().await; match response { diff --git a/examples/free-space.rs b/examples/free-space.rs index b54eabc..e2ed090 100644 --- a/examples/free-space.rs +++ b/examples/free-space.rs @@ -13,9 +13,9 @@ async fn main() -> Result<()> { let dir = env::var("TDIR")?; let mut client; if let (Ok(user), Ok(password)) = (env::var("TUSER"), env::var("TPWD")) { - client = TransClient::with_auth(&url, BasicAuth { user, password }); + client = TransClient::with_auth(url.parse()?, BasicAuth { user, password }); } else { - client = TransClient::new(&url); + client = TransClient::new(url.parse()?); } let response: Result> = client.free_space(dir).await; match response { diff --git a/examples/port-test.rs b/examples/port-test.rs index 9f1e574..e227d54 100644 --- a/examples/port-test.rs +++ b/examples/port-test.rs @@ -12,9 +12,9 @@ async fn main() -> Result<()> { let url = env::var("TURL")?; let mut client; if let (Ok(user), Ok(password)) = (env::var("TUSER"), env::var("TPWD")) { - client = TransClient::with_auth(&url, BasicAuth { user, password }); + client = TransClient::with_auth(url.parse()?, BasicAuth { user, password }); } else { - client = TransClient::new(&url); + client = TransClient::new(url.parse()?); } let response: Result> = client.port_test().await; match response { diff --git a/examples/session-close.rs b/examples/session-close.rs index 0a3eab2..db6f5bf 100644 --- a/examples/session-close.rs +++ b/examples/session-close.rs @@ -12,9 +12,9 @@ async fn main() -> Result<()> { let url = env::var("TURL")?; let mut client; if let (Ok(user), Ok(password)) = (env::var("TUSER"), env::var("TPWD")) { - client = TransClient::with_auth(&url, BasicAuth { user, password }); + client = TransClient::with_auth(url.parse()?, BasicAuth { user, password }); } else { - client = TransClient::new(&url); + client = TransClient::new(url.parse()?); } let response: Result> = client.session_close().await; match response { diff --git a/examples/session-get.rs b/examples/session-get.rs index c438d05..2748e3e 100644 --- a/examples/session-get.rs +++ b/examples/session-get.rs @@ -12,9 +12,9 @@ async fn main() -> Result<()> { let url = env::var("TURL")?; let mut client; if let (Ok(user), Ok(password)) = (env::var("TUSER"), env::var("TPWD")) { - client = TransClient::with_auth(&url, BasicAuth { user, password }); + client = TransClient::with_auth(url.parse()?, BasicAuth { user, password }); } else { - client = TransClient::new(&url); + client = TransClient::new(url.parse()?); } let response: Result> = client.session_get().await; match response { diff --git a/examples/session-stats.rs b/examples/session-stats.rs index 8762942..9ba7dfa 100644 --- a/examples/session-stats.rs +++ b/examples/session-stats.rs @@ -12,9 +12,9 @@ async fn main() -> Result<()> { let url = env::var("TURL")?; let mut client; if let (Ok(user), Ok(password)) = (env::var("TUSER"), env::var("TPWD")) { - client = TransClient::with_auth(&url, BasicAuth { user, password }); + client = TransClient::with_auth(url.parse()?, BasicAuth { user, password }); } else { - client = TransClient::new(&url); + client = TransClient::new(url.parse()?); } let response: Result> = client.session_stats().await; match response { diff --git a/examples/torrent-action.rs b/examples/torrent-action.rs index 88ff332..9d5d4eb 100644 --- a/examples/torrent-action.rs +++ b/examples/torrent-action.rs @@ -12,9 +12,9 @@ async fn main() -> Result<()> { let url = env::var("TURL")?; let mut client; if let (Ok(user), Ok(password)) = (env::var("TUSER"), env::var("TPWD")) { - client = TransClient::with_auth(&url, BasicAuth { user, password }); + client = TransClient::with_auth(url.parse()?, BasicAuth { user, password }); } else { - client = TransClient::new(&url); + client = TransClient::new(url.parse()?); } let res1: RpcResponse = client .torrent_action(TorrentAction::Start, vec![Id::Id(1)]) diff --git a/examples/torrent-add.rs b/examples/torrent-add.rs index 5a263df..3bf35c3 100644 --- a/examples/torrent-add.rs +++ b/examples/torrent-add.rs @@ -14,9 +14,9 @@ async fn main() -> Result<()> { let url = env::var("TURL")?; let mut client; if let (Ok(user), Ok(password)) = (env::var("TUSER"), env::var("TPWD")) { - client = TransClient::with_auth(&url, BasicAuth { user, password }); + client = TransClient::with_auth(url.parse()?, BasicAuth { user, password }); } else { - client = TransClient::new(&url); + client = TransClient::new(url.parse()?); } let add: TorrentAddArgs = TorrentAddArgs { filename: Some( diff --git a/examples/torrent-get.rs b/examples/torrent-get.rs index 6b54e96..336ae0a 100644 --- a/examples/torrent-get.rs +++ b/examples/torrent-get.rs @@ -14,9 +14,9 @@ async fn main() -> Result<()> { let url = env::var("TURL")?; let mut client; if let (Ok(user), Ok(password)) = (env::var("TUSER"), env::var("TPWD")) { - client = TransClient::with_auth(&url, BasicAuth { user, password }); + client = TransClient::with_auth(url.parse()?, BasicAuth { user, password }); } else { - client = TransClient::new(&url); + client = TransClient::new(url.parse()?); } let res: RpcResponse> = client.torrent_get(None, None).await?; diff --git a/examples/torrent-remove.rs b/examples/torrent-remove.rs index d7ce61c..2bffdfa 100644 --- a/examples/torrent-remove.rs +++ b/examples/torrent-remove.rs @@ -12,9 +12,9 @@ async fn main() -> Result<()> { let url = env::var("TURL")?; let mut client; if let (Ok(user), Ok(password)) = (env::var("TUSER"), env::var("TPWD")) { - client = TransClient::with_auth(&url, BasicAuth { user, password }); + client = TransClient::with_auth(url.parse()?, BasicAuth { user, password }); } else { - client = TransClient::new(&url); + client = TransClient::new(url.parse()?); } let res: RpcResponse = client.torrent_remove(vec![Id::Id(1)], false).await?; println!("Remove result: {:?}", &res.is_ok()); diff --git a/examples/torrent-rename-path.rs b/examples/torrent-rename-path.rs index 12e0d97..fcd9d83 100644 --- a/examples/torrent-rename-path.rs +++ b/examples/torrent-rename-path.rs @@ -14,7 +14,7 @@ async fn main() -> Result<()> { user: env::var("TUSER")?, password: env::var("TPWD")?, }; - let mut client = TransClient::with_auth(&url, basic_auth); + let mut client = TransClient::with_auth(url.parse()?, basic_auth); let res: RpcResponse = client .torrent_rename_path( vec![Id::Id(1)], diff --git a/examples/torrent-set-location.rs b/examples/torrent-set-location.rs index ed3cff4..2f797b4 100644 --- a/examples/torrent-set-location.rs +++ b/examples/torrent-set-location.rs @@ -12,9 +12,9 @@ async fn main() -> Result<()> { let url = env::var("TURL")?; let mut client; if let (Ok(user), Ok(password)) = (env::var("TUSER"), env::var("TPWD")) { - client = TransClient::with_auth(&url, BasicAuth { user, password }); + client = TransClient::with_auth(url.parse()?, BasicAuth { user, password }); } else { - client = TransClient::new(&url); + client = TransClient::new(url.parse()?); } let res: RpcResponse = client .torrent_set_location( diff --git a/src/lib.rs b/src/lib.rs index 911fee9..b5a0ba3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ extern crate env_logger; extern crate log; extern crate reqwest; -use reqwest::header::CONTENT_TYPE; +use reqwest::{header::CONTENT_TYPE, Url}; use reqwest::{Client, StatusCode}; use serde::de::DeserializeOwned; @@ -36,7 +36,7 @@ impl std::fmt::Display for TransError { impl std::error::Error for TransError {} pub struct TransClient { - url: String, + url: Url, auth: Option, session_id: Option, client: Client, @@ -45,9 +45,9 @@ pub struct TransClient { impl TransClient { /// Returns HTTP(S) client with configured Basic Auth #[must_use] - pub fn with_auth(url: &str, basic_auth: BasicAuth) -> TransClient { + pub fn with_auth(url: Url, basic_auth: BasicAuth) -> TransClient { TransClient { - url: url.to_string(), + url, auth: Some(basic_auth), session_id: None, client: Client::new(), @@ -56,9 +56,9 @@ impl TransClient { /// Returns HTTP(S) client #[must_use] - pub fn new(url: &str) -> TransClient { + pub fn new(url: Url) -> TransClient { TransClient { - url: url.to_string(), + url, auth: None, session_id: None, client: Client::new(), @@ -69,10 +69,10 @@ impl TransClient { fn rpc_request(&self) -> reqwest::RequestBuilder { if let Some(auth) = &self.auth { self.client - .post(&self.url) + .post(self.url.clone()) .basic_auth(&auth.user, Some(&auth.password)) } else { - self.client.post(&self.url) + self.client.post(self.url.clone()) } .header(CONTENT_TYPE, "application/json") } @@ -99,7 +99,7 @@ impl TransClient { /// env_logger::init(); /// let url= env::var("TURL")?; /// let basic_auth = BasicAuth{user: env::var("TUSER")?, password: env::var("TPWD")?}; - /// let mut client = TransClient::with_auth(&url, basic_auth); + /// let mut client = TransClient::with_auth(url.parse()?, basic_auth); /// let response: Result> = client.session_get().await; /// match response { /// Ok(_) => println!("Yay!"), @@ -135,7 +135,7 @@ impl TransClient { /// env_logger::init(); /// let url= env::var("TURL")?; /// let basic_auth = BasicAuth{user: env::var("TUSER")?, password: env::var("TPWD")?}; - /// let mut client = TransClient::with_auth(&url, basic_auth); + /// let mut client = TransClient::with_auth(url.parse()?, basic_auth); /// let response: Result> = client.session_stats().await; /// match response { /// Ok(_) => println!("Yay!"), @@ -171,7 +171,7 @@ impl TransClient { /// env_logger::init(); /// let url= env::var("TURL")?; /// let basic_auth = BasicAuth{user: env::var("TUSER")?, password: env::var("TPWD")?}; - /// let mut client = TransClient::with_auth(&url, basic_auth); + /// let mut client = TransClient::with_auth(url.parse()?, basic_auth); /// let response: Result> = client.session_close().await; /// match response { /// Ok(_) => println!("Yay!"), @@ -207,7 +207,7 @@ impl TransClient { /// env_logger::init(); /// let url= env::var("TURL")?; /// let basic_auth = BasicAuth{user: env::var("TUSER")?, password: env::var("TPWD")?}; - /// let mut client = TransClient::with_auth(&url, basic_auth); + /// let mut client = TransClient::with_auth(url.parse()?, basic_auth); /// let response: Result> = client.blocklist_update().await; /// match response { /// Ok(_) => println!("Yay!"), @@ -244,7 +244,7 @@ impl TransClient { /// let url= env::var("TURL")?; /// let dir = env::var("TDIR")?; /// let basic_auth = BasicAuth{user: env::var("TUSER")?, password: env::var("TPWD")?}; - /// let mut client = TransClient::with_auth(&url, basic_auth); + /// let mut client = TransClient::with_auth(url.parse()?, basic_auth); /// let response: Result> = client.free_space(dir).await; /// match response { /// Ok(_) => println!("Yay!"), @@ -280,7 +280,7 @@ impl TransClient { /// env_logger::init(); /// let url= env::var("TURL")?; /// let basic_auth = BasicAuth{user: env::var("TUSER")?, password: env::var("TPWD")?}; - /// let mut client = TransClient::with_auth(&url, basic_auth); + /// let mut client = TransClient::with_auth(url.parse()?, basic_auth); /// let response: Result> = client.port_test().await; /// match response { /// Ok(_) => println!("Yay!"), @@ -319,7 +319,7 @@ impl TransClient { /// env_logger::init(); /// let url= env::var("TURL")?; /// let basic_auth = BasicAuth{user: env::var("TUSER")?, password: env::var("TPWD")?}; - /// let mut client = TransClient::with_auth(&url, basic_auth); + /// let mut client = TransClient::with_auth(url.parse()?, basic_auth); /// /// let res: RpcResponse> = client.torrent_get(None, None).await?; /// let names: Vec<&String> = res.arguments.torrents.iter().map(|it| it.name.as_ref().unwrap()).collect(); @@ -420,7 +420,7 @@ impl TransClient { /// env_logger::init(); /// let url= env::var("TURL")?; /// let basic_auth = BasicAuth{user: env::var("TUSER")?, password: env::var("TPWD")?}; - /// let mut client = TransClient::with_auth(&url, basic_auth); + /// let mut client = TransClient::with_auth(url.parse()?, basic_auth); /// 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?; @@ -460,7 +460,7 @@ impl TransClient { /// env_logger::init(); /// let url= env::var("TURL")?; /// let basic_auth = BasicAuth{user: env::var("TUSER")?, password: env::var("TPWD")?}; - /// let mut client = TransClient::with_auth(&url, basic_auth); + /// let mut client = TransClient::with_auth(url.parse()?, basic_auth); /// let res: RpcResponse = client.torrent_remove(vec![Id::Id(1)], false).await?; /// println!("Remove result: {:?}", &res.is_ok()); /// @@ -499,7 +499,7 @@ impl TransClient { /// env_logger::init(); /// let url= env::var("TURL")?; /// let basic_auth = BasicAuth{user: env::var("TUSER")?, password: env::var("TPWD")?}; - /// let mut client = TransClient::with_auth(&url, basic_auth); + /// let mut client = TransClient::with_auth(url.parse()?, basic_auth); /// let res: RpcResponse = client.torrent_set_location(vec![Id::Id(1)], String::from("/new/location"), Option::from(false)).await?; /// println!("Set-location result: {:?}", &res.is_ok()); /// @@ -539,7 +539,7 @@ impl TransClient { /// env_logger::init(); /// let url= env::var("TURL")?; /// let basic_auth = BasicAuth{user: env::var("TUSER")?, password: env::var("TPWD")?}; - /// let mut client = TransClient::with_auth(&url, basic_auth); + /// let mut client = TransClient::with_auth(url.parse()?, basic_auth); /// let res: RpcResponse = client.torrent_rename_path(vec![Id::Id(1)], String::from("Folder/OldFile.jpg"), String::from("NewFile.jpg")).await?; /// println!("rename-path result: {:#?}", res); /// @@ -579,7 +579,7 @@ impl TransClient { /// env_logger::init(); /// let url= env::var("TURL")?; /// let basic_auth = BasicAuth{user: env::var("TUSER")?, password: env::var("TPWD")?}; - /// let mut client = TransClient::with_auth(&url, basic_auth); + /// let mut client = TransClient::with_auth(url.parse()?, basic_auth); /// let add: TorrentAddArgs = TorrentAddArgs { /// filename: Some("https://releases.ubuntu.com/jammy/ubuntu-22.04.1-desktop-amd64.iso.torrent".to_string()), /// ..TorrentAddArgs::default() @@ -680,9 +680,9 @@ mod tests { let url = env::var("TURL")?; let mut client; if let (Ok(user), Ok(password)) = (env::var("TUSER"), env::var("TPWD")) { - client = TransClient::with_auth(&url, BasicAuth { user, password }); + client = TransClient::with_auth(url.parse()?, BasicAuth { user, password }); } else { - client = TransClient::new(&url); + client = TransClient::new(url.parse()?); } info!("Client is ready!"); let add: TorrentAddArgs = TorrentAddArgs {