Skip to main content

hookburn_core/
builder.rs

1// crates/hookburn-core/src/builder.rs
2use crate::client::HttpClient;
3use crate::error::CoreError;
4use reqwest::Client;
5use std::time::Duration;
6
7/// Builder for [`HttpClient`] with configurable timeout settings.
8///
9/// # Example
10///
11/// ```rust
12/// use hookburn_core::HttpClientBuilder;
13/// use std::time::Duration;
14///
15/// let client = HttpClientBuilder::new()
16///     .connect_timeout(Duration::from_secs(5))
17///     .request_timeout(Duration::from_secs(15))
18///     .build()
19///     .expect("failed to build client");
20/// ```
21#[derive(Default)]
22pub struct HttpClientBuilder {
23    connect_timeout: Option<Duration>,
24    request_timeout: Option<Duration>,
25}
26
27impl HttpClientBuilder {
28    /// Create a new builder with default settings.
29    #[must_use]
30    pub fn new() -> Self {
31        Self::default()
32    }
33
34    /// Set the maximum time allowed to establish a TCP connection.
35    #[must_use]
36    pub const fn connect_timeout(mut self, timeout: Duration) -> Self {
37        self.connect_timeout = Some(timeout);
38        self
39    }
40
41    /// Set the maximum time allowed for a complete request/response cycle.
42    ///
43    /// Defaults to 30 seconds when not set.
44    #[must_use]
45    pub const fn request_timeout(mut self, timeout: Duration) -> Self {
46        self.request_timeout = Some(timeout);
47        self
48    }
49
50    /// Build the [`HttpClient`].
51    ///
52    /// # Errors
53    ///
54    /// Returns an error if the underlying [`reqwest::Client`] cannot be
55    /// constructed (e.g. TLS backend unavailable).
56    pub fn build(self) -> Result<HttpClient, CoreError> {
57        let mut builder =
58            Client::builder().timeout(self.request_timeout.unwrap_or(Duration::from_secs(30)));
59
60        if let Some(t) = self.connect_timeout {
61            builder = builder.connect_timeout(t);
62        }
63
64        let client = builder.build().map_err(CoreError::Network)?;
65
66        Ok(HttpClient::with_reqwest(client))
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn test_builder_defaults() {
76        let builder = HttpClientBuilder::new();
77        let client = builder.build().unwrap();
78        // Client should be created with default settings
79        // Timeout is set internally but not exposed by reqwest::Client
80        assert!(true);
81    }
82
83    #[test]
84    fn test_builder_custom_timeouts() {
85        let client = HttpClientBuilder::new()
86            .connect_timeout(Duration::from_secs(5))
87            .request_timeout(Duration::from_secs(15))
88            .build()
89            .unwrap();
90
91        // Builder should have stored the timeout values
92        let builder = HttpClientBuilder::new()
93            .connect_timeout(Duration::from_secs(5))
94            .request_timeout(Duration::from_secs(15));
95        assert_eq!(builder.connect_timeout, Some(Duration::from_secs(5)));
96        assert_eq!(builder.request_timeout, Some(Duration::from_secs(15)));
97    }
98}