@@ -23,11 +23,13 @@ pub(crate) async fn request<Input: Serialize, Output: DeserializeOwned + 'static
2323 use isahc:: * ;
2424
2525 let auth = format ! ( "Bearer {}" , apikey) ;
26+ let user_agent = qualified_version ( ) ;
2627
2728 let mut response = match & method {
2829 Method :: Get => {
2930 Request :: get ( url)
3031 . header ( header:: AUTHORIZATION , auth)
32+ . header ( header:: USER_AGENT , user_agent)
3133 . body ( ( ) )
3234 . map_err ( |_| crate :: errors:: Error :: InvalidRequest ) ?
3335 . send_async ( )
@@ -36,6 +38,7 @@ pub(crate) async fn request<Input: Serialize, Output: DeserializeOwned + 'static
3638 Method :: Delete => {
3739 Request :: delete ( url)
3840 . header ( header:: AUTHORIZATION , auth)
41+ . header ( header:: USER_AGENT , user_agent)
3942 . body ( ( ) )
4043 . map_err ( |_| crate :: errors:: Error :: InvalidRequest ) ?
4144 . send_async ( )
@@ -45,6 +48,7 @@ pub(crate) async fn request<Input: Serialize, Output: DeserializeOwned + 'static
4548 Request :: post ( url)
4649 . header ( header:: AUTHORIZATION , auth)
4750 . header ( header:: CONTENT_TYPE , "application/json" )
51+ . header ( header:: USER_AGENT , user_agent)
4852 . body ( to_string ( & body) . unwrap ( ) )
4953 . map_err ( |_| crate :: errors:: Error :: InvalidRequest ) ?
5054 . send_async ( )
@@ -54,6 +58,7 @@ pub(crate) async fn request<Input: Serialize, Output: DeserializeOwned + 'static
5458 Request :: patch ( url)
5559 . header ( header:: AUTHORIZATION , auth)
5660 . header ( header:: CONTENT_TYPE , "application/json" )
61+ . header ( header:: USER_AGENT , user_agent)
5762 . body ( to_string ( & body) . unwrap ( ) )
5863 . map_err ( |_| crate :: errors:: Error :: InvalidRequest ) ?
5964 . send_async ( )
@@ -63,6 +68,7 @@ pub(crate) async fn request<Input: Serialize, Output: DeserializeOwned + 'static
6368 Request :: put ( url)
6469 . header ( header:: AUTHORIZATION , auth)
6570 . header ( header:: CONTENT_TYPE , "application/json" )
71+ . header ( header:: USER_AGENT , user_agent)
6672 . body ( to_string ( & body) . unwrap ( ) )
6773 . map_err ( |_| crate :: errors:: Error :: InvalidRequest ) ?
6874 . send_async ( )
@@ -95,11 +101,13 @@ pub(crate) async fn request<Input: Serialize, Output: DeserializeOwned + 'static
95101
96102 const CONTENT_TYPE : & str = "Content-Type" ;
97103 const JSON : & str = "application/json" ;
104+ const user_agent: str = qualified_version ( ) ;
98105
99106 // The 2 following unwraps should not be able to fail
100107
101108 let headers = Headers :: new ( ) . unwrap ( ) ;
102109 headers. append ( "Authorization: Bearer" , apikey) . unwrap ( ) ;
110+ headers. append ( "User-Agent" , & user_agent) . unwrap ( ) ;
103111
104112 let mut request: RequestInit = RequestInit :: new ( ) ;
105113 request. headers ( & headers) ;
@@ -189,3 +197,9 @@ fn parse_response<Output: DeserializeOwned>(
189197 Err ( e) => Err ( Error :: ParseError ( e) ) ,
190198 }
191199}
200+
201+ pub fn qualified_version ( ) -> String {
202+ const VERSION : Option < & str > = option_env ! ( "CARGO_PKG_VERSION" ) ;
203+
204+ format ! ( "Meilisearch Rust (v{})" , VERSION . unwrap_or( "unknown" ) )
205+ }
0 commit comments