sysreq/
request.rs

1use crate::{
2	clients::{resolve::resolve, SystemHttpClientInterface},
3	url::ValidUrl,
4	Error,
5};
6use std::time::Duration;
7
8/// A response from a sent request
9#[derive(Debug)]
10pub struct Response {
11	/// The body of the response
12	pub body: Vec<u8>,
13}
14
15/// A builder for a request
16pub struct RequestBuilder<U: ValidUrl> {
17	url: U,
18	timeout: Option<Duration>,
19}
20impl<U: ValidUrl> RequestBuilder<U> {
21	/// Creates a new request builder with the given URL
22	#[must_use]
23	pub fn new(url: U) -> Self {
24		Self { url, timeout: None }
25	}
26
27	/// Sets the timeout for the request
28	///
29	/// # Panics
30	///
31	/// Panics if the timeout is zero.
32	#[must_use]
33	pub fn timeout(mut self, timeout: Option<Duration>) -> Self {
34		self.timeout = match timeout {
35			Some(timeout) if !timeout.is_zero() => Some(timeout),
36			None => None,
37			_ => panic!("Timeout must be non-zero"),
38		};
39		self
40	}
41
42	/// Sends the request
43	pub fn send(self) -> Result<Response, Error> {
44		resolve()?.get(self.url.validate()?, self.timeout)
45	}
46}