From 518fa4a3d523b481e2d72e78361dd979e6c850f4 Mon Sep 17 00:00:00 2001 From: Jokler Date: Fri, 11 May 2018 23:16:10 +0200 Subject: Url: Use shorter timeout for title downloads A new function has been added to the Url/download util to allow setting different timeouts. --- src/utils.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'src/utils.rs') 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, + timeout: Option, } impl<'a> From for Url<'a> { @@ -18,6 +20,7 @@ impl<'a> From 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 { - 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() -- cgit v1.2.3-70-g09d2