1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use crate::{
	clients::{resolve::resolve, SystemHttpClientInterface},
	url::ValidUrl,
	Error,
};
use std::time::Duration;

/// A response from a sent request
#[derive(Debug)]
pub struct Response {
	/// The body of the response
	pub body: Vec<u8>,
}

/// A builder for a request
pub struct RequestBuilder<U: ValidUrl> {
	url: U,
	timeout: Option<Duration>,
}
impl<U: ValidUrl> RequestBuilder<U> {
	/// Creates a new request builder with the given URL
	#[must_use]
	pub fn new(url: U) -> Self {
		Self { url, timeout: None }
	}

	/// Sets the timeout for the request
	///
	/// # Panics
	///
	/// Panics if the timeout is zero.
	#[must_use]
	pub fn timeout(mut self, timeout: Option<Duration>) -> Self {
		self.timeout = match timeout {
			Some(timeout) if !timeout.is_zero() => Some(timeout),
			None => None,
			_ => panic!("Timeout must be non-zero"),
		};
		self
	}

	/// Sends the request
	pub fn send(self) -> Result<Response, Error> {
		resolve()?.get(self.url.validate()?, self.timeout)
	}
}