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 301a0c4 commit b8d6026
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 13 deletions.
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
- [X] 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(())
}
53 changes: 41 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use types::BasicAuth;
use types::BlocklistUpdate;
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 @@ -185,6 +186,42 @@ impl TransClient {
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
/// fileds - if None then ALL fields
/// ids - if None then All items
Expand Down Expand Up @@ -464,18 +501,10 @@ impl TransClient {
.expect("Unable to get the request body")
.body_string()?
);
let http_resp: reqwest::Response = rq.send().await?;
match http_resp.error_for_status() {
Ok(http_resp) => {
let rpc_resp: RpcResponse<RS> = http_resp.json().await?;
info!("Response body: {:#?}", rpc_resp);
Ok(rpc_resp)
}
Err(err) => {
error!("{}", err.to_string());
Err(err.into())
}
}
let resp: reqwest::Response = rq.send().await?;
let rpc_response: RpcResponse<RS> = resp.json().await?;
info!("Response body: {:#?}", rpc_response);
Ok(rpc_response)
}
}

Expand Down
1 change: 1 addition & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub(crate) use self::response::RpcResponseArgument;
pub use self::response::SessionGet;
pub use self::response::SessionStats;
pub use self::response::BlocklistUpdate;
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 @@ -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 {
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 @@ -53,6 +53,13 @@ pub struct 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)]
pub struct Torrent {
#[serde(rename = "addedDate")]
Expand Down

0 comments on commit b8d6026

Please sign in to comment.