Skip to main content

qbit_rs/endpoint/
transfer.rs

1use serde::Serialize;
2
3use crate::{Error, Qbit, Result, ext::*, model::*};
4
5impl Qbit {
6    /// Return the global transfer information shown in the qBittorrent status
7    /// bar.
8    pub async fn get_transfer_info(&self) -> Result<TransferInfo> {
9        self.get("transfer/info")
10            .await?
11            .json()
12            .await
13            .map_err(Into::into)
14    }
15
16    /// Return whether alternative speed limits are enabled.
17    pub async fn get_speed_limits_mode(&self) -> Result<bool> {
18        self.get("transfer/speedLimitsMode")
19            .await?
20            .text()
21            .await
22            .map_err(Into::into)
23            .and_then(|s| match s.as_str() {
24                "0" => Ok(false),
25                "1" => Ok(true),
26                _ => Err(Error::BadResponse {
27                    explain: "Received non-number response body on `transfer/speedLimitsMode`",
28                }),
29            })
30    }
31
32    /// Toggle alternative speed limits.
33    pub async fn toggle_speed_limits_mode(&self) -> Result<()> {
34        self.post("transfer/toggleSpeedLimitsMode").await?.end()
35    }
36
37    /// Get global and alternative speed limits (KiB/s, -1 = unlimited).
38    ///
39    /// Added in qBittorrent 5.2.0 (Web API v2.16.0).
40    pub async fn get_speed_limits(&self) -> Result<SpeedLimits> {
41        self.get("transfer/getSpeedLimits")
42            .await?
43            .json()
44            .await
45            .map_err(Into::into)
46    }
47
48    /// Set global and alternative speed limits (KiB/s, -1 = unlimited).
49    ///
50    /// Added in qBittorrent 5.2.0 (Web API v2.16.0).
51    pub async fn set_speed_limits(&self, limits: &SpeedLimits) -> Result<()> {
52        self.post_with("transfer/setSpeedLimits", limits)
53            .await?
54            .end()
55    }
56
57    /// Return the global download limit in bytes per second.
58    pub async fn get_download_limit(&self) -> Result<u64> {
59        self.get("transfer/downloadLimit")
60            .await?
61            .text()
62            .await
63            .map_err(Into::into)
64            .and_then(|s| {
65                s.parse().map_err(|_| Error::BadResponse {
66                    explain: "Received non-number response body on `transfer/downloadLimit`",
67                })
68            })
69    }
70
71    /// Set the global download limit in bytes per second.
72    pub async fn set_download_limit(&self, limit: u64) -> Result<()> {
73        #[derive(Serialize)]
74        struct Arg {
75            limit: u64,
76        }
77
78        self.post_with("transfer/setDownloadLimit", &Arg { limit })
79            .await?
80            .end()
81    }
82
83    /// Return the global upload limit in bytes per second.
84    pub async fn get_upload_limit(&self) -> Result<u64> {
85        self.get("transfer/uploadLimit")
86            .await?
87            .text()
88            .await
89            .map_err(Into::into)
90            .and_then(|s| {
91                s.parse().map_err(|_| Error::BadResponse {
92                    explain: "Received non-number response body on `transfer/uploadLimit`",
93                })
94            })
95    }
96
97    /// Set the global upload limit in bytes per second.
98    pub async fn set_upload_limit(&self, limit: u64) -> Result<()> {
99        #[derive(Serialize)]
100        struct Arg {
101            limit: u64,
102        }
103
104        self.post_with("transfer/setUploadLimit", &Arg { limit })
105            .await?
106            .end()
107    }
108
109    /// Ban the supplied peers, identified by IP address and optional port.
110    pub async fn ban_peers(&self, peers: impl Into<Sep<String, '|'>> + Send + Sync) -> Result<()> {
111        #[derive(Serialize)]
112        struct Arg {
113            peers: String,
114        }
115
116        self.post_with(
117            "transfer/banPeers",
118            &Arg {
119                peers: peers.into().to_string(),
120            },
121        )
122        .await?
123        .end()
124    }
125}