11use bytes:: Bytes ;
22use freedom_config:: Config ;
3- use reqwest:: { Response , StatusCode } ;
3+ use reqwest:: { RequestBuilder , Response , StatusCode } ;
44use url:: Url ;
55
66use crate :: api:: { Api , Inner , Value } ;
@@ -13,6 +13,7 @@ use crate::api::{Api, Inner, Value};
1313pub struct Client {
1414 pub ( crate ) config : Config ,
1515 pub ( crate ) client : reqwest:: Client ,
16+ universal_headers : Vec < ( String , String ) > ,
1617}
1718
1819impl PartialEq for Client {
@@ -42,6 +43,7 @@ impl Client {
4243 Self {
4344 config,
4445 client : reqwest:: Client :: new ( ) ,
46+ universal_headers : Vec :: new ( ) ,
4547 }
4648 }
4749
@@ -56,6 +58,23 @@ impl Client {
5658 let config = Config :: from_env ( ) ?;
5759 Ok ( Self :: from_config ( config) )
5860 }
61+
62+ /// Adds a universal header key and value to all GET POST, and DELETEs made with the client
63+ pub fn with_universal_header (
64+ mut self ,
65+ key : impl Into < String > ,
66+ value : impl Into < String > ,
67+ ) -> Self {
68+ self . universal_headers . push ( ( key. into ( ) , value. into ( ) ) ) ;
69+ self
70+ }
71+
72+ fn append_headers ( & self , mut req : RequestBuilder ) -> RequestBuilder {
73+ for ( header, value) in self . universal_headers . iter ( ) {
74+ req = req. header ( header, value) ;
75+ }
76+ req
77+ }
5978}
6079
6180impl Api for Client {
@@ -64,9 +83,9 @@ impl Api for Client {
6483 async fn get ( & self , url : Url ) -> Result < ( Bytes , StatusCode ) , crate :: error:: Error > {
6584 tracing:: trace!( "GET to {}" , url) ;
6685
67- let resp = self
68- . client
69- . get ( url . clone ( ) )
86+ let req = self . append_headers ( self . client . get ( url . clone ( ) ) ) ;
87+
88+ let resp = req
7089 . basic_auth ( self . config . key ( ) , Some ( & self . config . expose_secret ( ) ) )
7190 . send ( )
7291 . await ?;
@@ -84,9 +103,9 @@ impl Api for Client {
84103 async fn delete ( & self , url : Url ) -> Result < Response , crate :: error:: Error > {
85104 tracing:: trace!( "DELETE to {}" , url) ;
86105
87- self . client
88- . delete ( url . clone ( ) )
89- . basic_auth ( self . config . key ( ) , Some ( self . config . expose_secret ( ) ) )
106+ let req = self . append_headers ( self . client . delete ( url . clone ( ) ) ) ;
107+
108+ req . basic_auth ( self . config . key ( ) , Some ( self . config . expose_secret ( ) ) )
90109 . send ( )
91110 . await
92111 . inspect_err ( |error| tracing:: warn!( %error, %url, "Failed to DELETE" ) )
@@ -100,9 +119,9 @@ impl Api for Client {
100119 {
101120 tracing:: trace!( "POST to {}" , url) ;
102121
103- self . client
104- . post ( url . clone ( ) )
105- . basic_auth ( self . config . key ( ) , Some ( self . config . expose_secret ( ) ) )
122+ let req = self . append_headers ( self . client . post ( url . clone ( ) ) ) ;
123+
124+ req . basic_auth ( self . config . key ( ) , Some ( self . config . expose_secret ( ) ) )
106125 . json ( & msg)
107126 . send ( )
108127 . await
@@ -193,7 +212,7 @@ mod tests {
193212
194213 assert_eq ! ( response, RESPONSE . as_bytes( ) ) ;
195214 assert_eq ! ( status, StatusCode :: OK ) ;
196- mock. assert_hits ( 1 ) ;
215+ mock. assert_calls ( 1 ) ;
197216 }
198217
199218 #[ tokio:: test]
@@ -211,7 +230,7 @@ mod tests {
211230
212231 assert_eq ! ( response, RESPONSE . as_bytes( ) ) ;
213232 assert_eq ! ( status, StatusCode :: NOT_FOUND ) ;
214- mock. assert_hits ( 1 ) ;
233+ mock. assert_calls ( 1 ) ;
215234 }
216235
217236 #[ tokio:: test]
@@ -231,6 +250,6 @@ mod tests {
231250 let url = Url :: parse ( & format ! ( "http://{}/testing" , addr) ) . unwrap ( ) ;
232251 client. post ( url, & json) . await . unwrap ( ) ;
233252
234- mock. assert_hits ( 1 ) ;
253+ mock. assert_calls ( 1 ) ;
235254 }
236255}
0 commit comments