svix_webhook_with_clone/api/
mod.rs

1use std::sync::Arc;
2
3use hyper_util::{client::legacy::Client as HyperClient, rt::TokioExecutor};
4
5use crate::Configuration;
6
7#[cfg(feature = "svix_beta")]
8pub use crate::apis::message_api::{
9    V1PeriodMessagePeriodCreateError, V1PeriodMessagePeriodCreateParams,
10    V1PeriodMessagePeriodEventsParams, V1PeriodMessagePeriodEventsSubscriptionError,
11    V1PeriodMessagePeriodEventsSubscriptionParams,
12};
13pub use crate::models::*;
14
15const CRATE_VERSION: &str = env!("CARGO_PKG_VERSION");
16
17#[cfg(feature = "svix_beta")]
18pub mod raw_stream_api {
19    pub use crate::{
20        apis::stream_api::*,
21        models::{
22            stream_in, stream_out, stream_patch, stream_sink_in, stream_sink_out, stream_sink_patch,
23        },
24    };
25}
26
27mod application;
28mod authentication;
29mod background_task;
30mod endpoint;
31mod event_type;
32mod integration;
33mod message;
34mod message_attempt;
35mod operational_webhook_endpoint;
36mod statistics;
37
38pub use self::{
39    application::{Application, ApplicationListOptions},
40    authentication::Authentication,
41    background_task::{BackgroundTask, BackgroundTaskListOptions},
42    endpoint::{Endpoint, EndpointListOptions, EndpointStatsOptions},
43    event_type::{EventType, EventTypeListOptions},
44    integration::{Integration, IntegrationListOptions},
45    message::{Message, MessageListOptions},
46    message_attempt::{
47        MessageAttempt, MessageAttemptListByEndpointOptions, MessageAttemptListOptions,
48    },
49    operational_webhook_endpoint::{
50        OperationalWebhookEndpoint, OperationalWebhookEndpointListOptions,
51    },
52    statistics::{AggregateAppStatsOptions, Statistics},
53};
54
55pub struct SvixOptions {
56    pub debug: bool,
57    pub server_url: Option<String>,
58    /// Timeout for HTTP requests.
59    ///
60    /// The timeout is applied from when the request starts connecting until
61    /// the response body has finished. If set to `None`, requests never time
62    /// out.
63    ///
64    /// Default: 15 seconds.
65    pub timeout: Option<std::time::Duration>,
66}
67
68impl Default for SvixOptions {
69    fn default() -> Self {
70        Self {
71            debug: false,
72            server_url: None,
73            timeout: Some(std::time::Duration::from_secs(15)),
74        }
75    }
76}
77
78/// Svix API client.
79#[derive(Clone)]
80pub struct Svix {
81    cfg: Arc<Configuration>,
82    server_url: Option<String>,
83}
84
85impl Svix {
86    pub fn new(token: String, options: Option<SvixOptions>) -> Self {
87        let options = options.unwrap_or_default();
88
89        let cfg = Arc::new(Configuration {
90            user_agent: Some(format!("svix-libs/{CRATE_VERSION}/rust")),
91            client: HyperClient::builder(TokioExecutor::new()).build(crate::default_connector()),
92            timeout: options.timeout,
93            // These fields will be set by `with_token` below
94            base_path: String::new(),
95            bearer_access_token: None,
96        });
97        let svix = Self {
98            cfg,
99            server_url: options.server_url,
100        };
101        svix.with_token(token)
102    }
103
104    /// Creates a new `Svix` API client with a different token,
105    /// re-using all of the settings and the Hyper client from
106    /// an existing `Svix` instance.
107    ///
108    /// This can be used to change the token without incurring
109    /// the cost of TLS initialization.
110    pub fn with_token(&self, token: String) -> Self {
111        let base_path = self.server_url.clone().unwrap_or_else(|| {
112            match token.split('.').last() {
113                Some("us") => "https://api.us.svix.com",
114                Some("eu") => "https://api.eu.svix.com",
115                Some("in") => "https://api.in.svix.com",
116                _ => "https://api.svix.com",
117            }
118            .to_string()
119        });
120        let cfg = Arc::new(Configuration {
121            base_path,
122            user_agent: self.cfg.user_agent.clone(),
123            bearer_access_token: Some(token),
124            client: self.cfg.client.clone(),
125            timeout: self.cfg.timeout,
126        });
127
128        Self {
129            cfg,
130            server_url: self.server_url.clone(),
131        }
132    }
133
134    pub fn authentication(&self) -> Authentication<'_> {
135        Authentication::new(&self.cfg)
136    }
137
138    pub fn application(&self) -> Application<'_> {
139        Application::new(&self.cfg)
140    }
141
142    pub fn background_task(&self) -> BackgroundTask<'_> {
143        BackgroundTask::new(&self.cfg)
144    }
145
146    pub fn endpoint(&self) -> Endpoint<'_> {
147        Endpoint::new(&self.cfg)
148    }
149
150    pub fn integration(&self) -> Integration<'_> {
151        Integration::new(&self.cfg)
152    }
153
154    pub fn event_type(&self) -> EventType<'_> {
155        EventType::new(&self.cfg)
156    }
157
158    pub fn message(&self) -> Message<'_> {
159        Message::new(&self.cfg)
160    }
161
162    pub fn message_attempt(&self) -> MessageAttempt<'_> {
163        MessageAttempt::new(&self.cfg)
164    }
165
166    pub fn operational_webhook_endpoint(&self) -> OperationalWebhookEndpoint<'_> {
167        OperationalWebhookEndpoint::new(&self.cfg)
168    }
169
170    pub fn statistics(&self) -> Statistics<'_> {
171        Statistics::new(&self.cfg)
172    }
173
174    #[cfg(feature = "svix_beta")]
175    pub fn cfg(&self) -> &Configuration {
176        &self.cfg
177    }
178}
179
180#[derive(Default)]
181pub struct PostOptions {
182    pub idempotency_key: Option<String>,
183}
184
185#[derive(Default)]
186pub struct ListOptions {
187    pub iterator: Option<String>,
188    pub limit: Option<i32>,
189}
190
191#[cfg(test)]
192mod tests {
193    use crate::api::Svix;
194
195    #[test]
196    fn test_future_send_sync() {
197        fn require_send_sync<T: Send + Sync>(_: T) {}
198
199        let svix = Svix::new(String::new(), None);
200        let message_api = svix.message();
201        let fut = message_api.expunge_content(String::new(), String::new());
202        require_send_sync(fut);
203    }
204}