1use std::{
2 borrow::Borrow,
3 path::{Path, PathBuf},
4};
5
6use serde::Serialize;
7
8use crate::{Error, Qbit, Result, ext::*, model::*};
9
10impl Qbit {
11 pub async fn get_version(&self) -> Result<String> {
13 self.get("app/version")
14 .await?
15 .text()
16 .await
17 .map_err(Into::into)
18 }
19
20 pub async fn get_webapi_version(&self) -> Result<String> {
22 self.get("app/webapiVersion")
23 .await?
24 .text()
25 .await
26 .map_err(Into::into)
27 }
28
29 pub async fn get_build_info(&self) -> Result<BuildInfo> {
31 self.get("app/buildInfo")
32 .await?
33 .json()
34 .await
35 .map_err(Into::into)
36 }
37
38 pub async fn get_process_info(&self) -> Result<ProcessInfo> {
42 self.get("app/processInfo")
43 .await?
44 .json()
45 .await
46 .map_err(Into::into)
47 }
48
49 pub async fn get_cookies(&self) -> Result<Vec<CookieEntry>> {
53 self.get("app/cookies")
54 .await?
55 .json()
56 .await
57 .map_err(Into::into)
58 }
59
60 pub async fn set_cookies(&self, cookies: &[SetCookieArg]) -> Result<()> {
64 #[derive(Serialize)]
65 struct Arg<'a> {
66 cookies: &'a str,
67 }
68 let json = serde_json::to_string(cookies)?;
69 self.post_with("app/setCookies", &Arg { cookies: &json })
70 .await?
71 .end()
72 }
73
74 pub async fn shutdown(&self) -> Result<()> {
76 self.post("app/shutdown").await?.end()
77 }
78
79 pub async fn get_preferences(&self) -> Result<Preferences> {
81 self.get("app/preferences")
82 .await?
83 .json()
84 .await
85 .map_err(Into::into)
86 }
87
88 pub async fn set_preferences(
90 &self,
91 preferences: impl Borrow<Preferences> + Send + Sync,
92 ) -> Result<()> {
93 #[derive(Serialize)]
94 struct Arg {
95 json: String,
96 }
97
98 self.post_with(
99 "app/setPreferences",
100 &Arg {
101 json: serde_json::to_string(preferences.borrow())?,
102 },
103 )
104 .await?
105 .end()
106 }
107
108 pub async fn get_free_space_at_path(
112 &self,
113 path: impl AsRef<Path> + Send + Sync,
114 ) -> Result<u64> {
115 #[derive(Serialize)]
116 struct Arg<'a> {
117 path: &'a Path,
118 }
119 self.get_with(
120 "app/getFreeSpaceAtPathAction",
121 &Arg {
122 path: path.as_ref(),
123 },
124 )
125 .await?
126 .text()
127 .await
128 .map_err(Into::into)
129 .and_then(|s| {
130 s.parse::<u64>().map_err(|_| Error::BadResponse {
131 explain: "getFreeSpaceAtPathAction returned non-numeric response",
132 })
133 })
134 }
135
136 pub async fn get_default_save_path(&self) -> Result<PathBuf> {
138 self.get("app/defaultSavePath")
139 .await?
140 .text()
141 .await
142 .map_err(Into::into)
143 .map(PathBuf::from)
144 }
145}