Skip to content

Commit c57b5de

Browse files
committed
Send qualified User-Agent to Meilisearch server
1 parent 9afbab2 commit c57b5de

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/client.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,46 @@ mod tests {
637637
};
638638
use meilisearch_test_macro::meilisearch_test;
639639
use time::OffsetDateTime;
640+
use mockito::mock;
641+
use std::mem;
642+
643+
#[meilisearch_test]
644+
async fn test_methods_has_qualified_version_as_header() {
645+
let mock_server_url = &mockito::server_url();
646+
let path = "/hello";
647+
let address = &format!("{}{}", mock_server_url, path);
648+
let user_agent = &*qualified_version();
649+
650+
let assertions = vec![
651+
(
652+
mock("GET", path).match_header("User-Agent", user_agent).create(),
653+
request::<String, ()>(address, "", Method::Get, 200)
654+
),
655+
(
656+
mock("POST", path).match_header("User-Agent", user_agent).create(),
657+
request::<String, ()>(address, "", Method::Post("".to_string()), 200)
658+
),
659+
(
660+
mock("DELETE", path).match_header("User-Agent", user_agent).create(),
661+
request::<String, ()>(address, "", Method::Delete, 200)
662+
),
663+
(
664+
mock("PUT", path).match_header("User-Agent", user_agent).create(),
665+
request::<String, ()>(address, "", Method::Put("".to_string()), 200)
666+
),
667+
(
668+
mock("PATCH", path).match_header("User-Agent", user_agent).create(),
669+
request::<String, ()>(address, "", Method::Patch("".to_string()), 200)
670+
)
671+
];
672+
673+
for (m, req) in assertions {
674+
let _ = req.await;
675+
676+
m.assert();
677+
mem::drop(m);
678+
}
679+
}
640680

641681
#[meilisearch_test]
642682
async fn test_get_keys(client: Client) {

src/request.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ pub(crate) async fn request<
2828
trace!("{:?} on {}", method, url);
2929

3030
let auth = format!("Bearer {}", apikey);
31+
let user_agent = qualified_version();
3132

3233
let mut response = match &method {
3334
Method::Get => {
3435
Request::get(url)
3536
.header(header::AUTHORIZATION, auth)
37+
.header(header::USER_AGENT, user_agent)
3638
.body(())
3739
.map_err(|_| crate::errors::Error::InvalidRequest)?
3840
.send_async()
@@ -41,6 +43,7 @@ pub(crate) async fn request<
4143
Method::Delete => {
4244
Request::delete(url)
4345
.header(header::AUTHORIZATION, auth)
46+
.header(header::USER_AGENT, user_agent)
4447
.body(())
4548
.map_err(|_| crate::errors::Error::InvalidRequest)?
4649
.send_async()
@@ -50,6 +53,7 @@ pub(crate) async fn request<
5053
Request::post(url)
5154
.header(header::AUTHORIZATION, auth)
5255
.header(header::CONTENT_TYPE, "application/json")
56+
.header(header::USER_AGENT, user_agent)
5357
.body(to_string(&body).unwrap())
5458
.map_err(|_| crate::errors::Error::InvalidRequest)?
5559
.send_async()
@@ -59,6 +63,7 @@ pub(crate) async fn request<
5963
Request::patch(url)
6064
.header(header::AUTHORIZATION, auth)
6165
.header(header::CONTENT_TYPE, "application/json")
66+
.header(header::USER_AGENT, user_agent)
6267
.body(to_string(&body).unwrap())
6368
.map_err(|_| crate::errors::Error::InvalidRequest)?
6469
.send_async()
@@ -68,6 +73,7 @@ pub(crate) async fn request<
6873
Request::put(url)
6974
.header(header::AUTHORIZATION, auth)
7075
.header(header::CONTENT_TYPE, "application/json")
76+
.header(header::USER_AGENT, user_agent)
7177
.body(to_string(&body).unwrap())
7278
.map_err(|_| crate::errors::Error::InvalidRequest)?
7379
.send_async()
@@ -105,11 +111,13 @@ pub(crate) async fn request<
105111

106112
const CONTENT_TYPE: &str = "Content-Type";
107113
const JSON: &str = "application/json";
114+
const user_agent: str = qualified_version();
108115

109116
// The 2 following unwraps should not be able to fail
110117

111118
let headers = Headers::new().unwrap();
112119
headers.append("Authorization: Bearer", apikey).unwrap();
120+
headers.append("User-Agent", &user_agent).unwrap();
113121

114122
let mut request: RequestInit = RequestInit::new();
115123
request.headers(&headers);
@@ -199,3 +207,9 @@ fn parse_response<Output: DeserializeOwned>(
199207
Err(e) => Err(Error::ParseError(e)),
200208
}
201209
}
210+
211+
pub fn qualified_version() -> String {
212+
const VERSION: Option<&str> = option_env!("CARGO_PKG_VERSION");
213+
214+
format!("Meilisearch Rust (v{})", VERSION.unwrap_or("unknown"))
215+
}

0 commit comments

Comments
 (0)