From 88abc7001025b75b69424f910936592948bb97b5 Mon Sep 17 00:00:00 2001 From: Leopith <113810806+leopith@users.noreply.github.com> Date: Mon, 19 Sep 2022 11:28:03 +1000 Subject: [PATCH] Implement basic torrent-set rpc to set labels and file priorities --- src/lib.rs | 47 +++++++++++++++++++++++++++++++++++++++++++- src/types/request.rs | 22 +++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 3bad9c8..ad02749 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -294,7 +294,7 @@ impl TransClient { } /// Performs a torrent get call - /// fileds - if None then ALL fields + /// fields - if None then ALL fields /// ids - if None then All items /// /// # Errors @@ -351,6 +351,51 @@ impl TransClient { self.call(RpcRequest::torrent_get(fields, ids)).await } + /// Performs a torrent set call + /// args - the fields to update + /// 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, TorrentSetArgs, 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 mut client = TransClient::with_auth(&url, basic_auth); + /// + /// let args = TorrentSetArgs { + /// labels: Some(vec![String::from("blue")]), + /// ..Default::default(), + /// }; + /// assert!(client.torrent_set(args, vec![Id::Id(0)]).await?.is_ok()) + /// + /// Ok(()) + /// } + /// ``` + pub async fn torrent_set( + &mut self, + args: TorrentSetArgs, + ids: Option>, + ) -> Result> { + self.call(RpcRequest::torrent_set(args, ids)).await + } + /// Performs a torrent action call /// /// # Errors diff --git a/src/types/request.rs b/src/types/request.rs index fe79ecf..892cf98 100644 --- a/src/types/request.rs +++ b/src/types/request.rs @@ -66,6 +66,14 @@ impl RpcRequest { } } + pub fn torrent_set(mut args: TorrentSetArgs, ids: Option>) -> RpcRequest { + args.ids = ids; + RpcRequest { + method: String::from("torrent-set"), + arguments: Some(Args::TorrentSetArgs(args)), + } + } + pub fn torrent_remove(ids: Vec, delete_local_data: bool) -> RpcRequest { RpcRequest { method: String::from("torrent-remove"), @@ -358,3 +366,17 @@ impl TorrentAction { .to_string() } } + +#[derive(Serialize, Debug, Clone, Default)] +pub struct TorrentSetArgs { + #[serde(skip_serializing_if = "Option::is_none")] + pub labels: Option>, + #[serde(rename = "priority-low", skip_serializing_if = "Option::is_none")] + pub priority_low: Option>, + #[serde(rename = "priority-normal", skip_serializing_if = "Option::is_none")] + pub priority_normal: Option>, + #[serde(rename = "priority-high", skip_serializing_if = "Option::is_none")] + pub priority_high: Option>, + #[serde(skip_serializing_if = "Option::is_none")] + pub ids: Option>, +}