1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! The `snapshots` module allows the creation of database snapshots.
//!
//! - snapshots are `.snapshots` files that can be used to launch Meilisearch.
//!
//! - snapshots are not compatible between Meilisearch versions.
//!
//! # Example
//!
//! ```
//! # use meilisearch_sdk::{client::*, errors::*, snapshots::*, snapshots::*, task_info::*, tasks::*};
//! # use futures_await_test::async_test;
//! # use std::{thread::sleep, time::Duration};
//! # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
//! #
//! # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
//! # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
//! #
//! # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
//!
//! // Create a snapshot
//! let task_info = client.create_snapshot().await.unwrap();
//! assert!(matches!(
//!     task_info,
//!     TaskInfo {
//!         update_type: TaskType::SnapshotCreation { .. },
//!         ..
//!     }
//! ));
//! # });
//! ```

use crate::{client::Client, errors::Error, request::*, task_info::TaskInfo};

/// Snapshots related methods.
/// See the [snapshots](crate::snapshots) module.
impl<Http: HttpClient> Client<Http> {
    /// Triggers a snapshots creation process.
    ///
    /// Once the process is complete, a snapshots is created in the [snapshots directory].
    /// If the snapshots directory does not exist yet, it will be created.
    ///
    /// # Example
    ///
    /// ```
    /// # use meilisearch_sdk::{client::*, errors::*, snapshots::*, snapshots::*, task_info::*, tasks::*};
    /// # use futures_await_test::async_test;
    /// # use std::{thread::sleep, time::Duration};
    /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
    /// #
    /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
    /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
    /// #
    /// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
    /// #
    /// let task_info = client.create_snapshot().await.unwrap();
    ///
    /// assert!(matches!(
    ///     task_info,
    ///     TaskInfo {
    ///         update_type: TaskType::SnapshotCreation { .. },
    ///         ..
    ///     }
    /// ));
    /// # });
    /// ```
    pub async fn create_snapshot(&self) -> Result<TaskInfo, Error> {
        self.http_client
            .request::<(), (), TaskInfo>(
                &format!("{}/snapshots", self.host),
                Method::Post {
                    query: (),
                    body: (),
                },
                202,
            )
            .await
    }
}

/// Alias for [`create_snapshot`](Client::create_snapshot).
pub async fn create_snapshot<Http: HttpClient>(client: &Client<Http>) -> Result<TaskInfo, Error> {
    client.create_snapshot().await
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{client::*, tasks::*};
    use meilisearch_test_macro::meilisearch_test;

    #[meilisearch_test]
    async fn test_snapshot_success_creation(client: Client) -> Result<(), Error> {
        let task = client
            .create_snapshot()
            .await?
            .wait_for_completion(&client, None, None)
            .await?;

        assert!(matches!(task, Task::Succeeded { .. }));
        Ok(())
    }

    #[meilisearch_test]
    async fn test_snapshot_correct_update_type(client: Client) -> Result<(), Error> {
        let task_info = client.create_snapshot().await.unwrap();

        assert!(matches!(
            task_info,
            TaskInfo {
                update_type: TaskType::SnapshotCreation { .. },
                ..
            }
        ));
        Ok(())
    }
}