@@ -7,8 +7,8 @@ type HmacSha256 = Hmac<sha2::Sha256>;
77
88const DEFAULT_API_URL : & str = "https://api.ferrflow.com" ;
99
10- fn hmac_secret ( ) -> & ' static str {
11- option_env ! ( "FERRFLOW_HMAC_SECRET" ) . unwrap_or ( env ! ( "FERRFLOW_HMAC_SECRET" ) )
10+ fn hmac_secret ( ) -> Option < & ' static str > {
11+ option_env ! ( "FERRFLOW_HMAC_SECRET" )
1212}
1313
1414#[ derive( Debug , Clone , Copy , Serialize ) ]
@@ -107,19 +107,21 @@ pub fn send_event(
107107 . as_secs ( )
108108 . to_string ( ) ;
109109
110- let message = format ! ( "{timestamp}.{body}" ) ;
111- let mut mac = HmacSha256 :: new_from_slice ( hmac_secret ( ) . as_bytes ( ) )
112- . expect ( "HMAC accepts any key length" ) ;
113- mac. update ( message. as_bytes ( ) ) ;
114- let signature = hex:: encode ( mac. finalize ( ) . into_bytes ( ) ) ;
115-
116110 let agent = ureq:: Agent :: new_with_defaults ( ) ;
117- let _ = agent
118- . post ( & url)
119- . header ( "Content-Type" , "application/json" )
120- . header ( "X-Timestamp" , & timestamp)
121- . header ( "X-Signature" , & signature)
122- . send ( body. as_bytes ( ) ) ;
111+ let mut req = agent. post ( & url) . header ( "Content-Type" , "application/json" ) ;
112+
113+ if let Some ( secret) = hmac_secret ( ) {
114+ let message = format ! ( "{timestamp}.{body}" ) ;
115+ let mut mac =
116+ HmacSha256 :: new_from_slice ( secret. as_bytes ( ) ) . expect ( "HMAC accepts any key length" ) ;
117+ mac. update ( message. as_bytes ( ) ) ;
118+ let signature = hex:: encode ( mac. finalize ( ) . into_bytes ( ) ) ;
119+ req = req
120+ . header ( "X-Timestamp" , & timestamp)
121+ . header ( "X-Signature" , & signature) ;
122+ }
123+
124+ let _ = req. send ( body. as_bytes ( ) ) ;
123125 } ) ;
124126}
125127
0 commit comments