@@ -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 } ;
1111use serde_json:: { json, Value } ;
1212use std:: { collections:: HashMap , time:: Duration } ;
1313use 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+
2227impl 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 ) ;
0 commit comments