refactored library, structurized types, added session-get example

This commit is contained in:
red 2020-04-20 15:32:54 +02:00
parent cb5684337e
commit 5f131322ca
7 changed files with 47 additions and 18 deletions

21
examples/get_session.rs Normal file
View File

@ -0,0 +1,21 @@
extern crate transmission_rpc;
use std::env;
use dotenv::dotenv;
use transmission_rpc::TransClient;
use transmission_rpc::types::{Result, RpcResponse, SessionInfo, BasicAuth};
#[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<SessionInfo>> = client.get_session().await;
match response {
Ok(_) => println!("Yay!"),
Err(_) => panic!("Oh no!")
}
Ok(())
}

View File

@ -12,20 +12,11 @@ extern crate tokio_postgres;
use reqwest::header::CONTENT_TYPE; use reqwest::header::CONTENT_TYPE;
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>; pub mod types;
use types::{Result, RpcResponse, SessionGet, SessionInfo};
use types::BasicAuth;
#[derive(Debug)] pub struct TransClient {
struct BasicAuth {
user: String,
password: String,
}
mod models;
use models::request::SessionGet;
use models::response::RpcResponse;
use models::entity::SessionInfo;
struct TransClient {
url: String, url: String,
auth: Option<BasicAuth> auth: Option<BasicAuth>
} }
@ -73,6 +64,7 @@ impl TransClient {
session_id session_id
} }
pub async fn get_session(&self) -> Result<RpcResponse<SessionInfo>> { pub async fn get_session(&self) -> Result<RpcResponse<SessionInfo>> {
info!("Loaded auth: {:?}", &self.auth); info!("Loaded auth: {:?}", &self.auth);
let rq: reqwest::RequestBuilder = self.rpc_request() let rq: reqwest::RequestBuilder = self.rpc_request()
@ -110,7 +102,7 @@ mod tests {
async fn it_works() -> Result<()> { async fn it_works() -> Result<()> {
dotenv().ok(); dotenv().ok();
env_logger::init(); env_logger::init();
let url= env::var("URL")?; let url= env::var("TURL")?;
let basic_auth = BasicAuth{user: env::var("TUSER")?, password: env::var("TPWD")?}; let basic_auth = BasicAuth{user: env::var("TUSER")?, password: env::var("TPWD")?};
TransClient::with_auth(&url, basic_auth).get_session().await; TransClient::with_auth(&url, basic_auth).get_session().await;
Ok(()) Ok(())

View File

@ -1,4 +0,0 @@
pub mod request;
pub mod response;
pub mod entity;

View File

@ -1,4 +1,11 @@
use serde::Deserialize; use serde::Deserialize;
#[derive(Debug)]
pub struct BasicAuth {
pub user: String,
pub password: String,
}
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct SessionInfo { pub struct SessionInfo {
#[serde(rename="blocklist-enabled")] #[serde(rename="blocklist-enabled")]

13
src/types/mod.rs Normal file
View File

@ -0,0 +1,13 @@
mod request;
mod response;
mod entity;
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
pub(crate) use self::request::SessionGet;
pub use self::response::RpcResponse;
pub use self::entity::BasicAuth;
pub use self::entity::SessionInfo;