crypto_pay_api/webhook/
config.rs1use 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 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 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 pub fn expiration_time(mut self, duration: Duration) -> Self {
41 self.config.expiration_time = Some(duration);
42 self
43 }
44
45 pub fn disable_expiration(mut self) -> Self {
47 self.config.expiration_time = None;
48 self
49 }
50
51 pub fn build_config(self) -> WebhookHandlerConfig {
53 self.config
54 }
55
56 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}