1- use clap:: { ArgEnum , Parser } ;
21use feature_probe_server_sdk:: { FPConfig , FPUser , FeatureProbe } ;
3- use serde_json:: json;
4- use std:: error:: Error ;
52use std:: time:: Duration ;
63
7- // cargo run --example demo -- -t test_jg_toggle_string -r string -u 10 -a userId=102 -a [email protected] -D 8- #[ derive( Parser , Debug ) ]
9- #[ clap( author, version, about, long_about = None ) ]
10- struct Args {
11- #[ clap(
12- short,
13- long,
14- default_value = "http://www.featureprobe.com/feature-probe-server"
15- ) ]
16- host_url : String ,
4+ // Connect to demo docker environment.
5+ // cargo run --example demo
176
18- #[ clap(
19- short,
20- long,
21- default_value = "server-3df72071f12a143ba67769ae6837323a2392da6c"
22- ) ]
23- server_sdk_key : String ,
24-
25- /// Toggle key
26- #[ clap( short, long) ]
27- toggle : String ,
28-
29- /// Toggle return type
30- #[ clap( short, long, arg_enum) ]
31- return_type : ReturnType ,
32-
33- /// User key
34- #[ clap( short, long) ]
35- user : String ,
36-
37- /// User fields, k=v
38- #[ clap( short, long, parse( try_from_str = parse_key_val) , multiple_occurrences( true ) ) ]
39- attrs : Vec < ( String , String ) > ,
40-
41- /// Make the toggle execute show detail
42- #[ clap( short = 'D' , long) ]
43- show_detail : bool ,
44- }
45-
46- #[ derive( Copy , Clone , Debug , PartialEq , Eq , PartialOrd , Ord , ArgEnum ) ]
47- enum ReturnType {
48- String ,
49- Boolean ,
50- JSON ,
51- Number ,
52- }
53-
54- fn parse_key_val < T , U > ( s : & str ) -> Result < ( T , U ) , Box < dyn Error + Send + Sync + ' static > >
55- where
56- T : std:: str:: FromStr ,
57- T :: Err : Error + Send + Sync + ' static ,
58- U : std:: str:: FromStr ,
59- U :: Err : Error + Send + Sync + ' static ,
60- {
61- let pos = s
62- . find ( '=' )
63- . ok_or_else ( || format ! ( "invalid KEY=value: no `=` found in `{}`" , s) ) ?;
64- Ok ( ( s[ ..pos] . parse ( ) ?, s[ pos + 1 ..] . parse ( ) ?) )
65- }
667
678#[ tokio:: main]
689async fn main ( ) {
69- let _ = tracing_subscriber:: fmt ( ) . init ( ) ;
70- let args = Args :: parse ( ) ;
71- let remote_url = args . host_url ;
10+ // let _ = tracing_subscriber::fmt()
11+ // .with_env_filter("feature_probe_server_sdk=trace")
12+ // .init() ;
7213
14+ let remote_url = "http://localhost:4007" ;
15+ let server_sdk_key = "server-8ed48815ef044428826787e9a238b9c6a479f98c" ;
7316 let config = FPConfig {
7417 remote_url : remote_url. to_owned ( ) ,
75- server_sdk_key : args . server_sdk_key . to_owned ( ) ,
76- refresh_interval : Duration :: from_millis ( 400 ) ,
18+ server_sdk_key : server_sdk_key. to_owned ( ) ,
19+ refresh_interval : Duration :: from_secs ( 5 ) ,
7720 #[ cfg( feature = "use_tokio" ) ]
7821 http_client : None ,
7922 wait_first_resp : true ,
@@ -88,24 +31,15 @@ async fn main() {
8831 }
8932 } ;
9033
91- let user = FPUser :: new ( & args. user ) . with_attrs ( args. attrs . into_iter ( ) ) ;
34+ let user = FPUser :: new ( "user_id" ) . with ( "city" , "Paris" ) ;
35+ let discount = fp. number_value ( "promotion_activity" , & user, 9.0 ) ;
36+ println ! ( "Result => discount for user in Paris is : {:?}" , discount) ;
9237
93- let detail = match args. return_type {
94- ReturnType :: Boolean => json ! ( fp. bool_detail( & args. toggle, & user, false ) ) ,
95- ReturnType :: String => json ! ( fp. string_detail( & args. toggle, & user, "" . to_string( ) ) ) ,
96- ReturnType :: Number => json ! ( fp. number_detail( & args. toggle, & user, 0.0 ) ) ,
97- ReturnType :: JSON => json ! ( fp. json_detail( & args. toggle, & user, json!( "" ) ) ) ,
98- } ;
99- tracing:: info!(
100- "Args => \n \t Server sdk key: {}\n \t Toggle: {}\n \t Return type: {:?} \n \t User: {:?}" ,
101- args. server_sdk_key,
102- args. toggle,
103- args. return_type,
104- user,
105- ) ;
106- if args. show_detail {
107- tracing:: warn!( "Detail => \n \t {:?}\n " , detail) ;
108- }
109- tracing:: info!( "Result => \n \t {:?}\n " , detail. get( "value" ) . unwrap( ) ) ;
110- tokio:: time:: sleep ( Duration :: from_secs ( 1 ) ) . await ; // wait event flush
38+ let detail = fp. number_detail ( "promotion_activity" , & user, 9.0 ) ;
39+ println ! ( " => reason : {:?}" , detail. reason) ;
40+ println ! ( " => index : {:?}" , detail. rule_index) ;
41+
42+ let user2 = FPUser :: new ( "user_id" ) . with ( "city" , "New York" ) ;
43+ let discount2 = fp. number_value ( "promotion_activity" , & user2, 9.0 ) ;
44+ println ! ( "Result => discount for user in New York is : {:?}" , discount2) ;
11145}
0 commit comments