hookburn-core 0.2.2

Shared HTTP client and WebhookSender trait for the Hookburn crates
Documentation
// crates/hookburn-core/src/builder.rs
use crate::client::HttpClient;
use crate::error::CoreError;
use reqwest::Client;
use std::time::Duration;

/// Builder for [`HttpClient`] with configurable timeout settings.
///
/// # Example
///
/// ```rust
/// use hookburn_core::HttpClientBuilder;
/// use std::time::Duration;
///
/// let client = HttpClientBuilder::new()
///     .connect_timeout(Duration::from_secs(5))
///     .request_timeout(Duration::from_secs(15))
///     .build()
///     .expect("failed to build client");
/// ```
#[derive(Default)]
pub struct HttpClientBuilder {
    connect_timeout: Option<Duration>,
    request_timeout: Option<Duration>,
}

impl HttpClientBuilder {
    /// Create a new builder with default settings.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the maximum time allowed to establish a TCP connection.
    #[must_use]
    pub const fn connect_timeout(mut self, timeout: Duration) -> Self {
        self.connect_timeout = Some(timeout);
        self
    }

    /// Set the maximum time allowed for a complete request/response cycle.
    ///
    /// Defaults to 30 seconds when not set.
    #[must_use]
    pub const fn request_timeout(mut self, timeout: Duration) -> Self {
        self.request_timeout = Some(timeout);
        self
    }

    /// Build the [`HttpClient`].
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying [`reqwest::Client`] cannot be
    /// constructed (e.g. TLS backend unavailable).
    pub fn build(self) -> Result<HttpClient, CoreError> {
        let mut builder =
            Client::builder().timeout(self.request_timeout.unwrap_or(Duration::from_secs(30)));

        if let Some(t) = self.connect_timeout {
            builder = builder.connect_timeout(t);
        }

        let client = builder.build().map_err(CoreError::Network)?;

        Ok(HttpClient::with_reqwest(client))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_builder_defaults() {
        let builder = HttpClientBuilder::new();
        let client = builder.build().unwrap();
        // Client should be created with default settings
        // Timeout is set internally but not exposed by reqwest::Client
        assert!(true);
    }

    #[test]
    fn test_builder_custom_timeouts() {
        let client = HttpClientBuilder::new()
            .connect_timeout(Duration::from_secs(5))
            .request_timeout(Duration::from_secs(15))
            .build()
            .unwrap();

        // Builder should have stored the timeout values
        let builder = HttpClientBuilder::new()
            .connect_timeout(Duration::from_secs(5))
            .request_timeout(Duration::from_secs(15));
        assert_eq!(builder.connect_timeout, Some(Duration::from_secs(5)));
        assert_eq!(builder.request_timeout, Some(Duration::from_secs(15)));
    }
}