Add port-test.

This commit is contained in:
Quang Ngô 2021-11-08 21:17:26 +07:00 committed by Aleksandr
parent 301a0c43c1
commit 13c0d50ef8
6 changed files with 83 additions and 13 deletions

View File

@ -30,7 +30,7 @@ spec: https://github.com/transmission/transmission/blob/master/extras/rpc-spec.t
- [X] session-get - [X] session-get
- [X] session-stats - [X] session-stats
- [X] blocklist-update - [X] blocklist-update
- [ ] port-test - [X] port-test
- [ ] session-close - [ ] session-close
- [ ] free-space - [ ] free-space

26
examples/port-test.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, PortTest};
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<PortTest>> = client.port_test().await;
match response {
Ok(_) => println!("Yay!"),
Err(_) => panic!("Oh no!")
}
println!("Rpc response is ok: {}", response?.is_ok());
Ok(())
}

View File

@ -12,6 +12,7 @@ use types::BasicAuth;
use types::BlocklistUpdate; use types::BlocklistUpdate;
use types::SessionGet; use types::SessionGet;
use types::SessionStats; use types::SessionStats;
use types::PortTest;
use types::TorrentAction; use types::TorrentAction;
use types::{Id, Torrent, TorrentGetField, Torrents}; use types::{Id, Torrent, TorrentGetField, Torrents};
use types::{Nothing, Result, RpcRequest, RpcResponse, RpcResponseArgument, TorrentRenamePath}; use types::{Nothing, Result, RpcRequest, RpcResponse, RpcResponseArgument, TorrentRenamePath};
@ -185,6 +186,42 @@ impl TransClient {
self.call(RpcRequest::blocklist_update()).await self.call(RpcRequest::blocklist_update()).await
} }
/// Performs a port test 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, PortTest};
///
/// #[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<PortTest>> = client.port_test().await;
/// match response {
/// Ok(_) => println!("Yay!"),
/// Err(_) => panic!("Oh no!")
/// }
/// println!("Rpc reqsponse is ok: {}", response?.is_ok());
/// Ok(())
/// }
/// ```
pub async fn port_test(&self) -> Result<RpcResponse<PortTest>> {
self.call(RpcRequest::port_test()).await
}
/// Performs a torrent get call /// Performs a torrent get call
/// fileds - if None then ALL fields /// fileds - if None then ALL fields
/// ids - if None then All items /// ids - if None then All items
@ -464,18 +501,10 @@ impl TransClient {
.expect("Unable to get the request body") .expect("Unable to get the request body")
.body_string()? .body_string()?
); );
let http_resp: reqwest::Response = rq.send().await?; let resp: reqwest::Response = rq.send().await?;
match http_resp.error_for_status() { let rpc_response: RpcResponse<RS> = resp.json().await?;
Ok(http_resp) => { info!("Response body: {:#?}", rpc_response);
let rpc_resp: RpcResponse<RS> = http_resp.json().await?; Ok(rpc_response)
info!("Response body: {:#?}", rpc_resp);
Ok(rpc_resp)
}
Err(err) => {
error!("{}", err.to_string());
Err(err.into())
}
}
} }
} }

View File

@ -23,6 +23,7 @@ pub(crate) use self::response::RpcResponseArgument;
pub use self::response::SessionGet; pub use self::response::SessionGet;
pub use self::response::SessionStats; pub use self::response::SessionStats;
pub use self::response::BlocklistUpdate; pub use self::response::BlocklistUpdate;
pub use self::response::PortTest;
pub use self::response::Torrent; pub use self::response::Torrent;
pub use self::response::TorrentAdded; pub use self::response::TorrentAdded;
pub use self::response::Torrents; pub use self::response::Torrents;

View File

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

View File

@ -53,6 +53,13 @@ pub struct BlocklistUpdate {
} }
impl RpcResponseArgument for BlocklistUpdate {} impl RpcResponseArgument for BlocklistUpdate {}
#[derive(Deserialize, Debug, Clone)]
pub struct PortTest {
#[serde(rename = "port-is-open")]
pub port_is_open: bool,
}
impl RpcResponseArgument for PortTest {}
#[derive(Deserialize, Debug, RustcEncodable, Clone)] #[derive(Deserialize, Debug, RustcEncodable, Clone)]
pub struct Torrent { pub struct Torrent {
#[serde(rename = "addedDate")] #[serde(rename = "addedDate")]