Skip to content

Commit c143d9e

Browse files
bors[bot]vishnugt
andauthored
Merge #186
186: Added list_all_indexes_raw r=curquiza a=vishnugt Fixes #182 Co-authored-by: vishnugt <[email protected]>
2 parents c2405ac + 64fbe43 commit c143d9e

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

src/client.rs

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl Client {
2929
}
3030
}
3131

32-
/// List all [indexes](../indexes/struct.Index.html).
32+
/// List all [indexes] and returns values as instances of Index(../indexes/struct.Index.html).
3333
///
3434
/// # Example
3535
///
@@ -44,19 +44,41 @@ impl Client {
4444
/// # });
4545
/// ```
4646
pub async fn list_all_indexes(&self) -> Result<Vec<Index>, Error> {
47+
match self.list_all_indexes_raw().await{
48+
Ok (json_indexes) => Ok({
49+
let mut indexes = Vec::new();
50+
for json_index in json_indexes {
51+
indexes.push(json_index.into_index(self))
52+
}
53+
indexes
54+
}),
55+
Err(error) => Err(error),
56+
}
57+
}
58+
59+
/// List all [indexes] and returns as Json (../indexes/struct.Index.html).
60+
///
61+
/// # Example
62+
///
63+
/// ```
64+
/// # use meilisearch_sdk::{client::*, indexes::*};
65+
/// # futures::executor::block_on(async move {
66+
/// // create the client
67+
/// let client = Client::new("http://localhost:7700", "masterKey");
68+
///
69+
/// let json_indexes: Vec<JsonIndex> = client.list_all_indexes_raw().await.unwrap();
70+
/// println!("{:?}", json_indexes);
71+
/// # });
72+
/// ```
73+
pub async fn list_all_indexes_raw(&self) -> Result<Vec<JsonIndex>, Error> {
4774
let json_indexes = request::<(), Vec<JsonIndex>>(
4875
&format!("{}/indexes", self.host),
4976
&self.api_key,
5077
Method::Get,
5178
200,
5279
).await?;
5380

54-
let mut indexes = Vec::new();
55-
for json_index in json_indexes {
56-
indexes.push(json_index.into_index(self))
57-
}
58-
59-
Ok(indexes)
81+
Ok(json_indexes)
6082
}
6183

6284
/// Get an [index](../indexes/struct.Index.html).
@@ -169,6 +191,11 @@ impl Client {
169191
self.list_all_indexes().await
170192
}
171193

194+
/// Alias for [list_all_indexes_raw](#method.list_all_indexes_raw).
195+
pub async fn get_indexes_raw(&self) -> Result<Vec<JsonIndex>, Error> {
196+
self.list_all_indexes_raw().await
197+
}
198+
172199
/// Get stats of all indexes.
173200
///
174201
/// # Example
@@ -350,6 +377,32 @@ mod tests {
350377
assert!(index.is_err());
351378
}
352379

380+
#[async_test]
381+
async fn test_list_all_indexes() {
382+
let client = Client::new("http://localhost:7700", "masterKey");
383+
let index_name = "list_all_indexes";
384+
client.create_index(index_name, None).await.unwrap();
385+
let index = client.get_index(index_name).await;
386+
assert!(index.is_ok());
387+
let all_indexes = client.list_all_indexes().await.unwrap();
388+
assert!(all_indexes.len() > 0);
389+
let deleted = client.delete_index_if_exists(index_name).await.unwrap();
390+
assert_eq!(deleted, true);
391+
}
392+
393+
#[async_test]
394+
async fn test_list_all_indexes_raw() {
395+
let client = Client::new("http://localhost:7700", "masterKey");
396+
let index_name = "list_all_indexes_raw";
397+
client.create_index(index_name, None).await.unwrap();
398+
let index = client.get_index(index_name).await;
399+
assert!(index.is_ok());
400+
let all_indexes_raw = client.list_all_indexes_raw().await.unwrap();
401+
assert!(all_indexes_raw.len() > 0);
402+
let deleted = client.delete_index_if_exists(index_name).await.unwrap();
403+
assert_eq!(deleted, true);
404+
}
405+
353406
#[async_test]
354407
async fn test_delete_if_exits_none() {
355408
let client = Client::new("http://localhost:7700", "masterKey");

src/indexes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{fmt::Display, collections::HashMap};
77

88
#[derive(Deserialize, Debug)]
99
#[allow(non_snake_case)]
10-
pub(crate) struct JsonIndex {
10+
pub struct JsonIndex {
1111
uid: String,
1212
primaryKey: Option<String>,
1313
createdAt: String,

0 commit comments

Comments
 (0)