Skip to content

Commit f983bb8

Browse files
authored
rust: Move Svix, SvixOptions into a separate module (#1694)
2 parents 2d5b7c5 + 022c5ea commit f983bb8

File tree

6 files changed

+119
-123
lines changed

6 files changed

+119
-123
lines changed

rust/src/api/client.rs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
use std::sync::Arc;
2+
3+
use hyper_util::{client::legacy::Client as HyperClient, rt::TokioExecutor};
4+
5+
use crate::Configuration;
6+
7+
const CRATE_VERSION: &str = env!("CARGO_PKG_VERSION");
8+
9+
pub struct SvixOptions {
10+
pub debug: bool,
11+
pub server_url: Option<String>,
12+
/// Timeout for HTTP requests.
13+
///
14+
/// The timeout is applied from when the request starts connecting until
15+
/// the response body has finished. If set to `None`, requests never time
16+
/// out.
17+
///
18+
/// Default: 15 seconds.
19+
pub timeout: Option<std::time::Duration>,
20+
/// Number of retries
21+
///
22+
/// The number of times the client will retry if a server-side error
23+
/// or timeout is received.
24+
///
25+
/// Default: 2
26+
pub num_retries: Option<u32>,
27+
}
28+
29+
impl Default for SvixOptions {
30+
fn default() -> Self {
31+
Self {
32+
debug: false,
33+
server_url: None,
34+
timeout: Some(std::time::Duration::from_secs(15)),
35+
num_retries: None,
36+
}
37+
}
38+
}
39+
40+
/// Svix API client.
41+
#[derive(Clone)]
42+
pub struct Svix {
43+
pub(super) cfg: Arc<Configuration>,
44+
server_url: Option<String>,
45+
}
46+
47+
impl Svix {
48+
pub fn new(token: String, options: Option<SvixOptions>) -> Self {
49+
let options = options.unwrap_or_default();
50+
51+
let cfg = Arc::new(Configuration {
52+
user_agent: Some(format!("svix-libs/{CRATE_VERSION}/rust")),
53+
client: HyperClient::builder(TokioExecutor::new()).build(crate::default_connector()),
54+
timeout: options.timeout,
55+
// These fields will be set by `with_token` below
56+
base_path: String::new(),
57+
bearer_access_token: None,
58+
num_retries: options.num_retries.unwrap_or(2),
59+
});
60+
let svix = Self {
61+
cfg,
62+
server_url: options.server_url,
63+
};
64+
svix.with_token(token)
65+
}
66+
67+
/// Creates a new `Svix` API client with a different token,
68+
/// re-using all of the settings and the Hyper client from
69+
/// an existing `Svix` instance.
70+
///
71+
/// This can be used to change the token without incurring
72+
/// the cost of TLS initialization.
73+
pub fn with_token(&self, token: String) -> Self {
74+
let base_path = self.server_url.clone().unwrap_or_else(|| {
75+
match token.split('.').last() {
76+
Some("us") => "https://api.us.svix.com",
77+
Some("eu") => "https://api.eu.svix.com",
78+
Some("in") => "https://api.in.svix.com",
79+
_ => "https://api.svix.com",
80+
}
81+
.to_string()
82+
});
83+
let cfg = Arc::new(Configuration {
84+
base_path,
85+
user_agent: self.cfg.user_agent.clone(),
86+
bearer_access_token: Some(token),
87+
client: self.cfg.client.clone(),
88+
timeout: self.cfg.timeout,
89+
num_retries: self.cfg.num_retries,
90+
});
91+
92+
Self {
93+
cfg,
94+
server_url: self.server_url.clone(),
95+
}
96+
}
97+
98+
#[cfg(feature = "svix_beta")]
99+
pub fn cfg(&self) -> &Configuration {
100+
&self.cfg
101+
}
102+
}
103+
104+
#[cfg(test)]
105+
mod tests {
106+
use crate::api::Svix;
107+
108+
#[test]
109+
fn test_future_send_sync() {
110+
fn require_send_sync<T: Send + Sync>(_: T) {}
111+
112+
let svix = Svix::new(String::new(), None);
113+
let message_api = svix.message();
114+
let fut = message_api.expunge_content(String::new(), String::new());
115+
require_send_sync(fut);
116+
}
117+
}

rust/src/api/mod.rs

Lines changed: 2 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
#![warn(unreachable_pub)]
22

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;
84

5+
pub use self::client::{Svix, SvixOptions};
96
pub use crate::models::*;
107

11-
const CRATE_VERSION: &str = env!("CARGO_PKG_VERSION");
12-
138
mod application;
149
mod authentication;
1510
mod background_task;
@@ -64,95 +59,7 @@ pub type ListOptions = MessageAttemptListAttemptedDestinationsOptions;
6459
#[deprecated = "Use AppUsageStatsIn instead"]
6560
pub type AggregateAppStatsOptions = AppUsageStatsIn;
6661

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-
10562
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-
15663
pub fn authentication(&self) -> Authentication<'_> {
15764
Authentication::new(&self.cfg)
15865
}
@@ -192,24 +99,4 @@ impl Svix {
19299
pub fn statistics(&self) -> Statistics<'_> {
193100
Statistics::new(&self.cfg)
194101
}
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-
}
215102
}

svix-cli/src/cmds/api/endpoint.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ pub enum EndpointCommands {
126126
/// List the application's endpoints.
127127
List {
128128
app_id: String,
129-
130129
#[clap(flatten)]
131130
options: EndpointListOptions,
132131
},
@@ -217,7 +216,6 @@ pub enum EndpointCommands {
217216
GetStats {
218217
app_id: String,
219218
id: String,
220-
221219
#[clap(flatten)]
222220
options: EndpointGetStatsOptions,
223221
},

svix-cli/src/cmds/api/integration.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ pub enum IntegrationCommands {
6868
/// List the application's integrations.
6969
List {
7070
app_id: String,
71-
7271
#[clap(flatten)]
7372
options: IntegrationListOptions,
7473
},

svix-cli/src/cmds/api/message.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ pub enum MessageCommands {
115115
/// set the `before` or `after` parameter as appropriate.
116116
List {
117117
app_id: String,
118-
119118
#[clap(flatten)]
120119
options: MessageListOptions,
121120
},

svix-cli/src/cmds/api/message_attempt.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,6 @@ pub enum MessageAttemptCommands {
254254
ListByEndpoint {
255255
app_id: String,
256256
endpoint_id: String,
257-
258257
#[clap(flatten)]
259258
options: MessageAttemptListByEndpointOptions,
260259
},
@@ -267,7 +266,6 @@ pub enum MessageAttemptCommands {
267266
ListByMsg {
268267
app_id: String,
269268
msg_id: String,
270-
271269
#[clap(flatten)]
272270
options: MessageAttemptListByMsgOptions,
273271
},
@@ -282,7 +280,6 @@ pub enum MessageAttemptCommands {
282280
ListAttemptedMessages {
283281
app_id: String,
284282
endpoint_id: String,
285-
286283
#[clap(flatten)]
287284
options: MessageAttemptListAttemptedMessagesOptions,
288285
},
@@ -308,7 +305,6 @@ pub enum MessageAttemptCommands {
308305
ListAttemptedDestinations {
309306
app_id: String,
310307
msg_id: String,
311-
312308
#[clap(flatten)]
313309
options: MessageAttemptListAttemptedDestinationsOptions,
314310
},

0 commit comments

Comments
 (0)