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
use super::json::{JsonResult, Params, Request, Response, Statistics, Status};
pub struct Http {
    client: reqwest::Client,
    token: String,
}

impl Http {
    pub fn new(token: &str) -> Self {
        Http {
            client: reqwest::Client::new(),
            token: token.to_owned(),
        }
    }
    fn create_request(method: &str, params: Params) -> String {
        let request = Request {
            jsonrpc: "2.0".to_owned(),
            id: "".to_owned(),
            method: format!("aria2.{}", method),
            params,
        };
        serde_json::to_string(&request).unwrap()
    }
    pub async fn call_method(&self, method: &str, params: Params) -> JsonResult {
        let request = Self::create_request(method, params);
        let res = self
            .client
            .post("http://localhost:6800/jsonrpc")
            .body(request)
            .send()
            .await
            .unwrap();
        let res = res.text().await.unwrap();
        let res: Response = serde_json::from_str(&res).unwrap();
        res.result
    }
    pub async fn change_global_option(&self, params: Params) {
        self.call_method("changeGlobalOption", params).await;
    }
    pub async fn add_uri(&self, uri: &str) -> String {
        let result = self
            .call_method(
                "addUri",
                Params::SV(self.token.clone(), vec![uri.to_owned()]),
            )
            .await;
        match result {
            JsonResult::S(s) => s,
            _ => panic!(),
        }
    }
    pub async fn add_torrent(&self, torrent: String) -> String {
        let result = self
            .call_method("addTorrent", Params::V(vec![self.token.clone(), torrent]))
            .await;
        match result {
            JsonResult::S(s) => s,
            _ => panic!(),
        }
    }
    pub async fn tell_status(&self, gid: &str) -> Status {
        let result = self
            .call_method(
                "tellStatus",
                Params::V(vec![self.token.clone(), gid.to_owned()]),
            )
            .await;
        match result {
            JsonResult::Status(status) => status,
            _ => panic!(),
        }
    }
    pub async fn tell_active(&self) -> Vec<Status> {
        let result = self
            .call_method("tellActive", Params::V(vec![self.token.clone()]))
            .await;
        match result {
            JsonResult::StatusList(status_list) => status_list,
            _ => panic!(),
        }
    }
    pub async fn tell_waiting(&self) -> Vec<Status> {
        let result = self
            .call_method("tellWaiting", Params::SUU(self.token.clone(), 0, 20))
            .await;
        match result {
            JsonResult::StatusList(status_list) => status_list,
            _ => panic!(),
        }
    }
    pub async fn tell_stopped(&self) -> Vec<Status> {
        let result = self
            .call_method("tellStopped", Params::SUU(self.token.clone(), 0, 20))
            .await;
        match result {
            JsonResult::StatusList(status_list) => status_list,
            _ => panic!(),
        }
    }
    pub async fn pause(&self, gid: &str) {
        self.call_method("pause", Params::V(vec![self.token.clone(), gid.to_owned()]))
            .await;
    }
    pub async fn unpause(&self, gid: &str) {
        self.call_method(
            "unpause",
            Params::V(vec![self.token.clone(), gid.to_owned()]),
        )
        .await;
    }
    pub async fn pause_all(&self) {
        self.call_method("pauseAll", Params::V(vec![self.token.clone()]))
            .await;
    }
    pub async fn unpause_all(&self) {
        self.call_method("unpauseAll", Params::V(vec![self.token.clone()]))
            .await;
    }
    pub async fn remove(&self, gid: &str) {
        self.call_method(
            "remove",
            Params::V(vec![self.token.clone(), gid.to_owned()]),
        )
        .await;
    }
    pub async fn get_global_stat(&self) -> Statistics {
        let result = self
            .call_method("getGlobalStat", Params::V(vec![self.token.clone()]))
            .await;
        match result {
            JsonResult::Statistics(statistics) => statistics,
            _ => panic!(),
        }
    }
    pub async fn shutdown(&self) {
        self.call_method("shutdown", Params::V(vec![self.token.clone()]))
            .await;
    }
}