|
1 | 1 | #![warn(unreachable_pub)] |
2 | 2 |
|
3 | | -use std::sync::Arc; |
4 | | - |
5 | | -use hyper_util::{client::legacy::Client as HyperClient, rt::TokioExecutor}; |
6 | | - |
7 | | -use crate::Configuration; |
| 3 | +mod client; |
8 | 4 |
|
| 5 | +pub use self::client::{Svix, SvixOptions}; |
9 | 6 | pub use crate::models::*; |
10 | 7 |
|
11 | | -const CRATE_VERSION: &str = env!("CARGO_PKG_VERSION"); |
12 | | - |
13 | 8 | mod application; |
14 | 9 | mod authentication; |
15 | 10 | mod background_task; |
@@ -64,95 +59,7 @@ pub type ListOptions = MessageAttemptListAttemptedDestinationsOptions; |
64 | 59 | #[deprecated = "Use AppUsageStatsIn instead"] |
65 | 60 | pub type AggregateAppStatsOptions = AppUsageStatsIn; |
66 | 61 |
|
67 | | -pub struct SvixOptions { |
68 | | - pub debug: bool, |
69 | | - pub server_url: Option<String>, |
70 | | - /// Timeout for HTTP requests. |
71 | | - /// |
72 | | - /// The timeout is applied from when the request starts connecting until |
73 | | - /// the response body has finished. If set to `None`, requests never time |
74 | | - /// out. |
75 | | - /// |
76 | | - /// Default: 15 seconds. |
77 | | - pub timeout: Option<std::time::Duration>, |
78 | | - /// Number of retries |
79 | | - /// |
80 | | - /// The number of times the client will retry if a server-side error |
81 | | - /// or timeout is received. |
82 | | - /// |
83 | | - /// Default: 2 |
84 | | - pub num_retries: Option<u32>, |
85 | | -} |
86 | | - |
87 | | -impl Default for SvixOptions { |
88 | | - fn default() -> Self { |
89 | | - Self { |
90 | | - debug: false, |
91 | | - server_url: None, |
92 | | - timeout: Some(std::time::Duration::from_secs(15)), |
93 | | - num_retries: None, |
94 | | - } |
95 | | - } |
96 | | -} |
97 | | - |
98 | | -/// Svix API client. |
99 | | -#[derive(Clone)] |
100 | | -pub struct Svix { |
101 | | - cfg: Arc<Configuration>, |
102 | | - server_url: Option<String>, |
103 | | -} |
104 | | - |
105 | 62 | impl Svix { |
106 | | - pub fn new(token: String, options: Option<SvixOptions>) -> Self { |
107 | | - let options = options.unwrap_or_default(); |
108 | | - |
109 | | - let cfg = Arc::new(Configuration { |
110 | | - user_agent: Some(format!("svix-libs/{CRATE_VERSION}/rust")), |
111 | | - client: HyperClient::builder(TokioExecutor::new()).build(crate::default_connector()), |
112 | | - timeout: options.timeout, |
113 | | - // These fields will be set by `with_token` below |
114 | | - base_path: String::new(), |
115 | | - bearer_access_token: None, |
116 | | - num_retries: options.num_retries.unwrap_or(2), |
117 | | - }); |
118 | | - let svix = Self { |
119 | | - cfg, |
120 | | - server_url: options.server_url, |
121 | | - }; |
122 | | - svix.with_token(token) |
123 | | - } |
124 | | - |
125 | | - /// Creates a new `Svix` API client with a different token, |
126 | | - /// re-using all of the settings and the Hyper client from |
127 | | - /// an existing `Svix` instance. |
128 | | - /// |
129 | | - /// This can be used to change the token without incurring |
130 | | - /// the cost of TLS initialization. |
131 | | - pub fn with_token(&self, token: String) -> Self { |
132 | | - let base_path = self.server_url.clone().unwrap_or_else(|| { |
133 | | - match token.split('.').last() { |
134 | | - Some("us") => "https://api.us.svix.com", |
135 | | - Some("eu") => "https://api.eu.svix.com", |
136 | | - Some("in") => "https://api.in.svix.com", |
137 | | - _ => "https://api.svix.com", |
138 | | - } |
139 | | - .to_string() |
140 | | - }); |
141 | | - let cfg = Arc::new(Configuration { |
142 | | - base_path, |
143 | | - user_agent: self.cfg.user_agent.clone(), |
144 | | - bearer_access_token: Some(token), |
145 | | - client: self.cfg.client.clone(), |
146 | | - timeout: self.cfg.timeout, |
147 | | - num_retries: self.cfg.num_retries, |
148 | | - }); |
149 | | - |
150 | | - Self { |
151 | | - cfg, |
152 | | - server_url: self.server_url.clone(), |
153 | | - } |
154 | | - } |
155 | | - |
156 | 63 | pub fn authentication(&self) -> Authentication<'_> { |
157 | 64 | Authentication::new(&self.cfg) |
158 | 65 | } |
@@ -192,24 +99,4 @@ impl Svix { |
192 | 99 | pub fn statistics(&self) -> Statistics<'_> { |
193 | 100 | Statistics::new(&self.cfg) |
194 | 101 | } |
195 | | - |
196 | | - #[cfg(feature = "svix_beta")] |
197 | | - pub fn cfg(&self) -> &Configuration { |
198 | | - &self.cfg |
199 | | - } |
200 | | -} |
201 | | - |
202 | | -#[cfg(test)] |
203 | | -mod tests { |
204 | | - use crate::api::Svix; |
205 | | - |
206 | | - #[test] |
207 | | - fn test_future_send_sync() { |
208 | | - fn require_send_sync<T: Send + Sync>(_: T) {} |
209 | | - |
210 | | - let svix = Svix::new(String::new(), None); |
211 | | - let message_api = svix.message(); |
212 | | - let fut = message_api.expunge_content(String::new(), String::new()); |
213 | | - require_send_sync(fut); |
214 | | - } |
215 | 102 | } |
0 commit comments