Add free-space.

This commit is contained in:
Quang Ngô 2021-11-08 22:30:17 +07:00 committed by Aleksandr
parent c28cfc6630
commit c3f2197694
6 changed files with 88 additions and 1 deletions

View File

@ -32,6 +32,6 @@ spec: https://github.com/transmission/transmission/blob/master/extras/rpc-spec.t
- [X] blocklist-update
- [X] port-test
- [ ] session-close
- [ ] free-space
- [X] free-space
Support the project: [![Donate button](https://www.paypalobjects.com/en_US/DK/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=H337RKJSC4YG4&source=url)

27
examples/free-space.rs Normal file
View File

@ -0,0 +1,27 @@
extern crate transmission_rpc;
use dotenv::dotenv;
use std::env;
use transmission_rpc::types::{BasicAuth, Result, RpcResponse, FreeSpace};
use transmission_rpc::TransClient;
#[tokio::main]
async fn main() -> Result<()> {
dotenv().ok();
env_logger::init();
let url = env::var("TURL")?;
let dir = env::var("TDIR")?;
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<RpcResponse<FreeSpace>> = client.free_space(dir).await;
match response {
Ok(_) => println!("Yay!"),
Err(_) => panic!("Oh no!")
}
println!("Rpc response is ok: {}", response?.is_ok());
Ok(())
}

View File

@ -13,6 +13,7 @@ use types::BlocklistUpdate;
use types::SessionGet;
use types::SessionStats;
use types::PortTest;
use types::FreeSpace;
use types::TorrentAction;
use types::{Id, Torrent, TorrentGetField, Torrents};
use types::{Nothing, Result, RpcRequest, RpcResponse, RpcResponseArgument, TorrentRenamePath};
@ -186,6 +187,43 @@ impl TransClient {
self.call(RpcRequest::blocklist_update()).await
}
/// Performs a session stats 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, FreeSpace};
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
/// dotenv().ok();
/// env_logger::init();
/// let url= env::var("TURL")?;
/// let dir = env::var("TDIR")?;
/// let basic_auth = BasicAuth{user: env::var("TUSER")?, password: env::var("TPWD")?};
/// let client = TransClient::with_auth(&url, basic_auth);
/// let response: Result<RpcResponse<FreeSpace>> = client.free_space(dir).await;
/// match response {
/// Ok(_) => println!("Yay!"),
/// Err(_) => panic!("Oh no!")
/// }
/// println!("Rpc reqsponse is ok: {}", response?.is_ok());
/// Ok(())
/// }
/// ```
pub async fn free_space(&self, path: String) -> Result<RpcResponse<FreeSpace>> {
self.call(RpcRequest::free_space(path)).await
}
/// Performs a port test call
///
/// # Errors

View File

@ -24,6 +24,7 @@ pub use self::response::SessionGet;
pub use self::response::SessionStats;
pub use self::response::BlocklistUpdate;
pub use self::response::PortTest;
pub use self::response::FreeSpace;
pub use self::response::Torrent;
pub use self::response::TorrentAdded;
pub use self::response::Torrents;

View File

@ -30,6 +30,13 @@ impl RpcRequest {
}
}
pub fn free_space(path: String) -> RpcRequest {
RpcRequest {
method: String::from("free-space"),
arguments: Some(Args::FreeSpaceArgs(FreeSpaceArgs { path })),
}
}
pub fn port_test() -> RpcRequest {
RpcRequest {
method: String::from("port-test"),
@ -104,6 +111,7 @@ impl ArgumentFields for TorrentGetField {}
#[derive(Serialize, Debug, RustcEncodable, Clone)]
#[serde(untagged)]
pub enum Args {
FreeSpaceArgs(FreeSpaceArgs),
TorrentGetArgs(TorrentGetArgs),
TorrentActionArgs(TorrentActionArgs),
TorrentRemoveArgs(TorrentRemoveArgs),
@ -112,6 +120,11 @@ pub enum Args {
TorrentRenamePathArgs(TorrentRenamePathArgs),
}
#[derive(Serialize, Debug, RustcEncodable, Clone)]
pub struct FreeSpaceArgs {
path: String,
}
#[derive(Serialize, Debug, RustcEncodable, Clone)]
pub struct TorrentGetArgs {
#[serde(skip_serializing_if = "Option::is_none")]

View File

@ -53,6 +53,14 @@ pub struct BlocklistUpdate {
}
impl RpcResponseArgument for BlocklistUpdate {}
#[derive(Deserialize, Debug, Clone)]
pub struct FreeSpace {
path: String,
#[serde(rename = "size-bytes")]
size_bytes: i64,
}
impl RpcResponseArgument for FreeSpace {}
#[derive(Deserialize, Debug, Clone)]
pub struct PortTest {
#[serde(rename = "port-is-open")]