Skip to content

Commit 52ac7d8

Browse files
bidoubiwameili-botalallema
authored
Add cancel task for v0.30.0 (#377)
* Update README.md * Update README.tpl * Add filters in tasksQuery * Add new error codes * Add builder methods and tests * Fix clippy suggestions * Update src/tasks.rs Co-authored-by: Amélie <[email protected]> * Implement default taskQuery structure * Fix flacky tests * Fix get_tasks test * Refactor filtering of tasks * Rename index_uids and task types * Rename uid, status and types filters to plural * Add new error codes * Update error code to plural form * Update code samples * Update code-samples with new filter plural naming * Rename filters to plural form * Implement cancel_tasks * Add taskCancelation task details * Fix execution of get_tasks * Add canceled by as a return from task * Fix taskCancelation detail * Fix typing error * Chage type of canceled_by * Make variable plural * Rollback unrelated changes * Change variable to plural form * Add delete tasks for v0.30.0 (#381) * Add delete tasks for v0.30.0 * Add swap indexes for v0.30.0 (#382) * Add swap indexes for v0.30.0 * Implement swap indexes * Add indexSwap detail * Use tuples as type for the indexes field * Add tests on index swap * Create better documentation for indexes swap * Fix clippy errors * Fix missing bracket * Fix swap doc test * Make doc more clear Co-authored-by: meili-bot <[email protected]> Co-authored-by: Amélie <[email protected]>
1 parent 48cb827 commit 52ac7d8

File tree

4 files changed

+441
-71
lines changed

4 files changed

+441
-71
lines changed

.code-samples.meilisearch.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,27 +109,27 @@ get_all_tasks_1: |-
109109
.await
110110
.unwrap();
111111
get_all_tasks_filtering_1: |-
112-
let mut query = TasksQuery::new(&client)
112+
let mut query = TasksSearchQuery::new(&client)
113113
.with_index_uids(["movies"])
114114
.execute()
115115
.await
116116
.unwrap();
117117
get_all_tasks_filtering_2: |-
118-
let mut query = TasksQuery::new(&client)
118+
let mut query = TasksSearchQuery::new(&client)
119119
.with_statuses(["succeeded", "failed"])
120120
.with_types(["documentAdditionOrUpdate"])
121121
.execute()
122122
.await
123123
.unwrap();
124124
get_all_tasks_paginating_1: |-
125-
let mut query = TasksQuery::new(&client)
125+
let mut query = TasksSearchQuery::new(&client)
126126
.with_limit(2)
127127
.with_from(10)
128128
.execute()
129129
.await
130130
.unwrap();
131131
get_all_tasks_paginating_2: |-
132-
let mut query = TasksQuery::new(&client)
132+
let mut query = TasksSearchQuery::new(&client)
133133
.with_limit(2)
134134
.from(8)
135135
.execute()

src/client.rs

Lines changed: 186 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use crate::{
44
key::{Key, KeyBuilder, KeyUpdater, KeysQuery, KeysResults},
55
request::*,
66
task_info::TaskInfo,
7-
tasks::{Task, TasksQuery, TasksResults},
7+
tasks::{Task, TasksCancelQuery, TasksDeleteQuery, TasksResults, TasksSearchQuery},
88
utils::async_sleep,
99
};
10-
use serde::Deserialize;
10+
use serde::{Deserialize, Serialize};
1111
use serde_json::{json, Value};
1212
use std::{collections::HashMap, time::Duration};
1313
use time::OffsetDateTime;
@@ -19,6 +19,11 @@ pub struct Client {
1919
pub(crate) api_key: String,
2020
}
2121

22+
#[derive(Debug, Clone, Serialize, Deserialize)]
23+
pub struct SwapIndexes {
24+
pub indexes: (String, String),
25+
}
26+
2227
impl Client {
2328
/// Create a client using the specified server.
2429
/// Don't put a '/' at the end of the host.
@@ -329,6 +334,56 @@ impl Client {
329334
self.list_all_indexes_raw_with(indexes_query).await
330335
}
331336

337+
/// Swaps a list of two [Index]'es.
338+
///
339+
/// # Example
340+
///
341+
/// ```
342+
/// # use meilisearch_sdk::{client::*, indexes::*};
343+
/// #
344+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
345+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
346+
/// #
347+
/// # futures::executor::block_on(async move {
348+
/// // Create the client
349+
/// let client = Client::new(MEILISEARCH_URL, MEILISEARCH_API_KEY);
350+
///
351+
/// let task_index_1 = client.create_index("swap_index_1", None).await.unwrap();
352+
/// let task_index_2 = client.create_index("swap_index_2", None).await.unwrap();
353+
///
354+
/// // Wait for the task to complete
355+
/// task_index_2.wait_for_completion(&client, None, None).await.unwrap();
356+
///
357+
/// let task = client
358+
/// .swap_indexes([&SwapIndexes {
359+
/// indexes: (
360+
/// "swap_index_1".to_string(),
361+
/// "swap_index_2".to_string(),
362+
/// ),
363+
/// }])
364+
/// .await
365+
/// .unwrap();
366+
///
367+
/// # client.index("swap_index_1").delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
368+
/// # client.index("swap_index_2").delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
369+
/// # });
370+
/// ```
371+
pub async fn swap_indexes(
372+
&self,
373+
indexes: impl IntoIterator<Item = &SwapIndexes>,
374+
) -> Result<TaskInfo, Error> {
375+
request::<(), Vec<&SwapIndexes>, TaskInfo>(
376+
&format!("{}/swap-indexes", self.host),
377+
&self.api_key,
378+
Method::Post {
379+
query: (),
380+
body: indexes.into_iter().collect(),
381+
},
382+
202,
383+
)
384+
.await
385+
}
386+
332387
/// Get stats of all indexes.
333388
///
334389
/// # Example
@@ -754,16 +809,16 @@ impl Client {
754809
/// # futures::executor::block_on(async move {
755810
/// # let client = client::Client::new(MEILISEARCH_URL, MEILISEARCH_API_KEY);
756811
///
757-
/// let mut query = tasks::TasksQuery::new(&client);
812+
/// let mut query = tasks::TasksSearchQuery::new(&client);
758813
/// query.with_index_uids(["get_tasks_with"]);
759814
/// let tasks = client.get_tasks_with(&query).await.unwrap();
760815
/// # });
761816
/// ```
762817
pub async fn get_tasks_with(
763818
&self,
764-
tasks_query: &TasksQuery<'_>,
819+
tasks_query: &TasksSearchQuery<'_>,
765820
) -> Result<TasksResults, Error> {
766-
let tasks = request::<&TasksQuery, (), TasksResults>(
821+
let tasks = request::<&TasksSearchQuery, (), TasksResults>(
767822
&format!("{}/tasks", self.host),
768823
&self.api_key,
769824
Method::Get { query: tasks_query },
@@ -774,6 +829,77 @@ impl Client {
774829
Ok(tasks)
775830
}
776831

832+
/// Cancel tasks with filters [TasksCancelQuery]
833+
///
834+
/// # Example
835+
///
836+
/// ```
837+
/// # use meilisearch_sdk::*;
838+
/// #
839+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
840+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
841+
/// #
842+
/// # futures::executor::block_on(async move {
843+
/// # let client = client::Client::new(MEILISEARCH_URL, MEILISEARCH_API_KEY);
844+
///
845+
/// let mut query = tasks::TasksCancelQuery::new(&client);
846+
/// query.with_index_uids(["movies"]);
847+
///
848+
/// let res = client.cancel_tasks_with(&query).await.unwrap();
849+
/// # });
850+
/// ```
851+
pub async fn cancel_tasks_with(
852+
&self,
853+
filters: &TasksCancelQuery<'_>,
854+
) -> Result<TaskInfo, Error> {
855+
let tasks = request::<&TasksCancelQuery, (), TaskInfo>(
856+
&format!("{}/tasks/cancel", self.host),
857+
&self.api_key,
858+
Method::Post {
859+
query: filters,
860+
body: (),
861+
},
862+
200,
863+
)
864+
.await?;
865+
866+
Ok(tasks)
867+
}
868+
869+
/// Delete tasks with filters [TasksDeleteQuery]
870+
///
871+
/// # Example
872+
///
873+
/// ```
874+
/// # use meilisearch_sdk::*;
875+
/// #
876+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
877+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
878+
/// #
879+
/// # futures::executor::block_on(async move {
880+
/// # let client = client::Client::new(MEILISEARCH_URL, MEILISEARCH_API_KEY);
881+
///
882+
/// let mut query = tasks::TasksDeleteQuery::new(&client);
883+
/// query.with_index_uids(["movies"]);
884+
///
885+
/// let res = client.delete_tasks_with(&query).await.unwrap();
886+
/// # });
887+
/// ```
888+
pub async fn delete_tasks_with(
889+
&self,
890+
filters: &TasksDeleteQuery<'_>,
891+
) -> Result<TaskInfo, Error> {
892+
let tasks = request::<&TasksDeleteQuery, (), TaskInfo>(
893+
&format!("{}/tasks", self.host),
894+
&self.api_key,
895+
Method::Delete { query: filters },
896+
200,
897+
)
898+
.await?;
899+
900+
Ok(tasks)
901+
}
902+
777903
/// Get all tasks from the server.
778904
///
779905
/// # Example
@@ -789,7 +915,6 @@ impl Client {
789915
/// let tasks = client.get_tasks().await.unwrap();
790916
///
791917
/// # assert!(tasks.results.len() > 0);
792-
/// # client.index("get_tasks").delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
793918
/// # });
794919
/// ```
795920
pub async fn get_tasks(&self) -> Result<TasksResults, Error> {
@@ -884,12 +1009,66 @@ mod tests {
8841009
use crate::{
8851010
client::*,
8861011
key::{Action, KeyBuilder},
1012+
tasks::TasksSearchQuery,
8871013
};
8881014
use meilisearch_test_macro::meilisearch_test;
8891015
use mockito::mock;
8901016
use std::mem;
8911017
use time::OffsetDateTime;
8921018

1019+
#[derive(Debug, Serialize, Deserialize, PartialEq)]
1020+
struct Document {
1021+
id: String,
1022+
}
1023+
1024+
#[meilisearch_test]
1025+
async fn test_swapping_two_indexes(client: Client) {
1026+
let index_1 = client.index("test_swapping_two_indexes_1");
1027+
let index_2 = client.index("test_swapping_two_indexes_2");
1028+
1029+
let t0 = index_1
1030+
.add_documents(
1031+
&[Document {
1032+
id: "1".to_string(),
1033+
}],
1034+
None,
1035+
)
1036+
.await
1037+
.unwrap();
1038+
1039+
index_2
1040+
.add_documents(
1041+
&[Document {
1042+
id: "2".to_string(),
1043+
}],
1044+
None,
1045+
)
1046+
.await
1047+
.unwrap();
1048+
1049+
t0.wait_for_completion(&client, None, None).await.unwrap();
1050+
1051+
let task = client
1052+
.swap_indexes([&SwapIndexes {
1053+
indexes: (
1054+
"test_swapping_two_indexes_1".to_string(),
1055+
"test_swapping_two_indexes_2".to_string(),
1056+
),
1057+
}])
1058+
.await
1059+
.unwrap();
1060+
task.wait_for_completion(&client, None, None).await.unwrap();
1061+
1062+
let document = index_1.get_document("2").await.unwrap();
1063+
1064+
assert_eq!(
1065+
Document {
1066+
id: "2".to_string()
1067+
},
1068+
document
1069+
);
1070+
}
1071+
8931072
#[meilisearch_test]
8941073
async fn test_methods_has_qualified_version_as_header() {
8951074
let mock_server_url = &mockito::server_url();
@@ -970,7 +1149,7 @@ mod tests {
9701149

9711150
#[meilisearch_test]
9721151
async fn test_get_tasks_with_params(client: Client) {
973-
let query = TasksQuery::new(&client);
1152+
let query = TasksSearchQuery::new(&client);
9741153
let tasks = client.get_tasks_with(&query).await.unwrap();
9751154

9761155
assert!(tasks.limit == 20);

src/indexes.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ impl Index {
939939
/// # });
940940
/// ```
941941
pub async fn get_tasks(&self) -> Result<TasksResults, Error> {
942-
let mut query = TasksQuery::new(&self.client);
942+
let mut query = TasksSearchQuery::new(&self.client);
943943
query.with_index_uids([self.uid.as_str()]);
944944

945945
self.client.get_tasks_with(&query).await
@@ -960,7 +960,7 @@ impl Index {
960960
/// # let client = Client::new(MEILISEARCH_URL, MEILISEARCH_API_KEY);
961961
/// # let index = client.create_index("get_tasks_with", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap().try_make_index(&client).unwrap();
962962
///
963-
/// let mut query = TasksQuery::new(&client);
963+
/// let mut query = TasksSearchQuery::new(&client);
964964
/// query.with_index_uids(["none_existant"]);
965965
/// let tasks = index.get_tasks_with(&query).await.unwrap();
966966
///
@@ -970,7 +970,7 @@ impl Index {
970970
/// ```
971971
pub async fn get_tasks_with(
972972
&self,
973-
tasks_query: &TasksQuery<'_>,
973+
tasks_query: &TasksQuery<'_, TasksPaginationFilters>,
974974
) -> Result<TasksResults, Error> {
975975
let mut query = tasks_query.clone();
976976
query.with_index_uids([self.uid.as_str()]);

0 commit comments

Comments
 (0)