Skip to content

Commit

Permalink
Add port-test.
Browse files Browse the repository at this point in the history
  • Loading branch information
ngoquang2708 committed Nov 8, 2021
1 parent 018d94f commit 86190a9
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ spec: https://github.com/transmission/transmission/blob/master/extras/rpc-spec.t
- [X] session-get
- [X] session-stats
- [ ] blocklist-update
- [ ] port-test
- [X] port-test
- [ ] session-close
- [ ] free-space

Expand Down
26 changes: 26 additions & 0 deletions examples/port-test.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
37 changes: 37 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod types;
use types::BasicAuth;
use types::SessionGet;
use types::SessionStats;
use types::PortTest;
use types::TorrentAction;
use types::{Id, Torrent, TorrentGetField, Torrents};
use types::{Nothing, Result, RpcRequest, RpcResponse, RpcResponseArgument, TorrentRenamePath};
Expand Down Expand Up @@ -148,6 +149,42 @@ impl TransClient {
self.call(RpcRequest::session_stats()).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
/// fileds - if None then ALL fields
/// ids - if None then All items
Expand Down
1 change: 1 addition & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ 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::PortTest;
pub use self::response::Torrent;
pub use self::response::TorrentAdded;
pub use self::response::Torrents;
Expand Down
7 changes: 7 additions & 0 deletions src/types/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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 {
let string_fields = fields
.unwrap_or(TorrentGetField::all())
Expand Down
7 changes: 7 additions & 0 deletions src/types/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ pub struct SessionStats {
}
impl RpcResponseArgument for SessionStats {}

#[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)]
pub struct Torrent {
#[serde(rename = "addedDate")]
Expand Down

0 comments on commit 86190a9

Please sign in to comment.