Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement client for experimental-features API #516

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1656,3 +1656,19 @@ multi_search_1: |-
.execute::<MovieRatings>()
.await
.unwrap();
get_experimental_features_1: |-
let client = Client::new("http://localhost:7700", Some("apiKey"));
let features = ExperimentalFeatures::new(&client);
let res = features
.get()
.await
.unwrap();
update_experimental_features_1: |-
let client = Client::new("http://localhost:7700", Some("apiKey"));
let mut features = ExperimentalFeatures::new(&client);
features.set_score_details(true);
features.set_vector_store(true);
let res = features
.update()
.await
.unwrap();
149 changes: 149 additions & 0 deletions src/features.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
use crate::{
request::{request, Method},
Client, Error,
};
use serde::{Deserialize, Serialize};

/// Struct representing the experimental features result from the API.
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExperimentalFeaturesResult {
pub score_details: bool,
pub vector_store: bool,
}

/// Struct representing the experimental features request.
///
/// You can build this struct using the builder pattern.
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{Client, ExperimentalFeatures};
/// # 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));
/// let mut features = ExperimentalFeatures::new(&client);
/// features.set_score_details(true);
/// ```
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExperimentalFeatures<'a> {
#[serde(skip_serializing)]
client: &'a Client,
#[serde(skip_serializing_if = "Option::is_none")]
pub score_details: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub vector_store: Option<bool>,
}

impl<'a> ExperimentalFeatures<'a> {
pub fn new(client: &'a Client) -> Self {
ExperimentalFeatures {
client,
score_details: None,
vector_store: None,
}
}

pub fn set_score_details(&mut self, score_details: bool) -> &mut Self {
self.score_details = Some(score_details);
self
}

pub fn set_vector_store(&mut self, vector_store: bool) -> &mut Self {
self.vector_store = Some(vector_store);
self
}

/// Get all the experimental features
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{Client, ExperimentalFeatures};
/// # 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));
/// futures::executor::block_on(async move {
/// let features = ExperimentalFeatures::new(&client);
/// features.get().await.unwrap();
/// });
/// ```
pub async fn get(&self) -> Result<ExperimentalFeaturesResult, Error> {
request::<(), (), ExperimentalFeaturesResult>(
&format!("{}/experimental-features", self.client.host),
self.client.get_api_key(),
Method::Get { query: () },
200,
)
.await
}

/// Update the experimental features
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{Client, ExperimentalFeatures};
/// # 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));
/// futures::executor::block_on(async move {
/// let mut features = ExperimentalFeatures::new(&client);
/// features.set_score_details(true);
/// features.update().await.unwrap();
/// });
/// ```
pub async fn update(&self) -> Result<ExperimentalFeaturesResult, Error> {
request::<(), &Self, ExperimentalFeaturesResult>(
&format!("{}/experimental-features", self.client.host),
self.client.get_api_key(),
Method::Patch {
query: (),
body: self,
},
200,
)
.await
}
}

#[cfg(test)]
mod tests {
use super::*;
use meilisearch_test_macro::meilisearch_test;

#[meilisearch_test]
async fn test_experimental_features_get(client: Client) {
let mut features = ExperimentalFeatures::new(&client);
features.set_score_details(false);
features.set_vector_store(false);
let _ = features.update().await.unwrap();

let res = features.get().await.unwrap();

assert!(!res.score_details);
assert!(!res.vector_store);
}

#[meilisearch_test]
async fn test_experimental_features_enable_score_details(client: Client) {
let mut features = ExperimentalFeatures::new(&client);
features.set_score_details(true);

let res = features.update().await.unwrap();

assert!(res.score_details);
}

#[meilisearch_test]
async fn test_experimental_features_enable_vector_store(client: Client) {
let mut features = ExperimentalFeatures::new(&client);
features.set_vector_store(true);

let res = features.update().await.unwrap();

assert!(res.vector_store);
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ pub mod documents;
pub mod dumps;
/// Module containing the [`errors::Error`] struct.
pub mod errors;
/// Module related to runtime and instance features.
pub mod features;
/// Module containing the Index struct.
pub mod indexes;
/// Module containing the [`key::Key`] struct.
Expand All @@ -253,6 +255,7 @@ pub use client::*;
pub use documents::*;
pub use dumps::*;
pub use errors::*;
pub use features::*;
pub use indexes::*;
pub use key::*;
pub use search::*;
Expand Down
Loading