1use super::json::{JsonResult, Params, Request, Response, Statistics, Status};
2pub struct Http {
3 client: reqwest::Client,
4 token: String,
5}
6
7impl Http {
8 pub fn new(token: &str) -> Self {
9 Http {
10 client: reqwest::Client::new(),
11 token: token.to_owned(),
12 }
13 }
14 fn create_request(method: &str, params: Params) -> String {
15 let request = Request {
16 jsonrpc: "2.0".to_owned(),
17 id: "".to_owned(),
18 method: format!("aria2.{}", method),
19 params,
20 };
21 serde_json::to_string(&request).unwrap()
22 }
23 pub async fn call_method(&self, method: &str, params: Params) -> JsonResult {
24 let request = Self::create_request(method, params);
25 let res = self
26 .client
27 .post("http://localhost:6800/jsonrpc")
28 .body(request)
29 .send()
30 .await
31 .unwrap();
32 let res = res.text().await.unwrap();
33 let res: Response = serde_json::from_str(&res).unwrap();
34 res.result
35 }
36 pub async fn change_global_option(&self, params: Params) {
37 self.call_method("changeGlobalOption", params).await;
38 }
39 pub async fn add_uri(&self, uri: &str) -> String {
40 let result = self
41 .call_method(
42 "addUri",
43 Params::SV(self.token.clone(), vec![uri.to_owned()]),
44 )
45 .await;
46 match result {
47 JsonResult::S(s) => s,
48 _ => panic!(),
49 }
50 }
51 pub async fn add_torrent(&self, torrent: String) -> String {
52 let result = self
53 .call_method("addTorrent", Params::V(vec![self.token.clone(), torrent]))
54 .await;
55 match result {
56 JsonResult::S(s) => s,
57 _ => panic!(),
58 }
59 }
60 pub async fn tell_status(&self, gid: &str) -> Status {
61 let result = self
62 .call_method(
63 "tellStatus",
64 Params::V(vec![self.token.clone(), gid.to_owned()]),
65 )
66 .await;
67 match result {
68 JsonResult::Status(status) => status,
69 _ => panic!(),
70 }
71 }
72 pub async fn tell_active(&self) -> Vec<Status> {
73 let result = self
74 .call_method("tellActive", Params::V(vec![self.token.clone()]))
75 .await;
76 match result {
77 JsonResult::StatusList(status_list) => status_list,
78 _ => panic!(),
79 }
80 }
81 pub async fn tell_waiting(&self) -> Vec<Status> {
82 let result = self
83 .call_method("tellWaiting", Params::SUU(self.token.clone(), 0, 20))
84 .await;
85 match result {
86 JsonResult::StatusList(status_list) => status_list,
87 _ => panic!(),
88 }
89 }
90 pub async fn tell_stopped(&self) -> Vec<Status> {
91 let result = self
92 .call_method("tellStopped", Params::SUU(self.token.clone(), 0, 20))
93 .await;
94 match result {
95 JsonResult::StatusList(status_list) => status_list,
96 _ => panic!(),
97 }
98 }
99 pub async fn pause(&self, gid: &str) {
100 self.call_method("pause", Params::V(vec![self.token.clone(), gid.to_owned()]))
101 .await;
102 }
103 pub async fn unpause(&self, gid: &str) {
104 self.call_method(
105 "unpause",
106 Params::V(vec![self.token.clone(), gid.to_owned()]),
107 )
108 .await;
109 }
110 pub async fn pause_all(&self) {
111 self.call_method("pauseAll", Params::V(vec![self.token.clone()]))
112 .await;
113 }
114 pub async fn unpause_all(&self) {
115 self.call_method("unpauseAll", Params::V(vec![self.token.clone()]))
116 .await;
117 }
118 pub async fn remove(&self, gid: &str) {
119 self.call_method(
120 "remove",
121 Params::V(vec![self.token.clone(), gid.to_owned()]),
122 )
123 .await;
124 }
125 pub async fn get_global_stat(&self) -> Statistics {
126 let result = self
127 .call_method("getGlobalStat", Params::V(vec![self.token.clone()]))
128 .await;
129 match result {
130 JsonResult::Statistics(statistics) => statistics,
131 _ => panic!(),
132 }
133 }
134 pub async fn shutdown(&self) {
135 self.call_method("shutdown", Params::V(vec![self.token.clone()]))
136 .await;
137 }
138}