This commit is contained in:
red 2020-04-22 14:30:03 +02:00
parent 6185c826c0
commit e27e3b2d04

View File

@ -22,6 +22,7 @@ pub struct TransClient {
} }
impl TransClient { impl TransClient {
/// Returns HTTP(S) client with configured Basic Auth
pub fn with_auth(url: &str, basic_auth: BasicAuth) -> TransClient { pub fn with_auth(url: &str, basic_auth: BasicAuth) -> TransClient {
TransClient { TransClient {
url: url.to_string(), url: url.to_string(),
@ -29,6 +30,7 @@ impl TransClient {
} }
} }
/// Returns HTTP(S) client
pub fn new(url: &str) -> TransClient { pub fn new(url: &str) -> TransClient {
TransClient { TransClient {
url: url.to_string(), url: url.to_string(),
@ -36,6 +38,7 @@ impl TransClient {
} }
} }
/// Prepares a request for provided server and auth
fn rpc_request(&self) -> reqwest::RequestBuilder { fn rpc_request(&self) -> reqwest::RequestBuilder {
let client = reqwest::Client::new(); let client = reqwest::Client::new();
if let Some(auth) = &self.auth { if let Some(auth) = &self.auth {
@ -47,6 +50,12 @@ impl TransClient {
.header(CONTENT_TYPE, "application/json") .header(CONTENT_TYPE, "application/json")
} }
/// Performs session-get call and takes the x-transmission-session-id
/// header to perform calls, using it's value
///
/// # Errors
///
/// Panics if any IO error happens
async fn get_session_id(&self) -> String { async fn get_session_id(&self) -> String {
info!("Requesting session id info"); info!("Requesting session id info");
let response: reqwest::Response = self.rpc_request() let response: reqwest::Response = self.rpc_request()
@ -64,19 +73,50 @@ impl TransClient {
session_id session_id
} }
/// Performs a session get call
///
/// # Errors
///
/// Any IO Error or Deserialization error
///
/// # Example
///
/// in examples/session-get.rs
pub async fn session_get(&self) -> Result<RpcResponse<SessionGet>> { pub async fn session_get(&self) -> Result<RpcResponse<SessionGet>> {
self.call(RpcRequest::session_get()).await self.call(RpcRequest::session_get()).await
} }
/// Performs a torrent get call
///
/// # Errors
///
/// Any IO Error or Deserialization error
///
/// # Example
///
/// in examples/torrent-get.rs
pub async fn torrent_get(&self, fields: Vec<TorrentGetField>) -> Result<RpcResponse<Torrents<Torrent>>> { pub async fn torrent_get(&self, fields: Vec<TorrentGetField>) -> Result<RpcResponse<Torrents<Torrent>>> {
self.call(RpcRequest::torrent_get(fields)).await self.call(RpcRequest::torrent_get(fields)).await
} }
/// Performs a torrent action call
///
/// # Errors
///
/// Any IO Error or Deserialization error
///
/// # Example
///
/// in examples/torrent-action.rs
pub async fn torrent_action(&self, action: TorrentAction, ids: Vec<i64>) -> Result<RpcResponse<Nothing>> { pub async fn torrent_action(&self, action: TorrentAction, ids: Vec<i64>) -> Result<RpcResponse<Nothing>> {
self.call(RpcRequest::torrent_action(action, ids)).await self.call(RpcRequest::torrent_action(action, ids)).await
} }
/// Performs an JRPC call to the server
///
/// # Errors
///
/// Any IO Error or Deserialization error
async fn call<RS> (&self, request: RpcRequest) -> Result<RpcResponse<RS>> async fn call<RS> (&self, request: RpcRequest) -> Result<RpcResponse<RS>>
where RS : RpcResponseArgument + DeserializeOwned + std::fmt::Debug where RS : RpcResponseArgument + DeserializeOwned + std::fmt::Debug
{ {