@@ -41,7 +41,23 @@ impl Client {
41
41
}
42
42
}
43
43
44
- /// List all [Index]es and returns values as instances of [Index].
44
+ fn parse_indexes_results_from_value ( & self , value : Value ) -> Result < IndexesResults , Error > {
45
+ let raw_indexes = value[ "results" ] . as_array ( ) . unwrap ( ) ;
46
+
47
+ let indexes_results = IndexesResults {
48
+ limit : value[ "limit" ] . as_u64 ( ) . unwrap ( ) as u32 ,
49
+ offset : value[ "offset" ] . as_u64 ( ) . unwrap ( ) as u32 ,
50
+ total : value[ "total" ] . as_u64 ( ) . unwrap ( ) as u32 ,
51
+ results : raw_indexes
52
+ . iter ( )
53
+ . map ( |raw_index| Index :: from_value ( raw_index. clone ( ) , self . clone ( ) ) )
54
+ . collect :: < Result < _ , _ > > ( ) ?,
55
+ } ;
56
+
57
+ Ok ( indexes_results)
58
+ }
59
+
60
+ /// List all [Index]es with query parameters and returns values as instances of [Index].
45
61
///
46
62
/// # Example
47
63
///
@@ -55,16 +71,44 @@ impl Client {
55
71
/// // create the client
56
72
/// let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
57
73
///
58
- /// let indexes: Vec<Index> = client.list_all_indexes().await.unwrap();
74
+ /// let indexes: IndexesResults = client.list_all_indexes().await.unwrap();
59
75
/// println!("{:?}", indexes);
60
76
/// # });
61
77
/// ```
62
- pub async fn list_all_indexes ( & self ) -> Result < Vec < Index > , Error > {
63
- self . list_all_indexes_raw ( )
64
- . await ?
65
- . into_iter ( )
66
- . map ( |index| Index :: from_value ( index, self . clone ( ) ) )
67
- . collect ( )
78
+ pub async fn list_all_indexes ( & self ) -> Result < IndexesResults , Error > {
79
+ let value = self . list_all_indexes_raw ( ) . await ?;
80
+ let indexes_results = self . parse_indexes_results_from_value ( value) ?;
81
+ Ok ( indexes_results)
82
+ }
83
+
84
+ /// List all [Index]es and returns values as instances of [Index].
85
+ ///
86
+ /// # Example
87
+ ///
88
+ /// ```
89
+ /// # use meilisearch_sdk::{client::*, indexes::*};
90
+ /// #
91
+ /// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
92
+ /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
93
+ /// #
94
+ /// # futures::executor::block_on(async move {
95
+ /// // create the client
96
+ /// let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
97
+ /// let mut query = IndexesQuery::new(&client);
98
+ /// query.with_limit(1);
99
+ /// let indexes: IndexesResults = client.list_all_indexes_with(&query).await.unwrap();
100
+ ///
101
+ /// assert_eq!(indexes.limit, 1);
102
+ /// # });
103
+ /// ```
104
+ pub async fn list_all_indexes_with (
105
+ & self ,
106
+ indexes_query : & IndexesQuery < ' _ > ,
107
+ ) -> Result < IndexesResults , Error > {
108
+ let value = self . list_all_indexes_raw_with ( indexes_query) . await ?;
109
+ let indexes_results = self . parse_indexes_results_from_value ( value) ?;
110
+
111
+ Ok ( indexes_results)
68
112
}
69
113
70
114
/// List all [Index]es and returns as Json.
@@ -85,8 +129,8 @@ impl Client {
85
129
/// println!("{:?}", json_indexes);
86
130
/// # });
87
131
/// ```
88
- pub async fn list_all_indexes_raw ( & self ) -> Result < Vec < Value > , Error > {
89
- let json_indexes = request :: < ( ) , Vec < Value > > (
132
+ pub async fn list_all_indexes_raw ( & self ) -> Result < Value , Error > {
133
+ let json_indexes = request :: < ( ) , Value > (
90
134
& format ! ( "{}/indexes" , self . host) ,
91
135
& self . api_key ,
92
136
Method :: Get ( ( ) ) ,
@@ -97,6 +141,42 @@ impl Client {
97
141
Ok ( json_indexes)
98
142
}
99
143
144
+ /// List all [Index]es with query parameters and returns as Json.
145
+ ///
146
+ /// # Example
147
+ ///
148
+ /// ```
149
+ /// # use meilisearch_sdk::{client::*, indexes::*};
150
+ /// #
151
+ /// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
152
+ /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
153
+ /// #
154
+ /// # futures::executor::block_on(async move {
155
+ /// // create the client
156
+ /// let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
157
+ ///
158
+ /// let mut query = IndexesQuery::new(&client);
159
+ /// query.with_limit(1);
160
+ /// let json_indexes = client.list_all_indexes_raw_with(&query).await.unwrap();
161
+ ///
162
+ /// println!("{:?}", json_indexes);
163
+ /// # });
164
+ /// ```
165
+ pub async fn list_all_indexes_raw_with (
166
+ & self ,
167
+ indexes_query : & IndexesQuery < ' _ > ,
168
+ ) -> Result < Value , Error > {
169
+ let json_indexes = request :: < & IndexesQuery , Value > (
170
+ & format ! ( "{}/indexes" , self . host) ,
171
+ & self . api_key ,
172
+ Method :: Get ( indexes_query) ,
173
+ 200 ,
174
+ )
175
+ . await ?;
176
+
177
+ Ok ( json_indexes)
178
+ }
179
+
100
180
/// Get an [Index], this index should already exist.
101
181
///
102
182
/// # Example
@@ -158,13 +238,7 @@ impl Client {
158
238
159
239
/// Create a corresponding object of an [Index] without any check or doing an HTTP call.
160
240
pub fn index ( & self , uid : impl Into < String > ) -> Index {
161
- Index {
162
- uid : Arc :: new ( uid. into ( ) ) ,
163
- client : self . clone ( ) ,
164
- primary_key : None ,
165
- created_at : None ,
166
- updated_at : None ,
167
- }
241
+ Index :: new ( uid, self . clone ( ) )
168
242
}
169
243
170
244
/// Create an [Index].
@@ -225,15 +299,31 @@ impl Client {
225
299
}
226
300
227
301
/// Alias for [Client::list_all_indexes].
228
- pub async fn get_indexes ( & self ) -> Result < Vec < Index > , Error > {
302
+ pub async fn get_indexes ( & self ) -> Result < IndexesResults , Error > {
229
303
self . list_all_indexes ( ) . await
230
304
}
231
305
306
+ /// Alias for [Client::list_all_indexes_with].
307
+ pub async fn get_indexes_with (
308
+ & self ,
309
+ indexes_query : & IndexesQuery < ' _ > ,
310
+ ) -> Result < IndexesResults , Error > {
311
+ self . list_all_indexes_with ( indexes_query) . await
312
+ }
313
+
232
314
/// Alias for [Client::list_all_indexes_raw].
233
- pub async fn get_indexes_raw ( & self ) -> Result < Vec < Value > , Error > {
315
+ pub async fn get_indexes_raw ( & self ) -> Result < Value , Error > {
234
316
self . list_all_indexes_raw ( ) . await
235
317
}
236
318
319
+ /// Alias for [Client::list_all_indexes_raw_with].
320
+ pub async fn get_indexes_raw_with (
321
+ & self ,
322
+ indexes_query : & IndexesQuery < ' _ > ,
323
+ ) -> Result < Value , Error > {
324
+ self . list_all_indexes_raw_with ( indexes_query) . await
325
+ }
326
+
237
327
/// Get stats of all indexes.
238
328
///
239
329
/// # Example
@@ -873,6 +963,7 @@ mod tests {
873
963
}
874
964
875
965
#[ meilisearch_test]
966
+
876
967
async fn test_error_delete_key ( mut client : Client , name : String ) {
877
968
// ==> accessing a key that does not exist
878
969
let error = client. delete_key ( "invalid_key" ) . await . unwrap_err ( ) ;
@@ -887,9 +978,9 @@ mod tests {
887
978
888
979
// ==> executing the action without enough right
889
980
let mut key = KeyBuilder :: new ( ) ;
981
+
890
982
key. with_name ( & name) ;
891
983
let key = client. create_key ( key) . await . unwrap ( ) ;
892
-
893
984
let master_key = client. api_key . clone ( ) ;
894
985
// this key has no right
895
986
client. api_key = Arc :: new ( key. key . clone ( ) ) ;
@@ -1058,19 +1149,39 @@ mod tests {
1058
1149
}
1059
1150
1060
1151
#[ meilisearch_test]
1061
- async fn test_list_all_indexes ( client : Client , index : Index ) {
1152
+ async fn test_list_all_indexes ( client : Client ) {
1062
1153
let all_indexes = client. list_all_indexes ( ) . await . unwrap ( ) ;
1063
- assert ! ( all_indexes. len( ) > 0 ) ;
1064
- assert ! ( all_indexes. iter( ) . any( |idx| idx. uid == index. uid) ) ;
1154
+
1155
+ assert_eq ! ( all_indexes. limit, 20 ) ;
1156
+ assert_eq ! ( all_indexes. offset, 0 ) ;
1065
1157
}
1066
1158
1067
1159
#[ meilisearch_test]
1068
- async fn test_list_all_indexes_raw ( client : Client , index : Index ) {
1160
+ async fn test_list_all_indexes_with_params ( client : Client ) {
1161
+ let mut query = IndexesQuery :: new ( & client) ;
1162
+ query. with_limit ( 1 ) ;
1163
+ let all_indexes = client. list_all_indexes_with ( & query) . await . unwrap ( ) ;
1164
+
1165
+ assert_eq ! ( all_indexes. limit, 1 ) ;
1166
+ assert_eq ! ( all_indexes. offset, 0 ) ;
1167
+ }
1168
+
1169
+ #[ meilisearch_test]
1170
+ async fn test_list_all_indexes_raw ( client : Client ) {
1069
1171
let all_indexes_raw = client. list_all_indexes_raw ( ) . await . unwrap ( ) ;
1070
- assert ! ( all_indexes_raw. len( ) > 0 ) ;
1071
- assert ! ( all_indexes_raw
1072
- . iter( )
1073
- . any( |idx| idx[ "uid" ] == json!( index. uid. to_string( ) ) ) ) ;
1172
+
1173
+ assert_eq ! ( all_indexes_raw[ "limit" ] , json!( 20 ) ) ;
1174
+ assert_eq ! ( all_indexes_raw[ "offset" ] , json!( 0 ) ) ;
1175
+ }
1176
+
1177
+ #[ meilisearch_test]
1178
+ async fn test_list_all_indexes_raw_with_params ( client : Client ) {
1179
+ let mut query = IndexesQuery :: new ( & client) ;
1180
+ query. with_limit ( 1 ) ;
1181
+ let all_indexes_raw = client. list_all_indexes_raw_with ( & query) . await . unwrap ( ) ;
1182
+
1183
+ assert_eq ! ( all_indexes_raw[ "limit" ] , json!( 1 ) ) ;
1184
+ assert_eq ! ( all_indexes_raw[ "offset" ] , json!( 0 ) ) ;
1074
1185
}
1075
1186
1076
1187
#[ meilisearch_test]
0 commit comments