Skip to content

Commit ce373f0

Browse files
committed
stops cloning the http client everywhere
1 parent 7c7dd4a commit ce373f0

File tree

8 files changed

+124
-251
lines changed

8 files changed

+124
-251
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,4 @@ lazy_static = "1.4"
5151
web-sys = "0.3"
5252
console_error_panic_hook = "0.1"
5353
big_s = "1.0.2"
54+
insta = "1.38.0"

examples/cli-app-with-reqwest/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub struct ReqwestClient;
2222
#[async_trait(?Send)]
2323
impl HttpClient for ReqwestClient {
2424
async fn request<Query, Body, Output>(
25-
self,
25+
&self,
2626
url: &str,
2727
apikey: Option<&str>,
2828
method: Method<Query, Body>,
@@ -113,7 +113,7 @@ impl HttpClient for ReqwestClient {
113113
Body: futures::AsyncRead + Send + Sync + 'static,
114114
Output: DeserializeOwned + 'static,
115115
>(
116-
self,
116+
&self,
117117
_url: &str,
118118
_apikey: Option<&str>,
119119
_method: Method<Query, Body>,

src/client.rs

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ impl Client {
4747
/// let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
4848
/// ```
4949
pub fn new(host: impl Into<String>, api_key: Option<impl Into<String>>) -> Client {
50-
// TODO: actually create a reqwest client
50+
let api_key = api_key.map(|key| key.into());
51+
let http_client = ReqwestClient::new(api_key.as_deref());
52+
5153
Client {
5254
host: host.into(),
53-
api_key: api_key.map(|api_key| api_key.into()),
54-
http_client: ReqwestClient,
55+
api_key,
56+
http_client,
5557
}
5658
}
5759
}
@@ -94,7 +96,6 @@ impl<Http: HttpClient> Client<Http> {
9496
body: &MultiSearchQuery<'_, '_, Http>,
9597
) -> Result<MultiSearchResponse<T>, Error> {
9698
self.http_client
97-
.clone()
9899
.request::<(), &MultiSearchQuery<Http>, MultiSearchResponse<T>>(
99100
&format!("{}/multi-search", &self.host),
100101
self.get_api_key(),
@@ -255,8 +256,8 @@ impl<Http: HttpClient> Client<Http> {
255256
/// # });
256257
/// ```
257258
pub async fn list_all_indexes_raw(&self) -> Result<Value, Error> {
258-
let http_client = self.http_client.clone();
259-
let json_indexes = http_client
259+
let json_indexes = self
260+
.http_client
260261
.request::<(), (), Value>(
261262
&format!("{}/indexes", self.host),
262263
self.get_api_key(),
@@ -292,8 +293,8 @@ impl<Http: HttpClient> Client<Http> {
292293
&self,
293294
indexes_query: &IndexesQuery<'_, Http>,
294295
) -> Result<Value, Error> {
295-
let http_client = self.http_client.clone();
296-
let json_indexes = http_client
296+
let json_indexes = self
297+
.http_client
297298
.request::<&IndexesQuery<Http>, (), Value>(
298299
&format!("{}/indexes", self.host),
299300
self.get_api_key(),
@@ -355,7 +356,6 @@ impl<Http: HttpClient> Client<Http> {
355356
/// ```
356357
pub async fn get_raw_index(&self, uid: impl AsRef<str>) -> Result<Value, Error> {
357358
self.http_client
358-
.clone()
359359
.request::<(), (), Value>(
360360
&format!("{}/indexes/{}", self.host, uid.as_ref()),
361361
self.get_api_key(),
@@ -404,7 +404,6 @@ impl<Http: HttpClient> Client<Http> {
404404
primary_key: Option<&str>,
405405
) -> Result<TaskInfo, Error> {
406406
self.http_client
407-
.clone()
408407
.request::<(), Value, TaskInfo>(
409408
&format!("{}/indexes", self.host),
410409
self.get_api_key(),
@@ -425,7 +424,6 @@ impl<Http: HttpClient> Client<Http> {
425424
/// To delete an [Index], use the [`Index::delete`] method.
426425
pub async fn delete_index(&self, uid: impl AsRef<str>) -> Result<TaskInfo, Error> {
427426
self.http_client
428-
.clone()
429427
.request::<(), (), TaskInfo>(
430428
&format!("{}/indexes/{}", self.host, uid.as_ref()),
431429
self.get_api_key(),
@@ -498,7 +496,6 @@ impl<Http: HttpClient> Client<Http> {
498496
indexes: impl IntoIterator<Item = &SwapIndexes>,
499497
) -> Result<TaskInfo, Error> {
500498
self.http_client
501-
.clone()
502499
.request::<(), Vec<&SwapIndexes>, TaskInfo>(
503500
&format!("{}/swap-indexes", self.host),
504501
self.get_api_key(),
@@ -528,7 +525,6 @@ impl<Http: HttpClient> Client<Http> {
528525
/// ```
529526
pub async fn get_stats(&self) -> Result<ClientStats, Error> {
530527
self.http_client
531-
.clone()
532528
.request::<(), (), ClientStats>(
533529
&format!("{}/stats", self.host),
534530
self.get_api_key(),
@@ -557,7 +553,6 @@ impl<Http: HttpClient> Client<Http> {
557553
/// ```
558554
pub async fn health(&self) -> Result<Health, Error> {
559555
self.http_client
560-
.clone()
561556
.request::<(), (), Health>(
562557
&format!("{}/health", self.host),
563558
self.get_api_key(),
@@ -615,8 +610,8 @@ impl<Http: HttpClient> Client<Http> {
615610
/// # });
616611
/// ```
617612
pub async fn get_keys_with(&self, keys_query: &KeysQuery) -> Result<KeysResults, Error> {
618-
let http_client = self.http_client.clone();
619-
let keys = http_client
613+
let keys = self
614+
.http_client
620615
.request::<&KeysQuery, (), KeysResults>(
621616
&format!("{}/keys", self.host),
622617
self.get_api_key(),
@@ -648,8 +643,8 @@ impl<Http: HttpClient> Client<Http> {
648643
/// # });
649644
/// ```
650645
pub async fn get_keys(&self) -> Result<KeysResults, Error> {
651-
let http_client = self.http_client.clone();
652-
let keys = http_client
646+
let keys = self
647+
.http_client
653648
.request::<(), (), KeysResults>(
654649
&format!("{}/keys", self.host),
655650
self.get_api_key(),
@@ -677,15 +672,14 @@ impl<Http: HttpClient> Client<Http> {
677672
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
678673
/// # let key = client.get_keys().await.unwrap().results.into_iter()
679674
/// # .find(|k| k.name.as_ref().map_or(false, |name| name.starts_with("Default Search API Key")))
680-
/// # .unwrap();
681-
/// let key = client.get_key(key).await.unwrap();
675+
/// # .expect("No default search key");
676+
/// let key = client.get_key(key).await.expect("Invalid key");
682677
///
683678
/// assert_eq!(key.name, Some("Default Search API Key".to_string()));
684679
/// # });
685680
/// ```
686681
pub async fn get_key(&self, key: impl AsRef<str>) -> Result<Key, Error> {
687682
self.http_client
688-
.clone()
689683
.request::<(), (), Key>(
690684
&format!("{}/keys/{}", self.host, key.as_ref()),
691685
self.get_api_key(),
@@ -722,7 +716,6 @@ impl<Http: HttpClient> Client<Http> {
722716
/// ```
723717
pub async fn delete_key(&self, key: impl AsRef<str>) -> Result<(), Error> {
724718
self.http_client
725-
.clone()
726719
.request::<(), (), ()>(
727720
&format!("{}/keys/{}", self.host, key.as_ref()),
728721
self.get_api_key(),
@@ -758,7 +751,6 @@ impl<Http: HttpClient> Client<Http> {
758751
/// ```
759752
pub async fn create_key(&self, key: impl AsRef<KeyBuilder>) -> Result<Key, Error> {
760753
self.http_client
761-
.clone()
762754
.request::<(), &KeyBuilder, Key>(
763755
&format!("{}/keys", self.host),
764756
self.get_api_key(),
@@ -800,7 +792,6 @@ impl<Http: HttpClient> Client<Http> {
800792
/// ```
801793
pub async fn update_key(&self, key: impl AsRef<KeyUpdater>) -> Result<Key, Error> {
802794
self.http_client
803-
.clone()
804795
.request::<(), &KeyUpdater, Key>(
805796
&format!("{}/keys/{}", self.host, key.as_ref().key),
806797
self.get_api_key(),
@@ -830,7 +821,6 @@ impl<Http: HttpClient> Client<Http> {
830821
/// ```
831822
pub async fn get_version(&self) -> Result<Version, Error> {
832823
self.http_client
833-
.clone()
834824
.request::<(), (), Version>(
835825
&format!("{}/version", self.host),
836826
self.get_api_key(),
@@ -935,7 +925,6 @@ impl<Http: HttpClient> Client<Http> {
935925
/// ```
936926
pub async fn get_task(&self, task_id: impl AsRef<u32>) -> Result<Task, Error> {
937927
self.http_client
938-
.clone()
939928
.request::<(), (), Task>(
940929
&format!("{}/tasks/{}", self.host, task_id.as_ref()),
941930
self.get_api_key(),
@@ -967,8 +956,8 @@ impl<Http: HttpClient> Client<Http> {
967956
&self,
968957
tasks_query: &TasksSearchQuery<'_, Http>,
969958
) -> Result<TasksResults, Error> {
970-
let http_client = self.http_client.clone();
971-
let tasks = http_client
959+
let tasks = self
960+
.http_client
972961
.request::<&TasksSearchQuery<Http>, (), TasksResults>(
973962
&format!("{}/tasks", self.host),
974963
self.get_api_key(),
@@ -1002,8 +991,8 @@ impl<Http: HttpClient> Client<Http> {
1002991
&self,
1003992
filters: &TasksCancelQuery<'_, Http>,
1004993
) -> Result<TaskInfo, Error> {
1005-
let http_client = self.http_client.clone();
1006-
let tasks = http_client
994+
let tasks = self
995+
.http_client
1007996
.request::<&TasksCancelQuery<Http>, (), TaskInfo>(
1008997
&format!("{}/tasks/cancel", self.host),
1009998
self.get_api_key(),
@@ -1040,8 +1029,8 @@ impl<Http: HttpClient> Client<Http> {
10401029
&self,
10411030
filters: &TasksDeleteQuery<'_, Http>,
10421031
) -> Result<TaskInfo, Error> {
1043-
let http_client = self.http_client.clone();
1044-
let tasks = http_client
1032+
let tasks = self
1033+
.http_client
10451034
.request::<&TasksDeleteQuery<Http>, (), TaskInfo>(
10461035
&format!("{}/tasks", self.host),
10471036
self.get_api_key(),
@@ -1071,8 +1060,8 @@ impl<Http: HttpClient> Client<Http> {
10711060
/// # });
10721061
/// ```
10731062
pub async fn get_tasks(&self) -> Result<TasksResults, Error> {
1074-
let http_client = self.http_client.clone();
1075-
let tasks = http_client
1063+
let tasks = self
1064+
.http_client
10761065
.request::<(), (), TasksResults>(
10771066
&format!("{}/tasks", self.host),
10781067
self.get_api_key(),
@@ -1358,14 +1347,7 @@ mod tests {
13581347
async fn test_error_delete_key(mut client: Client, name: String) {
13591348
// ==> accessing a key that does not exist
13601349
let error = client.delete_key("invalid_key").await.unwrap_err();
1361-
assert!(matches!(
1362-
error,
1363-
Error::Meilisearch(MeilisearchError {
1364-
error_code: ErrorCode::ApiKeyNotFound,
1365-
error_type: ErrorType::InvalidRequest,
1366-
..
1367-
})
1368-
));
1350+
insta::assert_snapshot!(error, @"Meilisearch invalid_request: api_key_not_found: API key `invalid_key` not found.. https://docs.meilisearch.com/errors#api_key_not_found");
13691351

13701352
// ==> executing the action without enough right
13711353
let mut key = KeyBuilder::new();
@@ -1377,6 +1359,7 @@ mod tests {
13771359
client.api_key = Some(key.key.clone());
13781360
// with a wrong key
13791361
let error = client.delete_key("invalid_key").await.unwrap_err();
1362+
insta::assert_snapshot!(error, @"Meilisearch auth: invalid_api_key: The provided API key is invalid.. https://docs.meilisearch.com/errors#invalid_api_key");
13801363
assert!(matches!(
13811364
error,
13821365
Error::Meilisearch(MeilisearchError {
@@ -1387,6 +1370,7 @@ mod tests {
13871370
));
13881371
// with a good key
13891372
let error = client.delete_key(&key.key).await.unwrap_err();
1373+
insta::assert_snapshot!(error, @"Meilisearch auth: invalid_api_key: The provided API key is invalid.. https://docs.meilisearch.com/errors#invalid_api_key");
13901374
assert!(matches!(
13911375
error,
13921376
Error::Meilisearch(MeilisearchError {

src/dumps.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ impl<Http: HttpClient> Client<Http> {
7474
/// ```
7575
pub async fn create_dump(&self) -> Result<TaskInfo, Error> {
7676
self.http_client
77-
.clone()
7877
.request::<(), (), TaskInfo>(
7978
&format!("{}/dumps", self.host),
8079
self.get_api_key(),

src/features.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ impl<'a, Http: HttpClient> ExperimentalFeatures<'a, Http> {
6666
pub async fn get(&self) -> Result<ExperimentalFeaturesResult, Error> {
6767
self.client
6868
.http_client
69-
.clone()
7069
.request::<(), (), ExperimentalFeaturesResult>(
7170
&format!("{}/experimental-features", self.client.host),
7271
self.client.get_api_key(),
@@ -94,7 +93,6 @@ impl<'a, Http: HttpClient> ExperimentalFeatures<'a, Http> {
9493
pub async fn update(&self) -> Result<ExperimentalFeaturesResult, Error> {
9594
self.client
9695
.http_client
97-
.clone()
9896
.request::<(), &Self, ExperimentalFeaturesResult>(
9997
&format!("{}/experimental-features", self.client.host),
10098
self.client.get_api_key(),

0 commit comments

Comments
 (0)