1- use std:: { sync:: Arc , time:: Duration } ;
1+ use std:: {
2+ sync:: Arc ,
3+ time:: Duration ,
4+ } ;
25
3- use hyper_util:: { client:: legacy:: Client as HyperClient , rt:: TokioExecutor } ;
6+ use hyper_util:: {
7+ client:: legacy:: Client as HyperClient ,
8+ rt:: TokioExecutor ,
9+ } ;
410
511use crate :: Configuration ;
612
@@ -64,20 +70,25 @@ pub struct Svix {
6470}
6571
6672impl Svix {
67- pub fn new ( token : String , options : Option < SvixOptions > ) -> Self {
73+ pub fn new (
74+ token : String ,
75+ options : Option < SvixOptions > ,
76+ ) -> Self {
6877 let options = options. unwrap_or_default ( ) ;
6978
70- let cfg = Arc :: new ( Configuration {
71- user_agent : Some ( format ! ( "svix-libs/{CRATE_VERSION}/rust" ) ) ,
72- client : HyperClient :: builder ( TokioExecutor :: new ( ) )
73- . build ( crate :: make_connector ( options. proxy_address ) ) ,
74- timeout : options. timeout ,
75- // These fields will be set by `with_token` below
76- base_path : String :: new ( ) ,
77- bearer_access_token : None ,
78- num_retries : options. num_retries . unwrap_or ( 2 ) ,
79- retry_schedule : options. retry_schedule ,
80- } ) ;
79+ let cfg = Arc :: new (
80+ Configuration {
81+ user_agent : Some ( format ! ( "svix-libs/{CRATE_VERSION}/rust" ) ) ,
82+ client : HyperClient :: builder ( TokioExecutor :: new ( ) )
83+ . build ( crate :: make_connector ( options. proxy_address ) ) ,
84+ timeout : options. timeout ,
85+ // These fields will be set by `with_token` below
86+ base_path : String :: new ( ) ,
87+ bearer_access_token : None ,
88+ num_retries : options. num_retries . unwrap_or ( 2 ) ,
89+ retry_schedule : options. retry_schedule ,
90+ } ,
91+ ) ;
8192 let svix = Self {
8293 cfg,
8394 server_url : options. server_url ,
@@ -91,27 +102,34 @@ impl Svix {
91102 ///
92103 /// This can be used to change the token without incurring
93104 /// the cost of TLS initialization.
94- pub fn with_token ( & self , token : String ) -> Self {
95- let base_path = self . server_url . clone ( ) . unwrap_or_else ( || {
96- match token. split ( '.' ) . next_back ( ) {
97- Some ( "us" ) => "https://api.us.svix.com" ,
98- Some ( "eu" ) => "https://api.eu.svix.com" ,
99- Some ( "in" ) => "https://api.in.svix.com" ,
100- Some ( "ca" ) => "https://api.ca.svix.com" ,
101- Some ( "au" ) => "https://api.au.svix.com" ,
102- _ => "https://api.svix.com" ,
103- }
104- . to_string ( )
105- } ) ;
106- let cfg = Arc :: new ( Configuration {
107- base_path,
108- user_agent : self . cfg . user_agent . clone ( ) ,
109- bearer_access_token : Some ( token) ,
110- client : self . cfg . client . clone ( ) ,
111- timeout : self . cfg . timeout ,
112- num_retries : self . cfg . num_retries ,
113- retry_schedule : self . cfg . retry_schedule . clone ( ) ,
114- } ) ;
105+ pub fn with_token (
106+ & self ,
107+ token : String ,
108+ ) -> Self {
109+ let base_path = self . server_url . clone ( ) . unwrap_or_else (
110+ || {
111+ match token. split ( '.' ) . next_back ( ) {
112+ Some ( "us" ) => "https://api.us.svix.com" ,
113+ Some ( "eu" ) => "https://api.eu.svix.com" ,
114+ Some ( "in" ) => "https://api.in.svix.com" ,
115+ Some ( "ca" ) => "https://api.ca.svix.com" ,
116+ Some ( "au" ) => "https://api.au.svix.com" ,
117+ _ => "https://api.svix.com" ,
118+ }
119+ . to_string ( )
120+ } ,
121+ ) ;
122+ let cfg = Arc :: new (
123+ Configuration {
124+ base_path,
125+ user_agent : self . cfg . user_agent . clone ( ) ,
126+ bearer_access_token : Some ( token) ,
127+ client : self . cfg . client . clone ( ) ,
128+ timeout : self . cfg . timeout ,
129+ num_retries : self . cfg . num_retries ,
130+ retry_schedule : self . cfg . retry_schedule . clone ( ) ,
131+ } ,
132+ ) ;
115133
116134 Self {
117135 cfg,
@@ -133,9 +151,15 @@ mod tests {
133151 fn test_future_send_sync ( ) {
134152 fn require_send_sync < T : Send + Sync > ( _: T ) { }
135153
136- let svix = Svix :: new ( String :: new ( ) , None ) ;
154+ let svix = Svix :: new (
155+ String :: new ( ) ,
156+ None ,
157+ ) ;
137158 let message_api = svix. message ( ) ;
138- let fut = message_api. expunge_content ( String :: new ( ) , String :: new ( ) ) ;
159+ let fut = message_api. expunge_content (
160+ String :: new ( ) ,
161+ String :: new ( ) ,
162+ ) ;
139163 require_send_sync ( fut) ;
140164 }
141165}
0 commit comments