Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ehttp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
///
/// `Err` can happen for a number of reasons:
/// * No internet connection
/// * Connection timed out
/// * DNS resolution failed
/// * Firewall or proxy blocked the request
/// * Server is not reachable
Expand Down Expand Up @@ -54,6 +55,7 @@ pub fn fetch(request: Request, on_done: impl 'static + Send + FnOnce(Result<Resp
///
/// `Err` can happen for a number of reasons:
/// * No internet connection
/// * Connection timed out
/// * DNS resolution failed
/// * Firewall or proxy blocked the request
/// * Server is not reachable
Expand Down
5 changes: 5 additions & 0 deletions ehttp/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use async_channel::{Receiver, Sender};
///
/// `Err` can happen for a number of reasons:
/// * No internet connection
/// * Connection timed out
/// * DNS resolution failed
/// * Firewall or proxy blocked the request
/// * Server is not reachable
Expand All @@ -25,6 +26,10 @@ use async_channel::{Receiver, Sender};
pub fn fetch_blocking(request: &Request) -> crate::Result<Response> {
let mut req = ureq::request(&request.method, &request.url);

if let Some(timeout) = request.timeout {
req = req.timeout(timeout);
}

for (k, v) in &request.headers {
req = req.set(k, v);
}
Expand Down
18 changes: 18 additions & 0 deletions ehttp/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

#[cfg(feature = "json")]
use serde::Serialize;

Expand Down Expand Up @@ -145,9 +147,14 @@ pub struct Request {
///
/// Used on Web to control CORS.
pub mode: Mode,

/// Cancel the request if it doesn't complete fast enough.
pub timeout: Option<Duration>,
}

impl Request {
pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);

/// Create a `GET` request with the given url.
#[allow(clippy::needless_pass_by_value)]
pub fn get(url: impl ToString) -> Self {
Expand All @@ -157,6 +164,7 @@ impl Request {
body: vec![],
headers: Headers::new(&[("Accept", "*/*")]),
mode: Mode::default(),
timeout: Some(Self::DEFAULT_TIMEOUT),
}
}

Expand All @@ -169,6 +177,7 @@ impl Request {
body: vec![],
headers: Headers::new(&[("Accept", "*/*")]),
mode: Mode::default(),
timeout: Some(Self::DEFAULT_TIMEOUT),
}
}

Expand All @@ -184,6 +193,7 @@ impl Request {
("Content-Type", "text/plain; charset=utf-8"),
]),
mode: Mode::default(),
timeout: Some(Self::DEFAULT_TIMEOUT),
}
}

Expand All @@ -209,6 +219,7 @@ impl Request {
/// .unwrap(),
/// );
/// ehttp::fetch(request, |result| {});
/// ```
#[cfg(feature = "multipart")]
pub fn multipart(url: impl ToString, builder: MultipartBuilder) -> Self {
let (content_type, data) = builder.finish();
Expand All @@ -218,6 +229,7 @@ impl Request {
body: data,
headers: Headers::new(&[("Accept", "*/*"), ("Content-Type", content_type.as_str())]),
mode: Mode::default(),
timeout: Some(Self::DEFAULT_TIMEOUT),
}
}

Expand All @@ -234,8 +246,14 @@ impl Request {
body: serde_json::to_string(body)?.into_bytes(),
headers: Headers::new(&[("Accept", "*/*"), ("Content-Type", "application/json")]),
mode: Mode::default(),
timeout: Some(Self::DEFAULT_TIMEOUT),
})
}

pub fn with_timeout(mut self, timeout: Option<Duration>) -> Self {
self.timeout = timeout;
self
}
}

/// Response from a completed HTTP request.
Expand Down
1 change: 1 addition & 0 deletions ehttp/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extern "C" {
///
/// `Err` can happen for a number of reasons:
/// * No internet connection
/// * Connection timed out
/// * DNS resolution failed
/// * Firewall or proxy blocked the request
/// * Server is not reachable
Expand Down
Loading