Add session-stats.

This commit is contained in:
Quang Ngô 2021-11-02 22:31:01 +07:00 committed by Aleksandr
parent 42f58b6707
commit 4a3c0e4118
6 changed files with 105 additions and 1 deletions

View File

@ -28,7 +28,7 @@ spec: https://github.com/transmission/transmission/blob/master/extras/rpc-spec.t
- [X] torrent-rename-path
- [ ] session-set
- [X] session-get
- [ ] session-stats
- [X] session-stats
- [ ] blocklist-update
- [ ] port-test
- [ ] session-close

26
examples/session-stats.rs Normal file
View File

@ -0,0 +1,26 @@
extern crate transmission_rpc;
use dotenv::dotenv;
use std::env;
use transmission_rpc::types::{BasicAuth, Result, RpcResponse, SessionStats};
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 response: Result<RpcResponse<SessionStats>> = client.session_stats().await;
match response {
Ok(_) => println!("Yay!"),
Err(_) => panic!("Oh no!")
}
println!("Rpc response is ok: {}", response?.is_ok());
Ok(())
}

View File

@ -10,6 +10,7 @@ pub mod types;
use types::BasicAuth;
use types::SessionGet;
use types::SessionStats;
use types::TorrentAction;
use types::{Id, Torrent, TorrentGetField, Torrents};
use types::{Nothing, Result, RpcRequest, RpcResponse, RpcResponseArgument, TorrentRenamePath};
@ -111,6 +112,42 @@ impl TransClient {
self.call(RpcRequest::session_get()).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, SessionStats, 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<SessionStats>> = client.session_stats().await;
/// match response {
/// Ok(_) => println!("Yay!"),
/// Err(_) => panic!("Oh no!")
/// }
/// println!("Rpc reqsponse is ok: {}", response?.is_ok());
/// Ok(())
/// }
/// ```
pub async fn session_stats(&self) -> Result<RpcResponse<SessionStats>> {
self.call(RpcRequest::session_stats()).await
}
/// Performs a torrent get call
/// fileds - if None then ALL fields
/// ids - if None then All items

View File

@ -21,6 +21,7 @@ pub use self::response::Nothing;
pub use self::response::RpcResponse;
pub(crate) use self::response::RpcResponseArgument;
pub use self::response::SessionGet;
pub use self::response::SessionStats;
pub use self::response::Torrent;
pub use self::response::TorrentAdded;
pub use self::response::Torrents;

View File

@ -16,6 +16,13 @@ impl RpcRequest {
}
}
pub fn session_stats() -> RpcRequest {
RpcRequest {
method: String::from("session-stats"),
arguments: None,
}
}
pub fn torrent_get(fields: Option<Vec<TorrentGetField>>, ids: Option<Vec<Id>>) -> RpcRequest {
let string_fields = fields
.unwrap_or(TorrentGetField::all())

View File

@ -27,6 +27,25 @@ pub struct SessionGet {
}
impl RpcResponseArgument for SessionGet {}
#[derive(Deserialize, Debug, Clone)]
pub struct SessionStats {
#[serde(rename = "torrentCount")]
pub torrent_count: i32,
#[serde(rename = "activeTorrentCount")]
pub active_torrent_count: i32,
#[serde(rename = "pausedTorrentCount")]
pub paused_torrent_count: i32,
#[serde(rename = "downloadSpeed")]
pub download_speed: i64,
#[serde(rename = "uploadSpeed")]
pub upload_speed: i64,
#[serde(rename = "current-stats")]
pub current_stats: Stats,
#[serde(rename = "cumulative-stats")]
pub cumulative_stats: Stats,
}
impl RpcResponseArgument for SessionStats {}
#[derive(Deserialize, Debug, RustcEncodable, Clone)]
pub struct Torrent {
#[serde(rename = "addedDate")]
@ -84,6 +103,20 @@ pub struct Torrent {
pub file_stats: Option<Vec<FileStat>>,
}
#[derive(Deserialize, Debug, Clone)]
pub struct Stats {
#[serde(rename = "filesAdded")]
pub files_added: i32,
#[serde(rename = "downloadedBytes")]
pub downloaded_bytes: i64,
#[serde(rename = "uploadedBytes")]
pub uploaded_bytes: i64,
#[serde(rename = "secondsActive")]
pub seconds_active: i64,
#[serde(rename = "sessionCount")]
pub session_count: Option<i32>
}
#[derive(Deserialize, Debug, RustcEncodable)]
pub struct Torrents<T> {
pub torrents: Vec<T>,