diff options
| author | Jokler <jokler.contact@gmail.com> | 2018-05-11 23:16:10 +0200 |
|---|---|---|
| committer | Jokler <jokler.contact@gmail.com> | 2018-05-11 23:16:10 +0200 |
| commit | 518fa4a3d523b481e2d72e78361dd979e6c850f4 (patch) | |
| tree | 54cb45f2047038c80094b0522329d5f4cf75d514 /src/utils.rs | |
| parent | 01d0fe59935c8465a92a2508049f58292545a99f (diff) | |
| download | frippy-518fa4a3d523b481e2d72e78361dd979e6c850f4.tar.gz frippy-518fa4a3d523b481e2d72e78361dd979e6c850f4.zip | |
Url: Use shorter timeout for title downloads
A new function has been added to the Url/download util to allow
setting different timeouts.
Diffstat (limited to 'src/utils.rs')
| -rw-r--r-- | src/utils.rs | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/src/utils.rs b/src/utils.rs index 6614095..e64b329 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,8 +1,9 @@ use std::borrow::Cow; use std::io::{self, Read}; +use std::time::Duration; -use reqwest::Client; use reqwest::header::Connection; +use reqwest::{Client, ClientBuilder}; use self::error::{DownloadError, ErrorKind}; use failure::ResultExt; @@ -11,6 +12,7 @@ use failure::ResultExt; pub struct Url<'a> { url: Cow<'a, str>, max_kib: Option<usize>, + timeout: Option<Duration>, } impl<'a> From<String> for Url<'a> { @@ -18,6 +20,7 @@ impl<'a> From<String> for Url<'a> { Url { url: Cow::from(url), max_kib: None, + timeout: None, } } } @@ -27,6 +30,7 @@ impl<'a> From<&'a str> for Url<'a> { Url { url: Cow::from(url), max_kib: None, + timeout: None, } } } @@ -37,13 +41,27 @@ impl<'a> Url<'a> { self } + pub fn timeout(mut self, timeout: Duration) -> Self { + self.timeout = Some(timeout); + self + } + /// Downloads the file and converts it to a String. /// Any invalid bytes are converted to a replacement character. /// /// The error indicated either a failed download or /// that the limit set by max_kib() was reached. pub fn request(&self) -> Result<String, DownloadError> { - let mut response = Client::new() + let client = if let Some(timeout) = self.timeout { + ClientBuilder::new() + .timeout(timeout) + .build() + .unwrap() + } else { + Client::new() + }; + + let mut response = client .get(self.url.as_ref()) .header(Connection::close()) .send() |
