Skip to content

Commit 6fa9fe4

Browse files
bors[bot]icyJoseph
andauthored
Merge #133
133: Create get_update fn to get one update status r=curquiza a=icyJoseph Hi, Having fun using this SDK, but I reckon a function to get the status of one single update is missing. When I read through https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status, I see for example: ```javascript client.index('movies').getUpdateStatus(1) ``` Where the argument of `getUpdateStatus`, I guess, is the update id based on what the cURL version does: ```bash curl \ -X GET 'http://localhost:7700/indexes/movies/updates/1' ``` Which mirrors the MeiliSearch `/indexes/:index_uid/updates/:updateId` endpoint. The rather unlucky thing is that to do the same with the Rust SDK it is not sufficient, not possible, to use the update id. ```rust let status: Status = progress.get_status().await.unwrap(); ``` The above requires access to the `progress` instance, which is not possible in my case. Regardless of any specific use case, _**it is my opinion**_ that for completeness sake, the `get_update` function, defined in this PR, is needed to complete the SDK. Of course this can be challenged, or perhaps there's a reason to why such function does not exist. Thanks for the SDK! Great Job! Co-authored-by: Joseph Chamochumbi <[email protected]>
2 parents 355d3e3 + c21a50c commit 6fa9fe4

File tree

3 files changed

+124
-1
lines changed

3 files changed

+124
-1
lines changed

.code-samples.meilisearch.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ search_1: |-
6060
.await
6161
.unwrap();
6262
get_update_1: |-
63+
// You can get the status of a `Progress` object:
6364
let status: Status = progress.get_status().await.unwrap();
65+
66+
// Or you can use index to get an update status using its `update_id`:
67+
let status: Status = index.get_update(1).await.unwrap();
6468
get_all_updates_1: |-
6569
let status: Vec<ProgressStatus> = index.get_all_updates().await.unwrap();
6670
get_keys_1: |-

src/indexes.rs

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,88 @@ impl<'a> Index<'a> {
619619
self.update(primary_key).await
620620
}
621621

622+
/// Get the status of an update on the index.
623+
///
624+
/// After executing an update, a `Progress` struct is returned,
625+
/// you can use this struct to check on the status of the update.
626+
///
627+
/// In some cases, you might not need the status of the update directly,
628+
/// or would rather not wait for it to resolve.
629+
///
630+
/// For these cases, you can get the `update_id` from the `Progress`
631+
/// struct and use it to query the index later on.
632+
///
633+
/// For example, if a clients updates an entry over an HTTP request,
634+
/// you can respond with the `update_id` and have the client check
635+
/// on the update status later on.
636+
///
637+
/// # Example
638+
///
639+
/// ```
640+
/// # use serde::{Serialize, Deserialize};
641+
/// # use std::thread::sleep;
642+
/// # use std::time::Duration;
643+
/// # use meilisearch_sdk::{client::*, document, indexes::*, progress::UpdateStatus};
644+
/// #
645+
/// # #[derive(Debug, Serialize, Deserialize, PartialEq)]
646+
/// # struct Document {
647+
/// # id: usize,
648+
/// # value: String,
649+
/// # kind: String,
650+
/// # }
651+
/// #
652+
/// # impl document::Document for Document {
653+
/// # type UIDType = usize;
654+
/// #
655+
/// # fn get_uid(&self) -> &Self::UIDType {
656+
/// # &self.id
657+
/// # }
658+
/// # }
659+
/// #
660+
/// # futures::executor::block_on(async move {
661+
/// let client = Client::new("http://localhost:7700", "masterKey");
662+
/// let movies = client.get_or_create("movies_get_one_update").await.unwrap();
663+
///
664+
/// let progress = movies.add_documents(&[
665+
/// Document { id: 0, kind: "title".into(), value: "The Social Network".to_string() }
666+
/// ], None).await.unwrap();
667+
///
668+
/// // Get update status directly on the progress object
669+
/// let status = progress.get_status().await.unwrap();
670+
/// let from_progress = match status {
671+
/// UpdateStatus::Enqueued{content} => content.update_id,
672+
/// UpdateStatus::Failed{content} => content.update_id,
673+
/// UpdateStatus::Processed{content} => content.update_id,
674+
/// };
675+
///
676+
/// let update_id = progress.get_update_id();
677+
/// // Get update status from the index, using `update_id`
678+
/// let status = movies.get_update(update_id).await.unwrap();
679+
///
680+
/// let from_index = match status {
681+
/// UpdateStatus::Enqueued{content} => content.update_id,
682+
/// UpdateStatus::Failed{content} => content.update_id,
683+
/// UpdateStatus::Processed{content} => content.update_id,
684+
/// };
685+
///
686+
/// assert_eq!(from_progress, from_index);
687+
/// assert_eq!(from_progress, update_id);
688+
/// # client.delete_index("movies_get_one_update").await.unwrap();
689+
/// # });
690+
/// ```
691+
pub async fn get_update(&self, update_id: u64) -> Result<UpdateStatus, Error> {
692+
request::<(), UpdateStatus>(
693+
&format!(
694+
"{}/indexes/{}/updates/{}",
695+
self.client.host, self.uid, update_id
696+
),
697+
self.client.apikey,
698+
Method::Get,
699+
200,
700+
)
701+
.await
702+
}
703+
622704
/// Get the status of all updates in a given index.
623705
///
624706
/// # Example
@@ -712,7 +794,7 @@ pub struct IndexStats {
712794

713795
#[cfg(test)]
714796
mod tests {
715-
use crate::{client::*};
797+
use crate::{client::*, progress::UpdateStatus};
716798
use futures_await_test::async_test;
717799

718800
#[async_test]
@@ -726,4 +808,24 @@ mod tests {
726808

727809
assert_eq!(status.len(), 0);
728810
}
811+
812+
#[async_test]
813+
async fn test_get_one_update() {
814+
let client = Client::new("http://localhost:7700", "masterKey");
815+
let uid = "test_get_one_update";
816+
817+
let index = client.get_or_create(uid).await.unwrap();
818+
let progress = index.delete_all_documents().await.unwrap();
819+
820+
let update_id = progress.get_update_id();
821+
let status = index.get_update(update_id).await.unwrap();
822+
823+
client.delete_index(uid).await.unwrap();
824+
825+
match status {
826+
UpdateStatus::Enqueued{content} => assert_eq!(content.update_id, update_id),
827+
UpdateStatus::Failed{content} => assert_eq!(content.update_id, update_id),
828+
UpdateStatus::Processed{content} => assert_eq!(content.update_id, update_id),
829+
}
830+
}
729831
}

src/progress.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@ pub struct Progress<'a> {
2727
}
2828

2929
impl<'a> Progress<'a> {
30+
31+
/// # Example
32+
///
33+
/// ```
34+
/// # use meilisearch_sdk::{client::*, indexes::*, document::*};
35+
/// # futures::executor::block_on(async move {
36+
/// let client = Client::new("http://localhost:7700", "masterKey");
37+
/// let mut movies_index = client.get_or_create("movies").await.unwrap();
38+
/// let progress = movies_index.delete_all_documents().await.unwrap();
39+
/// let update_id = progress.get_update_id();
40+
/// # client.delete_index("movies").await.unwrap();
41+
/// # });
42+
/// ```
43+
pub fn get_update_id(&self) -> u64 {
44+
self.id as u64
45+
}
46+
3047
/// # Example
3148
///
3249
/// ```

0 commit comments

Comments
 (0)