Skip to content

Commit

Permalink
Merge #509 #516
Browse files Browse the repository at this point in the history
509: Add total field to get-tasks route r=irevoire a=hmacr

# Pull Request

## Related issue
Fixes #504 

## What does this PR do?
- Adds `total` field to `get-tasks` route

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


516: Implement client for experimental-features API r=irevoire a=hmacr

# Pull Request

## Related issue
Needed for #498 & #501, since these issues involve updating the experimental-feature as a pre-requisite

## What does this PR do?
- Introduces the experimental-features client in the SDK
- Implements the `get` and `patch` endpoint.
- Adds code-samples and tests.

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: hmacr <hmac.devo@gmail.com>
  • Loading branch information
meili-bors[bot] and hmacr authored Sep 12, 2023
3 parents d74e2bb + 27230ab + 01e416d commit b7fa203
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1665,3 +1665,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
1 change: 1 addition & 0 deletions src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub enum TaskType {
#[derive(Debug, Clone, Deserialize)]
pub struct TasksResults {
pub results: Vec<Task>,
pub total: u64,
pub limit: u32,
pub from: Option<u32>,
pub next: Option<u32>,
Expand Down

0 comments on commit b7fa203

Please sign in to comment.