Skip to content

Commit 07364f4

Browse files
committed
refactor: [#142] extract functions in test api Client
1 parent 3422e93 commit 07364f4

File tree

1 file changed

+20
-42
lines changed

1 file changed

+20
-42
lines changed

tests/api/mod.rs

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -133,69 +133,47 @@ impl Client {
133133
}
134134

135135
pub async fn generate_auth_key(&self, seconds_valid: i32) -> AuthKey {
136-
let url = format!(
137-
"http://{}/api/key/{}?token={}",
138-
&self.connection_info.bind_address, &seconds_valid, &self.connection_info.api_token
139-
);
140-
reqwest::Client::new().post(url).send().await.unwrap().json().await.unwrap()
136+
self.post(&format!("key/{}", &seconds_valid)).await.json().await.unwrap()
141137
}
142138

143139
pub async fn whitelist_a_torrent(&self, info_hash: &str) -> Response {
144-
let url = format!(
145-
"http://{}/api/whitelist/{}?token={}",
146-
&self.connection_info.bind_address, &info_hash, &self.connection_info.api_token
147-
);
148-
reqwest::Client::new().post(url.clone()).send().await.unwrap()
140+
self.post(&format!("whitelist/{}", &info_hash)).await
149141
}
150142

151143
pub async fn get_torrent(&self, info_hash: &str) -> Torrent {
152-
let url = format!(
153-
"http://{}/api/torrent/{}?token={}",
154-
&self.connection_info.bind_address, &info_hash, &self.connection_info.api_token
155-
);
156-
reqwest::Client::builder()
157-
.build()
158-
.unwrap()
159-
.get(url)
160-
.send()
144+
self.get(&format!("torrent/{}", &info_hash))
161145
.await
162-
.unwrap()
163146
.json::<Torrent>()
164147
.await
165148
.unwrap()
166149
}
167150

168151
pub async fn get_torrents(&self) -> Vec<torrent::ListItem> {
169-
let url = format!(
170-
"http://{}/api/torrents?token={}",
171-
&self.connection_info.bind_address, &self.connection_info.api_token
172-
);
173-
reqwest::Client::builder()
174-
.build()
175-
.unwrap()
176-
.get(url)
177-
.send()
178-
.await
179-
.unwrap()
180-
.json::<Vec<torrent::ListItem>>()
181-
.await
182-
.unwrap()
152+
self.get("torrents").await.json::<Vec<torrent::ListItem>>().await.unwrap()
183153
}
184154

185155
pub async fn get_tracker_statistics(&self) -> Stats {
186-
let url = format!(
187-
"http://{}/api/stats?token={}",
188-
&self.connection_info.bind_address, &self.connection_info.api_token
189-
);
156+
self.get("stats").await.json::<Stats>().await.unwrap()
157+
}
158+
159+
async fn get(&self, path: &str) -> Response {
190160
reqwest::Client::builder()
191161
.build()
192162
.unwrap()
193-
.get(url)
163+
.get(self.url(path))
194164
.send()
195165
.await
196166
.unwrap()
197-
.json::<Stats>()
198-
.await
199-
.unwrap()
167+
}
168+
169+
async fn post(&self, path: &str) -> Response {
170+
reqwest::Client::new().post(self.url(path).clone()).send().await.unwrap()
171+
}
172+
173+
fn url(&self, path: &str) -> String {
174+
format!(
175+
"http://{}/api/{path}?token={}",
176+
&self.connection_info.bind_address, &self.connection_info.api_token
177+
)
200178
}
201179
}

0 commit comments

Comments
 (0)