Skip to content

Commit

Permalink
Add execute function on get_tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
bidoubiwa committed Aug 2, 2022
1 parent 6a52ca4 commit fd3d171
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 17 deletions.
26 changes: 12 additions & 14 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,30 +575,28 @@ impl Client {
/// # futures::executor::block_on(async move {
/// # let client = client::Client::new("http://localhost:7700", "masterKey");
///
/// let tasks = TasksQueryBuilder::new().with_index_uid(["movies"]).execute(&client).execute();
///
/// let tasks = client
/// .get_tasks()
/// .with_index_uid(["movies"])
/// .execute()
/// .await.unwrap();
///
///
/// let tasks = client.get_task().await.unwrap();
/// dbg!(&tasks);
/// let tasks = client.get_tasks(&["get_tasks"]).with_index_uid().await.unwrap();
/// # });
/// ```
pub async fn get_tasks(&self, tasks_query: TasksQuery) -> Result<TasksResults, Error> {
let tasks = request::<(), TasksResults>(
pub fn get_tasks(&self) -> TasksQuery {
TasksQuery::new(self)
}

pub(crate) async fn execute_get_tasks(
&self,
tasks_query: &TasksQuery<'_>,
) -> Result<TasksResults, Error> {
let tasks = request::<&TasksQuery, TasksResults>(
&format!("{}/tasks", self.host),
&self.api_key,
Method::Get,
Method::Get(tasks_query),
200,
)
.await?;

dbg!(&tasks);

Ok(tasks)
}

Expand Down Expand Up @@ -731,7 +729,7 @@ mod tests {

#[meilisearch_test]
async fn test_get_tasks(client: Client) {
let tasks = client.get_tasks().await.unwrap();
let tasks = client.get_tasks().execute().await.unwrap();
assert!(tasks.results.len() >= 2);
}

Expand Down
67 changes: 64 additions & 3 deletions src/tasks.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use serde::{Deserialize, Deserializer, Serialize};
use serde::{de::DeserializeOwned, Deserialize, Deserializer, Serialize};
use std::time::Duration;
use time::OffsetDateTime;

Expand Down Expand Up @@ -376,6 +376,8 @@ impl AsRef<u32> for Task {
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct TasksQuery<'a> {
#[serde(skip_serializing)]
pub client: &'a Client,
// Index uids array to only retrieve the tasks of the indexes.
#[serde(skip_serializing_if = "Option::is_none")]
pub index_uid: Option<&'a [&'a str]>,
Expand All @@ -389,8 +391,9 @@ pub struct TasksQuery<'a> {

#[allow(missing_docs)]
impl<'a> TasksQuery<'a> {
pub fn new() -> TasksQuery<'a> {
pub fn new(client: &'a Client) -> TasksQuery<'a> {
TasksQuery {
client,
index_uid: None,
status: None,
r#type: None,
Expand All @@ -408,7 +411,9 @@ impl<'a> TasksQuery<'a> {
self.r#type = Some(r#type);
self
}
// execute
pub async fn execute(&'a self) -> Result<TasksResults, Error> {
self.client.execute_get_tasks(self).await
}
}

#[cfg(test)]
Expand All @@ -419,6 +424,7 @@ mod test {
errors::{ErrorCode, ErrorType},
};
use meilisearch_test_macro::meilisearch_test;
use mockito::mock;
use serde::{Deserialize, Serialize};
use std::time::Duration;

Expand Down Expand Up @@ -568,6 +574,61 @@ mod test {
Ok(())
}

#[meilisearch_test]
async fn test_get_tasks_no_params() -> Result<(), Error> {
let mock_server_url = &mockito::server_url();
let client = Client::new(mock_server_url, "masterKey");
let path = "/tasks";

let mock_res = mock("GET", path).with_status(200).create();
let _ = client.get_tasks().execute().await;
mock_res.assert();

// let _ = mockRes.req.await;
// mockRes.m.assert();
// assert_eq!(tasks,);
// assert!(matches!(tasks, TasksResults { results.. } ));
Ok(())
}

#[meilisearch_test]
async fn test_get_tasks_with_params() -> Result<(), Error> {
let mock_server_url = &mockito::server_url();
let client = Client::new(mock_server_url, "masterKey");
let path = "/tasks?indexUid=movies,test&status=equeued&type=documentDeletion";

let mock_res = mock("GET", path).with_status(200).create();
let _ = client
.get_tasks()
.with_index_uid(&["movies", "test"])
.with_status(&["equeued"])
.with_type(&["documentDeletion"])
.execute()
.await;
mock_res.assert();
Ok(())
}

#[meilisearch_test]
// TODO: Will un ignore when pagination is added in TaskResults
#[ignore]
async fn test_get_tasks_with_params_2(client: Client, index: Index) -> Result<(), Error> {
let tasks = client
.get_tasks()
.with_index_uid(&[index.uid.as_str()])
.execute()
.await
.unwrap();

// let _ = mockRes.req.await;
// mockRes.m.assert();
// assert_eq!(tasks,);
// assert!(matches!(tasks, TasksResults { results.. } ));
dbg!(&tasks);
assert_eq!(tasks.results.len(), 1);
Ok(())
}

#[meilisearch_test]
// TODO: failing because settings routes now uses PUT instead of POST as http method
async fn test_failing_task(client: Client, movies: Index) -> Result<(), Error> {
Expand Down

0 comments on commit fd3d171

Please sign in to comment.