mirror of
https://github.com/kristoferssolo/transmission-rpc.git
synced 2025-10-21 20:10:37 +00:00
refactored library, structurized types, added session-get example
This commit is contained in:
parent
cb5684337e
commit
5f131322ca
21
examples/get_session.rs
Normal file
21
examples/get_session.rs
Normal 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(())
|
||||
}
|
||||
20
src/lib.rs
20
src/lib.rs
@ -12,20 +12,11 @@ extern crate tokio_postgres;
|
||||
|
||||
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)]
|
||||
struct BasicAuth {
|
||||
user: String,
|
||||
password: String,
|
||||
}
|
||||
|
||||
mod models;
|
||||
use models::request::SessionGet;
|
||||
use models::response::RpcResponse;
|
||||
use models::entity::SessionInfo;
|
||||
|
||||
struct TransClient {
|
||||
pub struct TransClient {
|
||||
url: String,
|
||||
auth: Option<BasicAuth>
|
||||
}
|
||||
@ -73,6 +64,7 @@ impl TransClient {
|
||||
session_id
|
||||
}
|
||||
|
||||
|
||||
pub async fn get_session(&self) -> Result<RpcResponse<SessionInfo>> {
|
||||
info!("Loaded auth: {:?}", &self.auth);
|
||||
let rq: reqwest::RequestBuilder = self.rpc_request()
|
||||
@ -110,7 +102,7 @@ mod tests {
|
||||
async fn it_works() -> Result<()> {
|
||||
dotenv().ok();
|
||||
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")?};
|
||||
TransClient::with_auth(&url, basic_auth).get_session().await;
|
||||
Ok(())
|
||||
|
||||
@ -1,4 +0,0 @@
|
||||
|
||||
pub mod request;
|
||||
pub mod response;
|
||||
pub mod entity;
|
||||
@ -1,4 +1,11 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BasicAuth {
|
||||
pub user: String,
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct SessionInfo {
|
||||
#[serde(rename="blocklist-enabled")]
|
||||
13
src/types/mod.rs
Normal file
13
src/types/mod.rs
Normal 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;
|
||||
Loading…
Reference in New Issue
Block a user