crypto_pay_api/webhook/
config.rs

1use std::time::Duration;
2
3use crate::client::DEFAULT_WEBHOOK_EXPIRATION_TIME;
4
5#[derive(Debug, Default)]
6pub struct WebhookHandlerConfig {
7    pub expiration_time: Option<Duration>,
8}
9
10pub struct WebhookHandlerConfigBuilder<'a> {
11    api_token: Option<&'a str>,
12    config: WebhookHandlerConfig,
13}
14
15impl<'a> WebhookHandlerConfigBuilder<'a> {
16    /// Creates a new webhook handler config builder with default expiration time
17    ///
18    /// # Default Settings
19    /// * Expiration time: 10 minutes
20    pub fn new() -> Self {
21        Self {
22            api_token: None,
23            config: WebhookHandlerConfig {
24                expiration_time: Some(Duration::from_secs(DEFAULT_WEBHOOK_EXPIRATION_TIME)),
25            },
26        }
27    }
28
29    /// Creates a new webhook handler config builder with client reference
30    pub(crate) fn new_with_client(api_token: &'a str) -> Self {
31        Self {
32            api_token: Some(api_token),
33            config: WebhookHandlerConfig {
34                expiration_time: Some(Duration::from_secs(DEFAULT_WEBHOOK_EXPIRATION_TIME)),
35            },
36        }
37    }
38
39    /// Sets the expiration time for the webhook handler
40    pub fn expiration_time(mut self, duration: Duration) -> Self {
41        self.config.expiration_time = Some(duration);
42        self
43    }
44
45    /// Disables the expiration time for the webhook handler
46    pub fn disable_expiration(mut self) -> Self {
47        self.config.expiration_time = None;
48        self
49    }
50
51    /// Builds the webhook handler config (for backward compatibility)
52    pub fn build_config(self) -> WebhookHandlerConfig {
53        self.config
54    }
55
56    /// Builds the webhook handler (requires client reference)
57    pub fn build(self) -> crate::webhook::handler::WebhookHandler {
58        let api_token = self
59            .api_token
60            .expect("WebhookHandlerConfigBuilder must be created via client.webhook_handler()");
61        crate::webhook::handler::WebhookHandler::with_config(api_token, self.config)
62    }
63}
64
65impl<'a> Default for WebhookHandlerConfigBuilder<'a> {
66    fn default() -> Self {
67        Self::new()
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74    use std::time::Duration;
75
76    #[test]
77    fn test_webhook_handler_config_builder() {
78        let builder = WebhookHandlerConfigBuilder::new().expiration_time(Duration::from_secs(1000));
79
80        assert_eq!(builder.config.expiration_time, Some(Duration::from_secs(1000)));
81    }
82
83    #[test]
84    fn test_webhook_handler_config_builder_disable_expiration() {
85        let builder = WebhookHandlerConfigBuilder::new().disable_expiration();
86
87        assert_eq!(builder.config.expiration_time, None);
88    }
89
90    #[test]
91    fn test_webhook_handler_config_builder_default() {
92        let builder = WebhookHandlerConfigBuilder::default();
93
94        assert_eq!(builder.config.expiration_time, Some(Duration::from_secs(600)));
95    }
96}