Skip to content

Commit

Permalink
Merge #254
Browse files Browse the repository at this point in the history
254: Feature/Analytics r=brunoocasali a=brunoocasali

- Add `User-Agent` inside the pre-defined headers.

Add Rust support as requested here meilisearch/integration-guides#150


Co-authored-by: Bruno Casali <brunoocasali@gmail.com>
  • Loading branch information
bors[bot] and brunoocasali authored May 6, 2022
2 parents 4e8181a + 21afaa0 commit b8a12a4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ sync = []
env_logger = "0.9"
futures-await-test = "0.3"
futures = "0.3"
mockito = "0.31.0"
meilisearch-test-macro = { path = "meilisearch-test-macro" }
tokio = { version = "1", features = ["rt", "macros"] }

Expand Down
40 changes: 40 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,46 @@ mod tests {
};
use meilisearch_test_macro::meilisearch_test;
use time::OffsetDateTime;
use mockito::mock;
use std::mem;

#[meilisearch_test]
async fn test_methods_has_qualified_version_as_header() {
let mock_server_url = &mockito::server_url();
let path = "/hello";
let address = &format!("{}{}", mock_server_url, path);
let user_agent = &*qualified_version();

let assertions = vec![
(
mock("GET", path).match_header("User-Agent", user_agent).create(),
request::<String, ()>(address, "", Method::Get, 200)
),
(
mock("POST", path).match_header("User-Agent", user_agent).create(),
request::<String, ()>(address, "", Method::Post("".to_string()), 200)
),
(
mock("DELETE", path).match_header("User-Agent", user_agent).create(),
request::<String, ()>(address, "", Method::Delete, 200)
),
(
mock("PUT", path).match_header("User-Agent", user_agent).create(),
request::<String, ()>(address, "", Method::Put("".to_string()), 200)
),
(
mock("PATCH", path).match_header("User-Agent", user_agent).create(),
request::<String, ()>(address, "", Method::Patch("".to_string()), 200)
)
];

for (m, req) in assertions {
let _ = req.await;

m.assert();
mem::drop(m);
}
}

#[meilisearch_test]
async fn test_get_keys(client: Client) {
Expand Down
14 changes: 14 additions & 0 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ pub(crate) async fn request<Input: Serialize, Output: DeserializeOwned + 'static
use isahc::*;

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

let mut response = match &method {
Method::Get => {
Request::get(url)
.header(header::AUTHORIZATION, auth)
.header(header::USER_AGENT, user_agent)
.body(())
.map_err(|_| crate::errors::Error::InvalidRequest)?
.send_async()
Expand All @@ -36,6 +38,7 @@ pub(crate) async fn request<Input: Serialize, Output: DeserializeOwned + 'static
Method::Delete => {
Request::delete(url)
.header(header::AUTHORIZATION, auth)
.header(header::USER_AGENT, user_agent)
.body(())
.map_err(|_| crate::errors::Error::InvalidRequest)?
.send_async()
Expand All @@ -45,6 +48,7 @@ pub(crate) async fn request<Input: Serialize, Output: DeserializeOwned + 'static
Request::post(url)
.header(header::AUTHORIZATION, auth)
.header(header::CONTENT_TYPE, "application/json")
.header(header::USER_AGENT, user_agent)
.body(to_string(&body).unwrap())
.map_err(|_| crate::errors::Error::InvalidRequest)?
.send_async()
Expand All @@ -54,6 +58,7 @@ pub(crate) async fn request<Input: Serialize, Output: DeserializeOwned + 'static
Request::patch(url)
.header(header::AUTHORIZATION, auth)
.header(header::CONTENT_TYPE, "application/json")
.header(header::USER_AGENT, user_agent)
.body(to_string(&body).unwrap())
.map_err(|_| crate::errors::Error::InvalidRequest)?
.send_async()
Expand All @@ -63,6 +68,7 @@ pub(crate) async fn request<Input: Serialize, Output: DeserializeOwned + 'static
Request::put(url)
.header(header::AUTHORIZATION, auth)
.header(header::CONTENT_TYPE, "application/json")
.header(header::USER_AGENT, user_agent)
.body(to_string(&body).unwrap())
.map_err(|_| crate::errors::Error::InvalidRequest)?
.send_async()
Expand Down Expand Up @@ -95,11 +101,13 @@ pub(crate) async fn request<Input: Serialize, Output: DeserializeOwned + 'static

const CONTENT_TYPE: &str = "Content-Type";
const JSON: &str = "application/json";
let user_agent = qualified_version();

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

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

let mut request: RequestInit = RequestInit::new();
request.headers(&headers);
Expand Down Expand Up @@ -189,3 +197,9 @@ fn parse_response<Output: DeserializeOwned>(
Err(e) => Err(Error::ParseError(e)),
}
}

pub fn qualified_version() -> String {
const VERSION: Option<&str> = option_env!("CARGO_PKG_VERSION");

format!("Meilisearch Rust (v{})", VERSION.unwrap_or("unknown"))
}

0 comments on commit b8a12a4

Please sign in to comment.