@@ -47,11 +47,13 @@ impl Client {
47
47
/// let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
48
48
/// ```
49
49
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
+
51
53
Client {
52
54
host : host. into ( ) ,
53
- api_key : api_key . map ( |api_key| api_key . into ( ) ) ,
54
- http_client : ReqwestClient ,
55
+ api_key,
56
+ http_client,
55
57
}
56
58
}
57
59
}
@@ -94,7 +96,6 @@ impl<Http: HttpClient> Client<Http> {
94
96
body : & MultiSearchQuery < ' _ , ' _ , Http > ,
95
97
) -> Result < MultiSearchResponse < T > , Error > {
96
98
self . http_client
97
- . clone ( )
98
99
. request :: < ( ) , & MultiSearchQuery < Http > , MultiSearchResponse < T > > (
99
100
& format ! ( "{}/multi-search" , & self . host) ,
100
101
self . get_api_key ( ) ,
@@ -255,8 +256,8 @@ impl<Http: HttpClient> Client<Http> {
255
256
/// # });
256
257
/// ```
257
258
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
260
261
. request :: < ( ) , ( ) , Value > (
261
262
& format ! ( "{}/indexes" , self . host) ,
262
263
self . get_api_key ( ) ,
@@ -292,8 +293,8 @@ impl<Http: HttpClient> Client<Http> {
292
293
& self ,
293
294
indexes_query : & IndexesQuery < ' _ , Http > ,
294
295
) -> 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
297
298
. request :: < & IndexesQuery < Http > , ( ) , Value > (
298
299
& format ! ( "{}/indexes" , self . host) ,
299
300
self . get_api_key ( ) ,
@@ -355,7 +356,6 @@ impl<Http: HttpClient> Client<Http> {
355
356
/// ```
356
357
pub async fn get_raw_index ( & self , uid : impl AsRef < str > ) -> Result < Value , Error > {
357
358
self . http_client
358
- . clone ( )
359
359
. request :: < ( ) , ( ) , Value > (
360
360
& format ! ( "{}/indexes/{}" , self . host, uid. as_ref( ) ) ,
361
361
self . get_api_key ( ) ,
@@ -404,7 +404,6 @@ impl<Http: HttpClient> Client<Http> {
404
404
primary_key : Option < & str > ,
405
405
) -> Result < TaskInfo , Error > {
406
406
self . http_client
407
- . clone ( )
408
407
. request :: < ( ) , Value , TaskInfo > (
409
408
& format ! ( "{}/indexes" , self . host) ,
410
409
self . get_api_key ( ) ,
@@ -425,7 +424,6 @@ impl<Http: HttpClient> Client<Http> {
425
424
/// To delete an [Index], use the [`Index::delete`] method.
426
425
pub async fn delete_index ( & self , uid : impl AsRef < str > ) -> Result < TaskInfo , Error > {
427
426
self . http_client
428
- . clone ( )
429
427
. request :: < ( ) , ( ) , TaskInfo > (
430
428
& format ! ( "{}/indexes/{}" , self . host, uid. as_ref( ) ) ,
431
429
self . get_api_key ( ) ,
@@ -498,7 +496,6 @@ impl<Http: HttpClient> Client<Http> {
498
496
indexes : impl IntoIterator < Item = & SwapIndexes > ,
499
497
) -> Result < TaskInfo , Error > {
500
498
self . http_client
501
- . clone ( )
502
499
. request :: < ( ) , Vec < & SwapIndexes > , TaskInfo > (
503
500
& format ! ( "{}/swap-indexes" , self . host) ,
504
501
self . get_api_key ( ) ,
@@ -528,7 +525,6 @@ impl<Http: HttpClient> Client<Http> {
528
525
/// ```
529
526
pub async fn get_stats ( & self ) -> Result < ClientStats , Error > {
530
527
self . http_client
531
- . clone ( )
532
528
. request :: < ( ) , ( ) , ClientStats > (
533
529
& format ! ( "{}/stats" , self . host) ,
534
530
self . get_api_key ( ) ,
@@ -557,7 +553,6 @@ impl<Http: HttpClient> Client<Http> {
557
553
/// ```
558
554
pub async fn health ( & self ) -> Result < Health , Error > {
559
555
self . http_client
560
- . clone ( )
561
556
. request :: < ( ) , ( ) , Health > (
562
557
& format ! ( "{}/health" , self . host) ,
563
558
self . get_api_key ( ) ,
@@ -615,8 +610,8 @@ impl<Http: HttpClient> Client<Http> {
615
610
/// # });
616
611
/// ```
617
612
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
620
615
. request :: < & KeysQuery , ( ) , KeysResults > (
621
616
& format ! ( "{}/keys" , self . host) ,
622
617
self . get_api_key ( ) ,
@@ -648,8 +643,8 @@ impl<Http: HttpClient> Client<Http> {
648
643
/// # });
649
644
/// ```
650
645
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
653
648
. request :: < ( ) , ( ) , KeysResults > (
654
649
& format ! ( "{}/keys" , self . host) ,
655
650
self . get_api_key ( ) ,
@@ -677,15 +672,14 @@ impl<Http: HttpClient> Client<Http> {
677
672
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
678
673
/// # let key = client.get_keys().await.unwrap().results.into_iter()
679
674
/// # .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" );
682
677
///
683
678
/// assert_eq!(key.name, Some("Default Search API Key".to_string()));
684
679
/// # });
685
680
/// ```
686
681
pub async fn get_key ( & self , key : impl AsRef < str > ) -> Result < Key , Error > {
687
682
self . http_client
688
- . clone ( )
689
683
. request :: < ( ) , ( ) , Key > (
690
684
& format ! ( "{}/keys/{}" , self . host, key. as_ref( ) ) ,
691
685
self . get_api_key ( ) ,
@@ -722,7 +716,6 @@ impl<Http: HttpClient> Client<Http> {
722
716
/// ```
723
717
pub async fn delete_key ( & self , key : impl AsRef < str > ) -> Result < ( ) , Error > {
724
718
self . http_client
725
- . clone ( )
726
719
. request :: < ( ) , ( ) , ( ) > (
727
720
& format ! ( "{}/keys/{}" , self . host, key. as_ref( ) ) ,
728
721
self . get_api_key ( ) ,
@@ -758,7 +751,6 @@ impl<Http: HttpClient> Client<Http> {
758
751
/// ```
759
752
pub async fn create_key ( & self , key : impl AsRef < KeyBuilder > ) -> Result < Key , Error > {
760
753
self . http_client
761
- . clone ( )
762
754
. request :: < ( ) , & KeyBuilder , Key > (
763
755
& format ! ( "{}/keys" , self . host) ,
764
756
self . get_api_key ( ) ,
@@ -800,7 +792,6 @@ impl<Http: HttpClient> Client<Http> {
800
792
/// ```
801
793
pub async fn update_key ( & self , key : impl AsRef < KeyUpdater > ) -> Result < Key , Error > {
802
794
self . http_client
803
- . clone ( )
804
795
. request :: < ( ) , & KeyUpdater , Key > (
805
796
& format ! ( "{}/keys/{}" , self . host, key. as_ref( ) . key) ,
806
797
self . get_api_key ( ) ,
@@ -830,7 +821,6 @@ impl<Http: HttpClient> Client<Http> {
830
821
/// ```
831
822
pub async fn get_version ( & self ) -> Result < Version , Error > {
832
823
self . http_client
833
- . clone ( )
834
824
. request :: < ( ) , ( ) , Version > (
835
825
& format ! ( "{}/version" , self . host) ,
836
826
self . get_api_key ( ) ,
@@ -935,7 +925,6 @@ impl<Http: HttpClient> Client<Http> {
935
925
/// ```
936
926
pub async fn get_task ( & self , task_id : impl AsRef < u32 > ) -> Result < Task , Error > {
937
927
self . http_client
938
- . clone ( )
939
928
. request :: < ( ) , ( ) , Task > (
940
929
& format ! ( "{}/tasks/{}" , self . host, task_id. as_ref( ) ) ,
941
930
self . get_api_key ( ) ,
@@ -967,8 +956,8 @@ impl<Http: HttpClient> Client<Http> {
967
956
& self ,
968
957
tasks_query : & TasksSearchQuery < ' _ , Http > ,
969
958
) -> Result < TasksResults , Error > {
970
- let http_client = self . http_client . clone ( ) ;
971
- let tasks = http_client
959
+ let tasks = self
960
+ . http_client
972
961
. request :: < & TasksSearchQuery < Http > , ( ) , TasksResults > (
973
962
& format ! ( "{}/tasks" , self . host) ,
974
963
self . get_api_key ( ) ,
@@ -1002,8 +991,8 @@ impl<Http: HttpClient> Client<Http> {
1002
991
& self ,
1003
992
filters : & TasksCancelQuery < ' _ , Http > ,
1004
993
) -> Result < TaskInfo , Error > {
1005
- let http_client = self . http_client . clone ( ) ;
1006
- let tasks = http_client
994
+ let tasks = self
995
+ . http_client
1007
996
. request :: < & TasksCancelQuery < Http > , ( ) , TaskInfo > (
1008
997
& format ! ( "{}/tasks/cancel" , self . host) ,
1009
998
self . get_api_key ( ) ,
@@ -1040,8 +1029,8 @@ impl<Http: HttpClient> Client<Http> {
1040
1029
& self ,
1041
1030
filters : & TasksDeleteQuery < ' _ , Http > ,
1042
1031
) -> Result < TaskInfo , Error > {
1043
- let http_client = self . http_client . clone ( ) ;
1044
- let tasks = http_client
1032
+ let tasks = self
1033
+ . http_client
1045
1034
. request :: < & TasksDeleteQuery < Http > , ( ) , TaskInfo > (
1046
1035
& format ! ( "{}/tasks" , self . host) ,
1047
1036
self . get_api_key ( ) ,
@@ -1071,8 +1060,8 @@ impl<Http: HttpClient> Client<Http> {
1071
1060
/// # });
1072
1061
/// ```
1073
1062
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
1076
1065
. request :: < ( ) , ( ) , TasksResults > (
1077
1066
& format ! ( "{}/tasks" , self . host) ,
1078
1067
self . get_api_key ( ) ,
@@ -1358,14 +1347,7 @@ mod tests {
1358
1347
async fn test_error_delete_key ( mut client : Client , name : String ) {
1359
1348
// ==> accessing a key that does not exist
1360
1349
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" ) ;
1369
1351
1370
1352
// ==> executing the action without enough right
1371
1353
let mut key = KeyBuilder :: new ( ) ;
@@ -1377,6 +1359,7 @@ mod tests {
1377
1359
client. api_key = Some ( key. key . clone ( ) ) ;
1378
1360
// with a wrong key
1379
1361
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" ) ;
1380
1363
assert ! ( matches!(
1381
1364
error,
1382
1365
Error :: Meilisearch ( MeilisearchError {
@@ -1387,6 +1370,7 @@ mod tests {
1387
1370
) ) ;
1388
1371
// with a good key
1389
1372
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" ) ;
1390
1374
assert ! ( matches!(
1391
1375
error,
1392
1376
Error :: Meilisearch ( MeilisearchError {
0 commit comments