Skip to content

Commit 5a854f9

Browse files
committedFeb 9, 2024
Merge #482: Index API client: add timeout to requests
47f5242 chore: fix comment (Jose Celano) 3d3fc2d chore: add todo to seeder (Jose Celano) 2fc2016 refactor: [#472] move unwraps from API client up (Jose Celano) 1015945 chore: remove comment (Jose Celano) 564df07 docs: move comment (Jose Celano) a8c2f84 refactor: [#472] move unwraps from generic HTTP client up to Index API client (Jose Celano) 26422fc feat: [#472] add timeout to API client requests (Jose Celano) 9ff4de4 feat: [#472] inject URL scheme in API client (Jose Celano) 2cf5f47 refactor: [#472] move generic HTTP client (Jose Celano) Pull request description: The API client does not have any timeout. The client waits indefinitely for responses. That should not be the default behaviour. It should return an error after a timeout. The users using the client should handle the timeout or any other error. ### Subtasks - [x] Move generic HTTP client to HTTP mod. - [x] Generic HTTP client: Inject the scheme part of the URL in the constructor. - [x] Add timeouts to the generic HTTP client. - [x] Remove `unwraps` from the generic HTTP client. Return errors. - [x] Remove `unwraps` from the Index API HTTP client. Return errors. ACKs for top commit: josecelano: ACK 47f5242 Tree-SHA512: 59cc901b58c19aff0f5258a5297d722389beb9d1ac38838fc470e9cc3e2159abc594cb05db7c1403e2111477149c1834a68f182425cbc101bc0c29c2d0b76613
2 parents 465c853 + 47f5242 commit 5a854f9

File tree

7 files changed

+506
-241
lines changed

7 files changed

+506
-241
lines changed
 

‎src/console/commands/seeder/api.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ pub async fn upload_torrent(client: &Client, upload_torrent_form: UploadTorrentM
3535
add_category(client, &upload_torrent_form.category).await;
3636
}
3737

38-
let response = client.upload_torrent(upload_torrent_form.into()).await;
38+
// todo: if we receive timeout error we should retry later. Otherwise we
39+
// have to restart the seeder manually.
40+
41+
let response = client
42+
.upload_torrent(upload_torrent_form.into())
43+
.await
44+
.expect("API should return a response");
3945

4046
debug!(target:"seeder", "response: {}", response.status);
4147

@@ -68,7 +74,8 @@ pub async fn login(client: &Client, username: &str, password: &str) -> LoggedInU
6874
login: username.to_owned(),
6975
password: password.to_owned(),
7076
})
71-
.await;
77+
.await
78+
.expect("API should return a response");
7279

7380
let res: SuccessfulLoginResponse = serde_json::from_str(&response.body).unwrap_or_else(|_| {
7481
panic!(
@@ -86,21 +93,26 @@ pub async fn login(client: &Client, username: &str, password: &str) -> LoggedInU
8693
///
8794
/// Panics if the response body is not a valid JSON.
8895
pub async fn get_categories(client: &Client) -> Vec<ListItem> {
89-
let response = client.get_categories().await;
96+
let response = client.get_categories().await.expect("API should return a response");
9097

9198
let res: ListResponse = serde_json::from_str(&response.body).unwrap();
9299

93100
res.data
94101
}
95102

96103
/// It adds a new category.
104+
///
105+
/// # Panics
106+
///
107+
/// Will panic if it doesn't get a response form the API.
97108
pub async fn add_category(client: &Client, name: &str) -> TextResponse {
98109
client
99110
.add_category(AddCategoryForm {
100111
name: name.to_owned(),
101112
icon: None,
102113
})
103114
.await
115+
.expect("API should return a response")
104116
}
105117

106118
/// It checks if the category list contains the given category.

‎src/console/commands/seeder/app.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//!
1616
//! ```text
1717
//! cargo run --bin seeder -- \
18-
//! --api-base-url "localhost:3001" \
18+
//! --api-base-url "http://localhost:3001" \
1919
//! --number-of-torrents 1000 \
2020
//! --user admin \
2121
//! --password 12345678 \
@@ -127,12 +127,14 @@
127127
//!
128128
//! As you can see the `info` dictionary is exactly the same, which produces
129129
//! the same info-hash for the torrent.
130+
use std::str::FromStr;
130131
use std::thread::sleep;
131132
use std::time::Duration;
132133

133134
use anyhow::Context;
134135
use clap::Parser;
135136
use log::{debug, info, LevelFilter};
137+
use reqwest::Url;
136138
use text_colorizer::Colorize;
137139
use uuid::Uuid;
138140

@@ -173,9 +175,11 @@ pub async fn run() -> anyhow::Result<()> {
173175

174176
let args = Args::parse();
175177

176-
let api_user = login_index_api(&args.api_base_url, &args.user, &args.password).await;
178+
let api_url = Url::from_str(&args.api_base_url).context("failed to parse API base URL")?;
177179

178-
let api_client = Client::authenticated(&args.api_base_url, &api_user.token);
180+
let api_user = login_index_api(&api_url, &args.user, &args.password).await;
181+
182+
let api_client = Client::authenticated(&api_url, &api_user.token);
179183

180184
info!(target:"seeder", "Uploading { } random torrents to the Torrust Index with a { } seconds interval...", args.number_of_torrents.to_string().yellow(), args.interval.to_string().yellow());
181185

@@ -202,7 +206,7 @@ pub async fn run() -> anyhow::Result<()> {
202206
}
203207

204208
/// It logs in a user in the Index API.
205-
pub async fn login_index_api(api_url: &str, username: &str, password: &str) -> LoggedInUserData {
209+
pub async fn login_index_api(api_url: &Url, username: &str, password: &str) -> LoggedInUserData {
206210
let unauthenticated_client = Client::unauthenticated(api_url);
207211

208212
info!(target:"seeder", "Trying to login with username: {} ...", username.yellow());

0 commit comments

Comments
 (0)