Skip to content

Commit 6ace820

Browse files
committed
Add timeout and send_null_body configurations to Builder
1 parent 7e92e05 commit 6ace820

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/lib.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ pub enum Error {
115115
pub struct Builder {
116116
/// Number of DNS worker threads
117117
dns_workers: usize,
118+
119+
/// Request timeout
120+
timeout: Duration,
121+
122+
/// Send null body
123+
send_null_body: bool,
118124
}
119125

120126
impl fmt::Display for Error {
@@ -154,7 +160,11 @@ impl error::Error for Error {
154160

155161
impl Default for Builder {
156162
fn default() -> Self {
157-
Self { dns_workers: 4 }
163+
Self {
164+
dns_workers: 4,
165+
timeout: Duration::from_secs(std::u64::MAX),
166+
send_null_body: true,
167+
}
158168
}
159169
}
160170

@@ -168,6 +178,24 @@ impl Builder {
168178
self
169179
}
170180

181+
/// Set request timeout
182+
///
183+
/// Default is no timeout
184+
#[inline]
185+
pub fn timeout(&mut self, timeout: Duration) -> &mut Self {
186+
self.timeout = timeout;
187+
self
188+
}
189+
190+
/// Send null body in POST/PUT
191+
///
192+
/// Default is yes
193+
#[inline]
194+
pub fn send_null_body(&mut self, value: bool) -> &mut Self {
195+
self.send_null_body = value;
196+
self
197+
}
198+
171199
/// Create `RestClient` with the configuration in this builder
172200
pub fn build(&self, url: &str) -> Result<RestClient, Error> {
173201
RestClient::with_builder(url, self)
@@ -209,8 +237,8 @@ impl RestClient {
209237
baseurl,
210238
auth: None,
211239
headers: HeaderMap::new(),
212-
timeout: Duration::from_secs(std::u64::MAX),
213-
send_null_body: true,
240+
timeout: builder.timeout,
241+
send_null_body: builder.send_null_body,
214242
})
215243
}
216244

tests/get.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ extern crate restson;
44
extern crate serde_derive;
55

66
use restson::{Error, RestClient, RestPath};
7+
use std::time::Duration;
78

89
#[derive(Deserialize)]
910
struct HttpBinAnything {
@@ -50,6 +51,8 @@ fn basic_get_http() {
5051
fn basic_get_builder() {
5152
let mut client = RestClient::builder()
5253
.dns_workers(1)
54+
.timeout(Duration::from_secs(10))
55+
.send_null_body(false)
5356
.build("http://httpbin.org")
5457
.unwrap();
5558

0 commit comments

Comments
 (0)