Skip to content

Commit 5ce98c7

Browse files
bidoubiwairevoire
andauthored
Update indexes for v0.28.0 (#315)
* Update task api v0.28.0 * Update tests to be relevant with the structures where they are ran * Update types in code-samples * Remove clearAll task type * Add index_uid as an optional in the Tasks * Add comments on best builder implementation * Add execute function on get_tasks * Update code samples * Remove out of context comment * Fix tests * Fix pagination tests * Update HTTP methods for v0.28.0 * Fix clippy * Remove comment of task tests since the tests are now sucesful * Fix doc tests * Update keys for v0.28.0 * Fix get tasks inside index structure * Make description and name optional in keys * Fix none doc tests with new get_tasks api * Add from and limit in tasks params * Add warning on failing test * Update keys design * Update task API * Remove useless comment * Remove client as mandatory parameter for the keyUpdater * Add doc and tests on doc * Fix docs tests on keys in client * Fix docs tests * Remove dbg * Add with_uid filter * Add new actions on key creation * Remove new line at the start of docs * Fix clippy errors * Rename type in Task structure * Removed useless newlines * Fix typo in comment * Add missing semi-column * Update indexes api for v0.28.0 * Improve doc comments * Change indexes methods * Add index query and index updater * Add documentation and doc tests on indexes routes * Add tests on get indexes with params * Add test on index update * Fix clippy * Improve limit to avoid flacky test * Update src/client.rs Co-authored-by: Tamo <[email protected]> * Update src/client.rs Co-authored-by: Tamo <[email protected]> * Remove useless newlines * Rollback changes on keys api * Fix duplicated closing bracket * Fix failing tests * Remove useless comment Co-authored-by: Tamo <[email protected]>
1 parent 24e6b17 commit 5ce98c7

File tree

3 files changed

+550
-58
lines changed

3 files changed

+550
-58
lines changed

.code-samples.meilisearch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
get_one_index_1: |-
77
let movies: Index = client.get_index("movies").await.unwrap();
88
list_all_indexes_1: |-
9-
let indexes: Vec<Index> = client.list_all_indexes().await.unwrap();
9+
let indexes: IndexesResults = client.list_all_indexes().await.unwrap();
1010
create_an_index_1: |-
1111
client.create_index("movies", Some("id")).await.unwrap();
1212
update_an_index_1: |-

src/client.rs

Lines changed: 139 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,23 @@ impl Client {
4141
}
4242
}
4343

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].
4561
///
4662
/// # Example
4763
///
@@ -55,16 +71,44 @@ impl Client {
5571
/// // create the client
5672
/// let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
5773
///
58-
/// let indexes: Vec<Index> = client.list_all_indexes().await.unwrap();
74+
/// let indexes: IndexesResults = client.list_all_indexes().await.unwrap();
5975
/// println!("{:?}", indexes);
6076
/// # });
6177
/// ```
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)
68112
}
69113

70114
/// List all [Index]es and returns as Json.
@@ -85,8 +129,8 @@ impl Client {
85129
/// println!("{:?}", json_indexes);
86130
/// # });
87131
/// ```
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>(
90134
&format!("{}/indexes", self.host),
91135
&self.api_key,
92136
Method::Get(()),
@@ -97,6 +141,42 @@ impl Client {
97141
Ok(json_indexes)
98142
}
99143

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+
100180
/// Get an [Index], this index should already exist.
101181
///
102182
/// # Example
@@ -158,13 +238,7 @@ impl Client {
158238

159239
/// Create a corresponding object of an [Index] without any check or doing an HTTP call.
160240
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())
168242
}
169243

170244
/// Create an [Index].
@@ -225,15 +299,31 @@ impl Client {
225299
}
226300

227301
/// 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> {
229303
self.list_all_indexes().await
230304
}
231305

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+
232314
/// 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> {
234316
self.list_all_indexes_raw().await
235317
}
236318

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+
237327
/// Get stats of all indexes.
238328
///
239329
/// # Example
@@ -873,6 +963,7 @@ mod tests {
873963
}
874964

875965
#[meilisearch_test]
966+
876967
async fn test_error_delete_key(mut client: Client, name: String) {
877968
// ==> accessing a key that does not exist
878969
let error = client.delete_key("invalid_key").await.unwrap_err();
@@ -887,9 +978,9 @@ mod tests {
887978

888979
// ==> executing the action without enough right
889980
let mut key = KeyBuilder::new();
981+
890982
key.with_name(&name);
891983
let key = client.create_key(key).await.unwrap();
892-
893984
let master_key = client.api_key.clone();
894985
// this key has no right
895986
client.api_key = Arc::new(key.key.clone());
@@ -1058,19 +1149,39 @@ mod tests {
10581149
}
10591150

10601151
#[meilisearch_test]
1061-
async fn test_list_all_indexes(client: Client, index: Index) {
1152+
async fn test_list_all_indexes(client: Client) {
10621153
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);
10651157
}
10661158

10671159
#[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) {
10691171
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));
10741185
}
10751186

10761187
#[meilisearch_test]

0 commit comments

Comments
 (0)