Make timeout configurable

This commit is contained in:
timvisee 2018-11-20 12:42:27 +01:00
parent 0054daadff
commit f693ff42d3
No known key found for this signature in database
GPG key ID: 109CBA0BF74036C2
12 changed files with 99 additions and 22 deletions

View file

@ -2,20 +2,20 @@ use std::time::Duration;
use ffsend_api::reqwest::{Client, ClientBuilder};
use config::{CLIENT_TIMEOUT, CLIENT_TRANSFER_TIMEOUT};
use cmd::matcher::MainMatcher;
/// Create the default client, which is used for generic Send requests.
///
/// Note: use `create_transfer_client()` instead for clients that upload/download.
pub fn create_client() -> Client {
create_custom_client(CLIENT_TIMEOUT)
pub fn create_client(matcher_main: &MainMatcher) -> Client {
create_custom_client(to_duration(matcher_main.timeout()))
}
/// Create the default client, which is used for generic Send requests.
///
/// Note: use `create_transfer_client()` instead for clients that upload/download.
pub fn create_transfer_client() -> Client {
create_custom_client(CLIENT_TRANSFER_TIMEOUT)
pub fn create_transfer_client(matcher_main: &MainMatcher) -> Client {
create_custom_client(to_duration(matcher_main.transfer_timeout()))
}
/// Create the Send client with a custom timeout.
@ -25,3 +25,12 @@ fn create_custom_client(timeout: Option<Duration>) -> Client {
.build()
.expect("failed to build custom reqwest client")
}
/// Convert the given number of seconds into an optional duration, used for clients.
fn to_duration(secs: u64) -> Option<Duration> {
if secs > 0 {
Some(Duration::from_secs(secs))
} else {
None
}
}