Skip to content

Commit 8f7ec25

Browse files
committed
replace isahc by reqwest
1 parent 80f6326 commit 8f7ec25

File tree

15 files changed

+273
-360
lines changed

15 files changed

+273
-360
lines changed

Cargo.toml

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,18 @@ yaup = "0.2.0"
2323
either = { version = "1.8.0", features = ["serde"] }
2424
thiserror = "1.0.37"
2525
meilisearch-index-setting-macro = { path = "meilisearch-index-setting-macro", version = "0.25.0" }
26-
27-
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
28-
futures = "0.3"
29-
futures-io = "0.3.26"
30-
isahc = { version = "1.0", features = ["http2", "text-decoding"], optional = true, default_features = false }
26+
tokio-util = { version = "0.7.10", default-features = false, features = ["io", "compat"] }
27+
reqwest = { version = "0.12.3", optional = true, default-features = false, features = ["rustls-tls", "http2", "stream"] }
3128
uuid = { version = "1.1.2", features = ["v4"] }
29+
futures-io = "0.3.30"
30+
futures = "0.3"
3231

3332
[target.'cfg(target_arch = "wasm32")'.dependencies]
34-
js-sys = "0.3.47"
35-
web-sys = { version = "0.3", features = ["RequestInit", "Headers", "Window", "Response", "console"] }
36-
wasm-bindgen = "0.2"
37-
wasm-bindgen-futures = "0.4"
33+
uuid = { version = "1.8.0", default-features = false, features = ["v4", "js"] }
3834

3935
[features]
40-
default = ["isahc", "isahc", "isahc-static-curl"]
41-
isahc-static-curl = ["isahc", "isahc", "isahc/static-curl"]
42-
isahc-static-ssl = ["isahc/static-ssl"]
36+
default = ["reqwest"]
37+
reqwest = ["dep:reqwest"]
4338

4439
[dev-dependencies]
4540
futures-await-test = "0.3"

