1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use std::convert::TryInto;
use std::error::Error;
use serde::Serialize;
mod payload;
use payload::Request;
use payload::Response;
mod list;
use list::FieldList;
use list::TorrentList;
mod torrent;
use torrent::Torrent;
use torrent::AddTorrent;
use torrent::AddTorrentResponse;
#[derive(Clone)]
pub struct Client {
host: String,
port: u16,
tls: bool,
auth: Option<(String, String)>,
base_url: String,
session_id: Option<String>,
http: reqwest::Client,
}
impl Client {
pub fn new(host: impl ToString, port: u16, tls: bool, auth: Option<(String, String)>) -> Result<Self, Box<dyn Error>> {
let mut headers = reqwest::header::HeaderMap::new();
headers.insert(reqwest::header::USER_AGENT, "transmission-rs/0.1".try_into()?);
let this = Self{
host: host.to_string(),
port: port,
tls: tls,
auth: auth,
base_url: {
let protocol = match tls {
true => "https",
false => "http"
};
format!("{}://{}:{}", protocol, host.to_string(), port)
},
session_id: None,
http: reqwest::Client::builder()
.gzip(true)
.default_headers(headers)
.build()?
};
Ok(this)
}
fn build_url(&self, path: impl AsRef<str>) -> String {
format!("{}{}", self.base_url, path.as_ref()).to_string()
}
pub async fn authenticate(&mut self) -> Result<(), Box<dyn Error>> {
let response = self.post("/transmission/rpc/")
.header("Content-Type", "application/x-www-form-urlencoded")
.send()
.await?;
self.session_id = match response.headers().get("X-Transmission-Session-Id") {
Some(v) => Some(v.to_str()?.to_string()),
None => None
};
Ok(())
}
pub fn post(&self, path: impl AsRef<str>) -> reqwest::RequestBuilder {
let url = self.build_url(path);
let mut request = self.http.post(&url);
request = match &self.auth {
Some(auth) => request.basic_auth(&auth.0, Some(&auth.1)),
None => request
};
request = match &self.session_id {
Some(token) => request.header("X-Transmission-Session-Id", token),
None => request
};
request
}
pub async fn rpc(&self, method: impl ToString, tag: u8, body: impl Serialize) -> Result<reqwest::Response, Box<dyn Error>> {
let request = Request::new(method, tag, body);
Ok(self.post("/transmission/rpc/")
.header("Content-Type", "application/x-www-form-urlencoded")
.body(serde_json::to_string(&request)?)
.send().await?
.error_for_status()?
)
}
pub async fn list(&self) -> Result<Vec<Torrent>, Box<dyn Error>> {
let response: Response<TorrentList> = self.rpc("torrent-get", 4, FieldList::new()).await?.error_for_status()?.json().await?;
Ok(response.arguments.torrents)
}
pub async fn add_torrent_from_link(&self, url: impl ToString) -> Result<AddTorrentResponse, Box<dyn Error>> {
let response: Response<AddTorrentResponse> = self.rpc("torrent-add", 8, AddTorrent{filename: url.to_string()}).await?.error_for_status()?.json().await?;
Ok(response.arguments)
}
}