|
| 1 | +use crate::{ |
| 2 | + request::{request, Method}, |
| 3 | + Client, Error, |
| 4 | +}; |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | + |
| 7 | +/// Struct representing the experimental features result from the API. |
| 8 | +#[derive(Clone, Debug, Deserialize)] |
| 9 | +#[serde(rename_all = "camelCase")] |
| 10 | +pub struct ExperimentalFeaturesResult { |
| 11 | + pub score_details: bool, |
| 12 | + pub vector_store: bool, |
| 13 | +} |
| 14 | + |
| 15 | +/// Struct representing the experimental features request. |
| 16 | +/// |
| 17 | +/// You can build this struct using the builder pattern. |
| 18 | +/// |
| 19 | +/// # Example |
| 20 | +/// |
| 21 | +/// ``` |
| 22 | +/// # use meilisearch_sdk::{Client, ExperimentalFeatures}; |
| 23 | +/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700"); |
| 24 | +/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey"); |
| 25 | +/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)); |
| 26 | +/// let mut features = ExperimentalFeatures::new(&client); |
| 27 | +/// features.set_score_details(true); |
| 28 | +/// ``` |
| 29 | +#[derive(Debug, Serialize)] |
| 30 | +#[serde(rename_all = "camelCase")] |
| 31 | +pub struct ExperimentalFeatures<'a> { |
| 32 | + #[serde(skip_serializing)] |
| 33 | + client: &'a Client, |
| 34 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 35 | + pub score_details: Option<bool>, |
| 36 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 37 | + pub vector_store: Option<bool>, |
| 38 | +} |
| 39 | + |
| 40 | +impl<'a> ExperimentalFeatures<'a> { |
| 41 | + pub fn new(client: &'a Client) -> Self { |
| 42 | + ExperimentalFeatures { |
| 43 | + client, |
| 44 | + score_details: None, |
| 45 | + vector_store: None, |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + pub fn set_score_details(&mut self, score_details: bool) -> &mut Self { |
| 50 | + self.score_details = Some(score_details); |
| 51 | + self |
| 52 | + } |
| 53 | + |
| 54 | + pub fn set_vector_store(&mut self, vector_store: bool) -> &mut Self { |
| 55 | + self.vector_store = Some(vector_store); |
| 56 | + self |
| 57 | + } |
| 58 | + |
| 59 | + /// Get all the experimental features |
| 60 | + /// |
| 61 | + /// # Example |
| 62 | + /// |
| 63 | + /// ``` |
| 64 | + /// # use meilisearch_sdk::{Client, ExperimentalFeatures}; |
| 65 | + /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700"); |
| 66 | + /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey"); |
| 67 | + /// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)); |
| 68 | + /// futures::executor::block_on(async move { |
| 69 | + /// let features = ExperimentalFeatures::new(&client); |
| 70 | + /// features.get().await.unwrap(); |
| 71 | + /// }); |
| 72 | + /// ``` |
| 73 | + pub async fn get(&self) -> Result<ExperimentalFeaturesResult, Error> { |
| 74 | + request::<(), (), ExperimentalFeaturesResult>( |
| 75 | + &format!("{}/experimental-features", self.client.host), |
| 76 | + self.client.get_api_key(), |
| 77 | + Method::Get { query: () }, |
| 78 | + 200, |
| 79 | + ) |
| 80 | + .await |
| 81 | + } |
| 82 | + |
| 83 | + /// Update the experimental features |
| 84 | + /// |
| 85 | + /// # Example |
| 86 | + /// |
| 87 | + /// ``` |
| 88 | + /// # use meilisearch_sdk::{Client, ExperimentalFeatures}; |
| 89 | + /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700"); |
| 90 | + /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey"); |
| 91 | + /// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)); |
| 92 | + /// futures::executor::block_on(async move { |
| 93 | + /// let mut features = ExperimentalFeatures::new(&client); |
| 94 | + /// features.set_score_details(true); |
| 95 | + /// features.update().await.unwrap(); |
| 96 | + /// }); |
| 97 | + /// ``` |
| 98 | + pub async fn update(&self) -> Result<ExperimentalFeaturesResult, Error> { |
| 99 | + request::<(), &Self, ExperimentalFeaturesResult>( |
| 100 | + &format!("{}/experimental-features", self.client.host), |
| 101 | + self.client.get_api_key(), |
| 102 | + Method::Patch { |
| 103 | + query: (), |
| 104 | + body: self, |
| 105 | + }, |
| 106 | + 200, |
| 107 | + ) |
| 108 | + .await |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +#[cfg(test)] |
| 113 | +mod tests { |
| 114 | + use super::*; |
| 115 | + use meilisearch_test_macro::meilisearch_test; |
| 116 | + |
| 117 | + #[meilisearch_test] |
| 118 | + async fn test_experimental_features_get(client: Client) { |
| 119 | + let mut features = ExperimentalFeatures::new(&client); |
| 120 | + features.set_score_details(false); |
| 121 | + features.set_vector_store(false); |
| 122 | + let _ = features.update().await.unwrap(); |
| 123 | + |
| 124 | + let res = features.get().await.unwrap(); |
| 125 | + |
| 126 | + assert!(!res.score_details); |
| 127 | + assert!(!res.vector_store); |
| 128 | + } |
| 129 | + |
| 130 | + #[meilisearch_test] |
| 131 | + async fn test_experimental_features_enable_score_details(client: Client) { |
| 132 | + let mut features = ExperimentalFeatures::new(&client); |
| 133 | + features.set_score_details(true); |
| 134 | + |
| 135 | + let res = features.update().await.unwrap(); |
| 136 | + |
| 137 | + assert!(res.score_details); |
| 138 | + } |
| 139 | + |
| 140 | + #[meilisearch_test] |
| 141 | + async fn test_experimental_features_enable_vector_store(client: Client) { |
| 142 | + let mut features = ExperimentalFeatures::new(&client); |
| 143 | + features.set_vector_store(true); |
| 144 | + |
| 145 | + let res = features.update().await.unwrap(); |
| 146 | + |
| 147 | + assert!(res.vector_store); |
| 148 | + } |
| 149 | +} |
0 commit comments