meilisearch-index-setting-macro/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ fn get_index_config_implementation(
128128

129129
quote! {
130130
#[::meilisearch_sdk::macro_helper::async_trait(?Send)]
131-
impl ::meilisearch_sdk::documents::IndexConfig<::meilisearch_sdk::request::IsahcClient> for #struct_ident {
131+
impl ::meilisearch_sdk::documents::IndexConfig<::meilisearch_sdk::request::ReqwestClient> for #struct_ident {
132132
const INDEX_STR: &'static str = #index_name;
133133

134134
fn generate_settings() -> ::meilisearch_sdk::settings::Settings {
@@ -140,7 +140,7 @@ fn get_index_config_implementation(
140140
#distinct_attr_token
141141
}
142142

143-
async fn generate_index(client: &::meilisearch_sdk::client::Client<::meilisearch_sdk::request::IsahcClient>) -> std::result::Result<::meilisearch_sdk::indexes::Index<::meilisearch_sdk::request::IsahcClient>, ::meilisearch_sdk::tasks::Task> {
143+
async fn generate_index(client: &::meilisearch_sdk::client::Client<::meilisearch_sdk::request::ReqwestClient>) -> std::result::Result<::meilisearch_sdk::indexes::Index<::meilisearch_sdk::request::ReqwestClient>, ::meilisearch_sdk::tasks::Task> {
144144
return client.create_index(#index_name, #primary_key_token)
145145
.await.unwrap()
146146
.wait_for_completion(&client, ::std::option::Option::None, ::std::option::Option::None)

src/client.rs

Lines changed: 29 additions & 57 deletions
Large diffs are not rendered by default.

src/documents.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl<'a, Http: HttpClient> DocumentQuery<'a, Http> {
129129
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
130130
/// #
131131
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
132-
/// # futures::executor::block_on(async move {
132+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
133133
/// #[derive(Debug, Serialize, Deserialize, PartialEq)]
134134
/// struct MyObject {
135135
/// id: String,
@@ -294,7 +294,7 @@ impl<'a, Http: HttpClient> DocumentsQuery<'a, Http> {
294294
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
295295
/// #
296296
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
297-
/// # futures::executor::block_on(async move {
297+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
298298
/// # let index = client.create_index("documents_query_execute", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap().try_make_index(&client).unwrap();
299299
/// #[derive(Debug, Serialize, Deserialize, PartialEq)]
300300
/// struct MyObject {
@@ -355,7 +355,7 @@ impl<'a, Http: HttpClient> DocumentDeletionQuery<'a, Http> {
355355
#[cfg(test)]
356356
mod tests {
357357
use super::*;
358-
use crate::{client::Client, errors::*, indexes::*, request::IsahcClient};
358+
use crate::{client::Client, errors::*, indexes::*};
359359
use meilisearch_test_macro::meilisearch_test;
360360
use serde::{Deserialize, Serialize};
361361

@@ -367,10 +367,7 @@ mod tests {
367367

368368
#[allow(unused)]
369369
#[derive(IndexConfig)]
370-
struct MovieClips
371-
where
372-
IsahcClient: HttpClient,
373-
{
370+
struct MovieClips {
374371
#[index_config(primary_key)]
375372
movie_id: u64,
376373
#[index_config(distinct)]
@@ -387,10 +384,7 @@ mod tests {
387384

388385
#[allow(unused)]
389386
#[derive(IndexConfig)]
390-
struct VideoClips
391-
where
392-
IsahcClient: HttpClient,
393-
{
387+
struct VideoClips {
394388
video_id: u64,
395389
}
396390

src/dumps.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//! # use meilisearch_sdk::{client::*, errors::*, dumps::*, dumps::*, task_info::*, tasks::*};
2020
//! # use futures_await_test::async_test;
2121
//! # use std::{thread::sleep, time::Duration};
22-
//! # futures::executor::block_on(async move {
22+
//! # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
2323
//! #
2424
//! # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
2525
//! # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
@@ -54,7 +54,7 @@ impl<Http: HttpClient> Client<Http> {
5454
/// # use meilisearch_sdk::{client::*, errors::*, dumps::*, dumps::*, task_info::*, tasks::*};
5555
/// # use futures_await_test::async_test;
5656
/// # use std::{thread::sleep, time::Duration};
57-
/// # futures::executor::block_on(async move {
57+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
5858
/// #
5959
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
6060
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");

src/errors.rs

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ pub enum Error {
1313
Meilisearch(#[from] MeilisearchError),
1414
#[error(transparent)]
1515
MeilisearchCommunication(#[from] MeilisearchCommunicationError),
16-
/// There is no Meilisearch server listening on the [specified host]
17-
/// (../client/struct.Client.html#method.new).
18-
#[error("The Meilisearch server can't be reached.")]
19-
UnreachableServer,
2016
/// The Meilisearch server returned an invalid JSON for a request.
2117
#[error("Error parsing response JSON: {}", .0)]
2218
ParseError(#[from] serde_json::Error),
@@ -46,15 +42,9 @@ pub enum Error {
4642
InvalidTenantToken(#[from] jsonwebtoken::errors::Error),
4743

4844
/// The http client encountered an error.
49-
#[cfg(feature = "isahc")]
50-
#[cfg(not(target_arch = "wasm32"))]
51-
#[error("HTTP request failed: {}", .0)]
52-
HttpError(isahc::Error),
53-
54-
/// The http client encountered an error.
55-
#[cfg(target_arch = "wasm32")]
45+
#[cfg(feature = "reqwest")]
5646
#[error("HTTP request failed: {}", .0)]
57-
HttpError(String),
47+
HttpError(#[from] reqwest::Error),
5848

5949
// The library formatting the query parameters encountered an error.
6050
#[error("Internal Error: could not parse the query parameters: {}", .0)]
@@ -277,27 +267,16 @@ impl std::fmt::Display for ErrorCode {
277267
}
278268
}
279269

280-
#[cfg(feature = "isahc")]
281-
#[cfg(not(target_arch = "wasm32"))]
282-
impl From<isahc::Error> for Error {
283-
fn from(error: isahc::Error) -> Error {
284-
if error.kind() == isahc::error::ErrorKind::ConnectionFailed {
285-
Error::UnreachableServer
286-
} else {
287-
Error::HttpError(error)
288-
}
289-
}
290-
}
291-
292270
#[cfg(test)]
293271
mod test {
294272
use super::*;
295273

296274
use jsonwebtoken::errors::ErrorKind::InvalidToken;
275+
use meilisearch_test_macro::meilisearch_test;
297276
use uuid::Uuid;
298277

299-
#[test]
300-
fn test_meilisearch_error() {
278+
#[meilisearch_test]
279+
async fn test_meilisearch_error() {
301280
let error: MeilisearchError = serde_json::from_str(
302281
r#"
303282
{
@@ -329,8 +308,8 @@ mod test {
329308
assert_eq!(error.error_type, ErrorType::Unknown);
330309
}
331310

332-
#[test]
333-
fn test_error_message_parsing() {
311+
#[meilisearch_test]
312+
async fn test_error_message_parsing() {
334313
let error: MeilisearchError = serde_json::from_str(
335314
r#"
336315
{
@@ -366,12 +345,6 @@ mod test {
366345
"MeilisearchCommunicationError: The server responded with a 404.\nurl: http://localhost:7700/something"
367346
);
368347

369-
let error = Error::UnreachableServer;
370-
assert_eq!(
371-
error.to_string(),
372-
"The Meilisearch server can't be reached."
373-
);
374-
375348
let error = Error::Timeout;
376349
assert_eq!(error.to_string(), "A task did not succeed in time.");
377350

@@ -411,10 +384,19 @@ mod test {
411384
"Error parsing response JSON: invalid type: map, expected a string at line 2 column 8"
412385
);
413386

414-
let error = Error::HttpError(isahc::post("test_url", "test_body").unwrap_err());
387+
let error = Error::HttpError(
388+
reqwest::Client::new()
389+
.execute(reqwest::Request::new(
390+
reqwest::Method::POST,
391+
// there will never be a `meilisearch.gouv.fr` addr since these domain name are controlled by the state of france
392+
reqwest::Url::parse("https://meilisearch.gouv.fr").unwrap(),
393+
))
394+
.await
395+
.unwrap_err(),
396+
);
415397
assert_eq!(
416398
error.to_string(),
417-
"HTTP request failed: failed to resolve host name"
399+
"HTTP request failed: error sending request for url (https://meilisearch.gouv.fr/)"
418400
);
419401

420402
let error = Error::InvalidTenantToken(jsonwebtoken::errors::Error::from(InvalidToken));

src/features.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<'a, Http: HttpClient> ExperimentalFeatures<'a, Http> {
5858
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
5959
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
6060
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
61-
/// futures::executor::block_on(async move {
61+
/// tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
6262
/// let features = ExperimentalFeatures::new(&client);
6363
/// features.get().await.unwrap();
6464
/// });
@@ -85,7 +85,7 @@ impl<'a, Http: HttpClient> ExperimentalFeatures<'a, Http> {
8585
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
8686
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
8787
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
88-
/// futures::executor::block_on(async move {
88+
/// tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
8989
/// let mut features = ExperimentalFeatures::new(&client);
9090
/// features.set_vector_store(true);
9191
/// features.update().await.unwrap();

0 commit comments

Comments
 (0)