1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
extern crate env_logger; #[macro_use] extern crate log; extern crate reqwest; use reqwest::header::CONTENT_TYPE; use serde::de::DeserializeOwned; pub mod types; use types::BasicAuth; use types::SessionGet; use types::TorrentAction; use types::{Id, Torrent, TorrentGetField, Torrents}; use types::{Nothing, Result, RpcRequest, RpcResponse, RpcResponseArgument}; use types::{TorrentAddArgs, TorrentAdded}; pub struct TransClient { url: String, auth: Option<BasicAuth>, } impl TransClient { /// Returns HTTP(S) client with configured Basic Auth pub fn with_auth(url: &str, basic_auth: BasicAuth) -> TransClient { TransClient { url: url.to_string(), auth: Some(basic_auth), } } /// Returns HTTP(S) client pub fn new(url: &str) -> TransClient { TransClient { url: url.to_string(), auth: None, } } /// Prepares a request for provided server and auth fn rpc_request(&self) -> reqwest::RequestBuilder { let client = reqwest::Client::new(); if let Some(auth) = &self.auth { client .post(&self.url) .basic_auth(&auth.user, Some(&auth.password)) } else { client.post(&self.url) } .header(CONTENT_TYPE, "application/json") } /// Performs session-get call and takes the x-transmission-session-id /// header to perform calls, using it's value /// /// # Errors /// /// If response is impossible to unwrap then it will return an empty session_id async fn get_session_id(&self) -> String { info!("Requesting session id info"); let response: reqwest::Result<reqwest::Response> = self .rpc_request() .json(&RpcRequest::session_get()) .send() .await; let session_id = match response { Ok(ref resp) => match resp.headers().get("x-transmission-session-id") { Some(res) => res.to_str().expect("header value should be a string"), _ => "", }, _ => "", } .to_owned(); info!("Received session id: {}", session_id); session_id } /// Performs a session get call /// /// # Errors /// /// Any IO Error or Deserialization error /// /// # Example /// /// ``` /// extern crate transmission_rpc; /// /// use std::env; /// use dotenv::dotenv; /// use transmission_rpc::TransClient; /// use transmission_rpc::types::{Result, RpcResponse, SessionGet, BasicAuth}; /// /// #[tokio::main] /// 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 response: Result<RpcResponse<SessionGet>> = client.session_get().await; /// match response { /// Ok(_) => println!("Yay!"), /// Err(_) => panic!("Oh no!") /// } /// println!("Rpc reqsponse is ok: {}", response?.is_ok()); /// Ok(()) /// } /// ``` pub async fn session_get(&self) -> Result<RpcResponse<SessionGet>> { self.call(RpcRequest::session_get()).await } /// Performs a torrent get call /// fileds - if None then ALL fields /// ids - if None then All items /// /// # Errors /// /// Any IO Error or Deserialization error /// /// # Example /// /// ``` /// extern crate transmission_rpc; /// /// use std::env; /// use dotenv::dotenv; /// use transmission_rpc::TransClient; /// use transmission_rpc::types::{Result, RpcResponse, BasicAuth}; /// use transmission_rpc::types::{Torrents, Torrent, TorrentGetField, Id}; /// /// #[tokio::main] /// 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 res: RpcResponse<Torrents<Torrent>> = client.torrent_get(None, None).await?; /// let names: Vec<&String> = res.arguments.torrents.iter().map(|it| it.name.as_ref().unwrap()).collect(); /// println!("{:#?}", names); /// /// let res1: RpcResponse<Torrents<Torrent>> = client.torrent_get(Some(vec![TorrentGetField::Id, TorrentGetField::Name]), Some(vec![Id::Id(1), Id::Id(2), Id::Id(3)])).await?; /// let first_three: Vec<String> = res1.arguments.torrents.iter().map(|it| /// format!("{}. {}",&it.id.as_ref().unwrap(), &it.name.as_ref().unwrap()) /// ).collect(); /// println!("{:#?}", first_three); /// /// /// let res2: RpcResponse<Torrents<Torrent>> = client.torrent_get(Some(vec![TorrentGetField::Id, TorrentGetField::HashString, TorrentGetField::Name]), Some(vec![Id::Hash(String::from("64b0d9a53ac9cd1002dad1e15522feddb00152fe"))])).await?; /// let info: Vec<String> = res2.arguments.torrents.iter().map(|it| /// format!("{:5}. {:^45} {}", /// &it.id.as_ref().unwrap(), /// &it.hash_string.as_ref().unwrap(), /// &it.name.as_ref().unwrap()) /// ).collect(); /// println!("{:#?}", info); /// /// Ok(()) /// } /// ``` pub async fn torrent_get( &self, fields: Option<Vec<TorrentGetField>>, ids: Option<Vec<Id>>, ) -> Result<RpcResponse<Torrents<Torrent>>> { self.call(RpcRequest::torrent_get(fields, ids)).await } /// Performs a torrent action call /// /// # Errors /// /// Any IO Error or Deserialization error /// /// # Example /// /// ``` /// extern crate transmission_rpc; /// /// use std::env; /// use dotenv::dotenv; /// use transmission_rpc::TransClient; /// use transmission_rpc::types::{Result, RpcResponse, BasicAuth}; /// use transmission_rpc::types::{TorrentAction, Nothing, Id}; /// /// #[tokio::main] /// 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 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(()) /// } /// ``` pub async fn torrent_action( &self, action: TorrentAction, ids: Vec<Id>, ) -> Result<RpcResponse<Nothing>> { self.call(RpcRequest::torrent_action(action, ids)).await } /// Performs a torrent remove call /// /// # Errors /// /// Any IO Error or Deserialization error /// /// # Example /// /// ``` /// extern crate transmission_rpc; /// /// use std::env; /// use dotenv::dotenv; /// use transmission_rpc::TransClient; /// use transmission_rpc::types::{Result, RpcResponse, BasicAuth}; /// use transmission_rpc::types::{Nothing, Id}; /// /// #[tokio::main] /// 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 res: RpcResponse<Nothing> = client.torrent_remove(vec![Id::Id(1)], false).await?; /// println!("Remove result: {:?}", &res.is_ok()); /// /// Ok(()) /// } /// ``` pub async fn torrent_remove( &self, ids: Vec<Id>, delete_local_data: bool, ) -> Result<RpcResponse<Nothing>> { self.call(RpcRequest::torrent_remove(ids, delete_local_data)) .await } /// Performs a torrent set location call /// /// # Errors /// /// Any IO Error or Deserialization error /// /// # Example /// /// ``` /// extern crate transmission_rpc; /// /// use std::env; /// use dotenv::dotenv; /// use transmission_rpc::TransClient; /// use transmission_rpc::types::{Result, RpcResponse, BasicAuth}; /// use transmission_rpc::types::{Nothing, Id}; /// /// #[tokio::main] /// 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 res: RpcResponse<Nothing> = client.torrent_set_location(vec![Id::Id(1)], String::from("/new/location"), Option::from(false)).await?; /// println!("Set-location result: {:?}", &res.is_ok()); /// /// Ok(()) /// } /// ``` pub async fn torrent_set_location( &self, ids: Vec<Id>, location: String, move_from: Option<bool>, ) -> Result<RpcResponse<Nothing>> { self.call(RpcRequest::torrent_set_location(ids, location, move_from)) .await } /// Performs a torrent add call /// /// # Errors /// /// Any IO Error or Deserialization error /// /// # Example /// /// ``` /// extern crate transmission_rpc; /// /// use std::env; /// use dotenv::dotenv; /// use transmission_rpc::TransClient; /// use transmission_rpc::types::{Result, RpcResponse, BasicAuth}; /// use transmission_rpc::types::{TorrentAddArgs, TorrentAdded}; /// /// #[tokio::main] /// 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 add: TorrentAddArgs = TorrentAddArgs { /// filename: Some("https://releases.ubuntu.com/20.04/ubuntu-20.04-desktop-amd64.iso.torrent".to_string()), /// ..TorrentAddArgs::default() /// }; /// let res: RpcResponse<TorrentAdded> = client.torrent_add(add).await?; /// println!("Add result: {:?}", &res.is_ok()); /// println!("response: {:?}", &res); /// /// Ok(()) /// } /// ``` pub async fn torrent_add(&self, add: TorrentAddArgs) -> Result<RpcResponse<TorrentAdded>> { if add.metainfo == None && add.filename == None { panic!("Metainfo or Filename should be provided") } self.call(RpcRequest::torrent_add(add)).await } /// Performs a JRPC call to the server /// /// # Errors /// /// Any IO Error or Deserialization error async fn call<RS>(&self, request: RpcRequest) -> Result<RpcResponse<RS>> where RS: RpcResponseArgument + DeserializeOwned + std::fmt::Debug, { info!("Loaded auth: {:?}", &self.auth); let rq: reqwest::RequestBuilder = self .rpc_request() .header("X-Transmission-Session-Id", self.get_session_id().await) .json(&request); info!( "Request body: {:?}", rq.try_clone() .expect("Unable to get the request body") .body_string()? ); let resp: reqwest::Response = rq.send().await?; let rpc_response: RpcResponse<RS> = resp.json().await?; info!("Response body: {:#?}", rpc_response); Ok(rpc_response) } } trait BodyString { fn body_string(self) -> Result<String>; } impl BodyString for reqwest::RequestBuilder { fn body_string(self) -> Result<String> { let rq = self.build()?; let body = rq.body().unwrap().as_bytes().unwrap(); Ok(std::str::from_utf8(body)?.to_string()) } } #[cfg(test)] mod tests { use super::*; use dotenv::dotenv; use std::env; #[tokio::test] pub async fn test_malformed_url() -> 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); info!("Client is ready!"); let add: TorrentAddArgs = TorrentAddArgs { filename: Some( "https://releases.ubuntu.com/20.04/ubuntu-20.04-desktop-amd64.iso.torrentt" .to_string(), ), ..TorrentAddArgs::default() }; match client.torrent_add(add).await { Ok(res) => { println!("Add result: {:?}", &res.is_ok()); println!("response: {:?}", &res); assert!(!&res.is_ok()); } Err(e) => { println!("Error: {:#?}", e); } } Ok(()) } }