Skip to content

Commit 055e6d8

Browse files
committed
Implement cancel_tasks
1 parent 6ded71d commit 055e6d8

File tree

3 files changed

+104
-18
lines changed

3 files changed

+104
-18
lines changed

src/client.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
key::{Key, KeyBuilder, KeyUpdater, KeysQuery, KeysResults},
55
request::*,
66
task_info::TaskInfo,
7-
tasks::{Task, TasksResults, TasksSearchQuery},
7+
tasks::{Task, TasksCancelQuery, TasksResults, TasksSearchQuery},
88
utils::async_sleep,
99
};
1010
use serde::Deserialize;
@@ -763,6 +763,43 @@ impl Client {
763763
Ok(tasks)
764764
}
765765

766+
/// Cancel tasks with filters [TasksCancelQuery]
767+
///
768+
/// # Example
769+
///
770+
/// ```
771+
/// # use meilisearch_sdk::*;
772+
/// #
773+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
774+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
775+
/// #
776+
/// # futures::executor::block_on(async move {
777+
/// # let client = client::Client::new(MEILISEARCH_URL, MEILISEARCH_API_KEY);
778+
///
779+
/// let mut query = tasks::TasksCancelQuery::new(&client);
780+
/// query.with_index_uids(["get_tasks_with"]);
781+
///
782+
/// let res = client.cancel_tasks_with(&query).await.unwrap();
783+
/// # });
784+
/// ```
785+
pub async fn cancel_tasks_with(
786+
&self,
787+
filters: &TasksCancelQuery<'_>,
788+
) -> Result<TasksResults, Error> {
789+
let tasks = request::<&TasksCancelQuery, (), TasksResults>(
790+
&format!("{}/tasks/cancel", self.host),
791+
&self.api_key,
792+
Method::Post {
793+
query: filters,
794+
body: (),
795+
},
796+
200,
797+
)
798+
.await?;
799+
800+
Ok(tasks)
801+
}
802+
766803
/// Get all tasks from the server.
767804
///
768805
/// # Example

