feat: make sync optional

This commit is contained in:
George Miao 2022-10-31 10:33:56 -04:00 committed by Aleksandr
parent 6791807f3f
commit 69760035f3
3 changed files with 23 additions and 15 deletions

View File

@ -11,6 +11,9 @@ keywords = ["transmission", "torrent", "jrpc"]
categories = ["api-bindings"] categories = ["api-bindings"]
include = ["**/*.rs", "Cargo.toml"] include = ["**/*.rs", "Cargo.toml"]
[features]
sync = []
[dependencies] [dependencies]
reqwest = { version = "0.11.11", features = [ reqwest = { version = "0.11.11", features = [
"json", "json",

View File

@ -4,10 +4,13 @@ extern crate log;
use reqwest::{header::CONTENT_TYPE, Client, StatusCode, Url}; use reqwest::{header::CONTENT_TYPE, Client, StatusCode, Url};
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
#[cfg(feature = "sync")]
mod sync; mod sync;
pub mod types; #[cfg(feature = "sync")]
pub use sync::SharableTransClient; pub use sync::SharableTransClient;
pub mod types;
use types::{ use types::{
BasicAuth, BlocklistUpdate, FreeSpace, Id, Nothing, PortTest, Result, RpcRequest, RpcResponse, BasicAuth, BlocklistUpdate, FreeSpace, Id, Nothing, PortTest, Result, RpcRequest, RpcResponse,
RpcResponseArgument, SessionClose, SessionGet, SessionStats, Torrent, TorrentAction, RpcResponseArgument, SessionClose, SessionGet, SessionStats, Torrent, TorrentAction,

View File

@ -97,7 +97,7 @@ impl SharableTransClient {
/// user: env::var("TUSER")?, /// user: env::var("TUSER")?,
/// password: env::var("TPWD")?, /// password: env::var("TPWD")?,
/// }; /// };
/// let client = TransClient::with_auth(url.parse()?, basic_auth); /// let client = SharableTransClient::with_auth(url.parse()?, basic_auth);
/// let response: Result<RpcResponse<SessionGet>> = client.session_get().await; /// let response: Result<RpcResponse<SessionGet>> = client.session_get().await;
/// match response { /// match response {
/// Ok(_) => println!("Yay!"), /// Ok(_) => println!("Yay!"),
@ -139,7 +139,7 @@ impl SharableTransClient {
/// user: env::var("TUSER")?, /// user: env::var("TUSER")?,
/// password: env::var("TPWD")?, /// password: env::var("TPWD")?,
/// }; /// };
/// let client = TransClient::with_auth(url.parse()?, basic_auth); /// let client = SharableTransClient::with_auth(url.parse()?, basic_auth);
/// let response: Result<RpcResponse<SessionStats>> = client.session_stats().await; /// let response: Result<RpcResponse<SessionStats>> = client.session_stats().await;
/// match response { /// match response {
/// Ok(_) => println!("Yay!"), /// Ok(_) => println!("Yay!"),
@ -181,7 +181,7 @@ impl SharableTransClient {
/// user: env::var("TUSER")?, /// user: env::var("TUSER")?,
/// password: env::var("TPWD")?, /// password: env::var("TPWD")?,
/// }; /// };
/// let client = TransClient::with_auth(url.parse()?, basic_auth); /// let client = SharableTransClient::with_auth(url.parse()?, basic_auth);
/// let response: Result<RpcResponse<SessionClose>> = client.session_close().await; /// let response: Result<RpcResponse<SessionClose>> = client.session_close().await;
/// match response { /// match response {
/// Ok(_) => println!("Yay!"), /// Ok(_) => println!("Yay!"),
@ -223,7 +223,7 @@ impl SharableTransClient {
/// user: env::var("TUSER")?, /// user: env::var("TUSER")?,
/// password: env::var("TPWD")?, /// password: env::var("TPWD")?,
/// }; /// };
/// let client = TransClient::with_auth(url.parse()?, basic_auth); /// let client = SharableTransClient::with_auth(url.parse()?, basic_auth);
/// let response: Result<RpcResponse<BlocklistUpdate>> = client.blocklist_update().await; /// let response: Result<RpcResponse<BlocklistUpdate>> = client.blocklist_update().await;
/// match response { /// match response {
/// Ok(_) => println!("Yay!"), /// Ok(_) => println!("Yay!"),
@ -266,7 +266,7 @@ impl SharableTransClient {
/// user: env::var("TUSER")?, /// user: env::var("TUSER")?,
/// password: env::var("TPWD")?, /// password: env::var("TPWD")?,
/// }; /// };
/// let client = TransClient::with_auth(url.parse()?, basic_auth); /// let client = SharableTransClient::with_auth(url.parse()?, basic_auth);
/// let response: Result<RpcResponse<FreeSpace>> = client.free_space(dir).await; /// let response: Result<RpcResponse<FreeSpace>> = client.free_space(dir).await;
/// match response { /// match response {
/// Ok(_) => println!("Yay!"), /// Ok(_) => println!("Yay!"),
@ -308,7 +308,7 @@ impl SharableTransClient {
/// user: env::var("TUSER")?, /// user: env::var("TUSER")?,
/// password: env::var("TPWD")?, /// password: env::var("TPWD")?,
/// }; /// };
/// let client = TransClient::with_auth(url.parse()?, basic_auth); /// let client = SharableTransClient::with_auth(url.parse()?, basic_auth);
/// let response: Result<RpcResponse<PortTest>> = client.port_test().await; /// let response: Result<RpcResponse<PortTest>> = client.port_test().await;
/// match response { /// match response {
/// Ok(_) => println!("Yay!"), /// Ok(_) => println!("Yay!"),
@ -352,7 +352,7 @@ impl SharableTransClient {
/// user: env::var("TUSER")?, /// user: env::var("TUSER")?,
/// password: env::var("TPWD")?, /// password: env::var("TPWD")?,
/// }; /// };
/// let client = TransClient::with_auth(url.parse()?, basic_auth); /// let client = SharableTransClient::with_auth(url.parse()?, basic_auth);
/// ///
/// let res: RpcResponse<Torrents<Torrent>> = client.torrent_get(None, None).await?; /// let res: RpcResponse<Torrents<Torrent>> = client.torrent_get(None, None).await?;
/// let names: Vec<&String> = res /// let names: Vec<&String> = res
@ -452,7 +452,7 @@ impl SharableTransClient {
/// user: env::var("TUSER")?, /// user: env::var("TUSER")?,
/// password: env::var("TPWD")?, /// password: env::var("TPWD")?,
/// }; /// };
/// let client = TransClient::with_auth(url, basic_auth); /// let client = SharableTransClient::with_auth(url, basic_auth);
/// ///
/// let args = TorrentSetArgs { /// let args = TorrentSetArgs {
/// labels: Some(vec![String::from("blue")]), /// labels: Some(vec![String::from("blue")]),
@ -504,7 +504,7 @@ impl SharableTransClient {
/// user: env::var("TUSER")?, /// user: env::var("TUSER")?,
/// password: env::var("TPWD")?, /// password: env::var("TPWD")?,
/// }; /// };
/// let client = TransClient::with_auth(url.parse()?, basic_auth); /// let client = SharableTransClient::with_auth(url.parse()?, basic_auth);
/// let res1: RpcResponse<Nothing> = client /// let res1: RpcResponse<Nothing> = client
/// .torrent_action(TorrentAction::Start, vec![Id::Id(1)]) /// .torrent_action(TorrentAction::Start, vec![Id::Id(1)])
/// .await?; /// .await?;
@ -553,7 +553,7 @@ impl SharableTransClient {
/// user: env::var("TUSER")?, /// user: env::var("TUSER")?,
/// password: env::var("TPWD")?, /// password: env::var("TPWD")?,
/// }; /// };
/// let client = TransClient::with_auth(url.parse()?, basic_auth); /// let client = SharableTransClient::with_auth(url.parse()?, basic_auth);
/// let res: RpcResponse<Nothing> = client.torrent_remove(vec![Id::Id(1)], false).await?; /// let res: RpcResponse<Nothing> = client.torrent_remove(vec![Id::Id(1)], false).await?;
/// println!("Remove result: {:?}", &res.is_ok()); /// println!("Remove result: {:?}", &res.is_ok());
/// ///
@ -597,7 +597,7 @@ impl SharableTransClient {
/// user: env::var("TUSER")?, /// user: env::var("TUSER")?,
/// password: env::var("TPWD")?, /// password: env::var("TPWD")?,
/// }; /// };
/// let client = TransClient::with_auth(url.parse()?, basic_auth); /// let client = SharableTransClient::with_auth(url.parse()?, basic_auth);
/// let res: RpcResponse<Nothing> = client /// let res: RpcResponse<Nothing> = client
/// .torrent_set_location( /// .torrent_set_location(
/// vec![Id::Id(1)], /// vec![Id::Id(1)],
@ -648,7 +648,7 @@ impl SharableTransClient {
/// user: env::var("TUSER")?, /// user: env::var("TUSER")?,
/// password: env::var("TPWD")?, /// password: env::var("TPWD")?,
/// }; /// };
/// let client = TransClient::with_auth(url.parse()?, basic_auth); /// let client = SharableTransClient::with_auth(url.parse()?, basic_auth);
/// let res: RpcResponse<TorrentRenamePath> = client /// let res: RpcResponse<TorrentRenamePath> = client
/// .torrent_rename_path( /// .torrent_rename_path(
/// vec![Id::Id(1)], /// vec![Id::Id(1)],
@ -699,7 +699,7 @@ impl SharableTransClient {
/// user: env::var("TUSER")?, /// user: env::var("TUSER")?,
/// password: env::var("TPWD")?, /// password: env::var("TPWD")?,
/// }; /// };
/// let client = TransClient::with_auth(url.parse()?, basic_auth); /// let client = SharableTransClient::with_auth(url.parse()?, basic_auth);
/// let add: TorrentAddArgs = TorrentAddArgs { /// let add: TorrentAddArgs = TorrentAddArgs {
/// filename: Some( /// filename: Some(
/// "https://releases.ubuntu.com/jammy/ubuntu-22.04.1-desktop-amd64.iso.torrent" /// "https://releases.ubuntu.com/jammy/ubuntu-22.04.1-desktop-amd64.iso.torrent"
@ -758,6 +758,8 @@ impl SharableTransClient {
); );
let rsp: reqwest::Response = rq.send().await?; let rsp: reqwest::Response = rq.send().await?;
info!("Response: {:?}", &rsp);
if matches!(rsp.status(), StatusCode::CONFLICT) { if matches!(rsp.status(), StatusCode::CONFLICT) {
let session_id = rsp let session_id = rsp
.headers() .headers()