src/indexes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ impl Index {
933933
/// ```
934934
pub async fn get_tasks_with(
935935
&self,
936-
tasks_query: &TasksQuery<'_, TasksPagination>,
936+
tasks_query: &TasksQuery<'_, TasksPaginationFilters>,
937937
) -> Result<TasksResults, Error> {
938938
let mut query = tasks_query.clone();
939939
query.with_index_uids([self.uid.as_str()]);

src/tasks.rs

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ impl AsRef<u32> for Task {
400400
}
401401

402402
#[derive(Debug, Serialize, Clone)]
403-
pub struct TasksPagination {
403+
pub struct TasksPaginationFilters {
404404
// Maximum number of tasks to return
405405
#[serde(skip_serializing_if = "Option::is_none")]
406406
limit: Option<u32>,
@@ -410,10 +410,10 @@ pub struct TasksPagination {
410410
}
411411

412412
#[derive(Debug, Serialize, Clone)]
413-
pub struct TasksDefault {}
413+
pub struct TasksCancelFilters {}
414414

415-
pub type TasksSearchQuery<'a> = TasksQuery<'a, TasksPagination>;
416-
pub type TasksCancelQuery<'a> = TasksQuery<'a, TasksDefault>;
415+
pub type TasksSearchQuery<'a> = TasksQuery<'a, TasksPaginationFilters>;
416+
pub type TasksCancelQuery<'a> = TasksQuery<'a, TasksCancelFilters>;
417417

418418
#[derive(Debug, Serialize, Clone)]
419419
#[serde(rename_all = "camelCase")]
@@ -547,8 +547,8 @@ impl<'a, T> TasksQuery<'a, T> {
547547
}
548548
}
549549

550-
impl<'a> TasksQuery<'a, TasksDefault> {
551-
pub fn new(client: &'a Client) -> TasksQuery<'a, TasksDefault> {
550+
impl<'a> TasksQuery<'a, TasksCancelFilters> {
551+
pub fn new(client: &'a Client) -> TasksQuery<'a, TasksCancelFilters> {
552552
TasksQuery {
553553
client,
554554
index_uids: None,
@@ -561,17 +561,17 @@ impl<'a> TasksQuery<'a, TasksDefault> {
561561
after_started_at: None,
562562
before_finished_at: None,
563563
after_finished_at: None,
564-
pagination: TasksDefault {},
564+
pagination: TasksCancelFilters {},
565565
}
566566
}
567567

568-
// pub async fn execute(&'a self) -> Result<TasksResults, Error> {
569-
// self.client.get_tasks_with(self).await
570-
// }
568+
pub async fn execute(&'a self) -> Result<TasksResults, Error> {
569+
self.client.cancel_tasks_with(self).await
570+
}
571571
}
572572

573-
impl<'a> TasksQuery<'a, TasksPagination> {
574-
pub fn new(client: &'a Client) -> TasksQuery<'a, TasksPagination> {
573+
impl<'a> TasksQuery<'a, TasksPaginationFilters> {
574+
pub fn new(client: &'a Client) -> TasksQuery<'a, TasksPaginationFilters> {
575575
TasksQuery {
576576
client,
577577
index_uids: None,
@@ -584,17 +584,23 @@ impl<'a> TasksQuery<'a, TasksPagination> {
584584
after_started_at: None,
585585
before_finished_at: None,
586586
after_finished_at: None,
587-
pagination: TasksPagination {
587+
pagination: TasksPaginationFilters {
588588
limit: None,
589589
from: None,
590590
},
591591
}
592592
}
593-
pub fn with_limit<'b>(&'b mut self, limit: u32) -> &'b mut TasksQuery<'a, TasksPagination> {
593+
pub fn with_limit<'b>(
594+
&'b mut self,
595+
limit: u32,
596+
) -> &'b mut TasksQuery<'a, TasksPaginationFilters> {
594597
self.pagination.limit = Some(limit);
595598
self
596599
}
597-
pub fn with_from<'b>(&'b mut self, from: u32) -> &'b mut TasksQuery<'a, TasksPagination> {
600+
pub fn with_from<'b>(
601+
&'b mut self,
602+
from: u32,
603+
) -> &'b mut TasksQuery<'a, TasksPaginationFilters> {
598604
self.pagination.from = Some(from);
599605
self
600606
}
@@ -873,7 +879,6 @@ mod test {
873879
.execute()
874880
.await;
875881

876-
// let _ = client.get_tasks(&query).await;
877882
mock_res.assert();
878883
Ok(())
879884
}
@@ -912,4 +917,48 @@ mod test {
912917
assert_eq!(error.error_type, ErrorType::InvalidRequest);
913918
Ok(())
914919
}
920+
921+
#[meilisearch_test]
922+
async fn test_cancel_tasks_with_params() -> Result<(), Error> {
923+
let mock_server_url = &mockito::server_url();
924+
let client = Client::new(mock_server_url, "masterKey");
925+
let path =
926+
"/tasks/cancel?indexUids=movies,test&statuses=equeued&types=documentDeletion&uids=1";
927+
928+
let mock_res = mock("GET", path).with_status(200).create();
929+
930+
let mut query = TasksCancelQuery::new(&client);
931+
query
932+
.with_index_uids(["movies", "test"])
933+
.with_statuses(["equeued"])
934+
.with_types(["documentDeletion"])
935+
.with_uids([&1]);
936+
937+
let _ = client.cancel_tasks_with(&query).await;
938+
939+
mock_res.assert();
940+
Ok(())
941+
}
942+
943+
#[meilisearch_test]
944+
async fn test_cancel_tasks_with_params_execute() -> Result<(), Error> {
945+
let mock_server_url = &mockito::server_url();
946+
let client = Client::new(mock_server_url, "masterKey");
947+
let path =
948+
"/tasks/cancel?indexUids=movies,test&statuses=equeued&types=documentDeletion&uids=1";
949+
950+
let mock_res = mock("GET", path).with_status(200).create();
951+
952+
let mut query = TasksCancelQuery::new(&client);
953+
let _ = query
954+
.with_index_uids(["movies", "test"])
955+
.with_statuses(["equeued"])
956+
.with_types(["documentDeletion"])
957+
.with_uids([&1])
958+
.execute()
959+
.await;
960+
961+
mock_res.assert();
962+
Ok(())
963+
}
915964
}

0 commit comments

Comments
 (0)