Skip to content

Support Network API routes #657

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

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
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
20 changes: 20 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1823,3 +1823,23 @@ reset_localized_attribute_settings_1: |-
.reset_localized_attributes()
.await
.unwrap();
get_network_1: |-
let network: Network = client.get_network().await.unwrap();
update_network_1: |-
client.update_network(
NetworkUpdate::new()
.with_self("ms-00")
.with_remotes(&[
Remote {
name: "ms-00".to_string(),
url: "http://INSTANCE_URL".to_string(),
search_api_key: Some("INSTANCE_API_KEY".to_string()),
},
Remote {
name: "ms-01".to_string(),
url: "http://ANOTHER_INSTANCE_URL".to_string(),
search_api_key: Some("ANOTHER_INSTANCE_API_KEY".to_string()),
},
]),
)
.await.unwrap();
150 changes: 149 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
errors::*,
indexes::*,
key::{Key, KeyBuilder, KeyUpdater, KeysQuery, KeysResults},
network::{Network, NetworkUpdate},
request::*,
search::*,
task_info::TaskInfo,
Expand Down Expand Up @@ -1107,6 +1108,76 @@ impl<Http: HttpClient> Client<Http> {
Ok(tasks)
}

/// Get the network configuration (sharding).
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, features::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");
/// #
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
/// # ExperimentalFeatures::new(&client).set_network(true).update().await.unwrap();
/// let network = client.get_network().await.unwrap();
/// # });
/// ```
pub async fn get_network(&self) -> Result<Network, Error> {
let network = self
.http_client
.request::<(), (), Network>(
&format!("{}/network", self.host),
Method::Get { query: () },
200,
)
.await?;

Ok(network)
}

/// Update the network configuration (sharding).
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, features::ExperimentalFeatures, network::*};
/// #
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
/// #
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
/// # ExperimentalFeatures::new(&client).set_network(true).update().await.unwrap();
/// let network = client.update_network(
/// NetworkUpdate::new()
/// // .reset_self()
/// // .reset_remotes()
/// // .delete_remotes(&["ms-00"])
/// .with_self("ms-00")
/// .with_remotes(&[Remote {
/// name: "ms-00".to_string(),
/// url: "http://localhost:7700".to_string(),
/// search_api_key: Some("secret".to_string()),
/// }]),
/// )
/// .await.unwrap();
/// # });
/// ```
pub async fn update_network(&self, network_update: &NetworkUpdate) -> Result<Network, Error> {
self.http_client
.request::<(), &NetworkUpdate, Network>(
&format!("{}/network", self.host),
Method::Patch {
body: network_update,
query: (),
},
200,
)
.await
}

/// Generates a new tenant token.
///
/// # Example
Expand Down Expand Up @@ -1205,7 +1276,10 @@ mod tests {

use meilisearch_test_macro::meilisearch_test;

use crate::{client::*, key::Action, reqwest::qualified_version};
use crate::{
client::*, features::ExperimentalFeatures, key::Action, network::Remote,
reqwest::qualified_version,
};

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Document {
Expand Down Expand Up @@ -1355,6 +1429,80 @@ mod tests {
assert_eq!(tasks.limit, 20);
}

async fn enable_network(client: &Client) {
ExperimentalFeatures::new(client)
.set_network(true)
.update()
.await
.unwrap();
}

#[meilisearch_test]
async fn test_get_network(client: Client) {
enable_network(&client).await;

let network = client.get_network().await.unwrap();
assert!(matches!(
network,
Network {
self_: _,
remotes: _,
}
))
}

#[meilisearch_test]
async fn test_update_network_self(client: Client) {
enable_network(&client).await;

client
.update_network(NetworkUpdate::new().reset_self())
.await
.unwrap();

let network = client
.update_network(NetworkUpdate::new().with_self("ms-00"))
.await
.unwrap();

assert_eq!(network.self_, Some("ms-00".to_string()));
}

#[meilisearch_test]
async fn test_update_network_remotes(client: Client) {
enable_network(&client).await;

client
.update_network(NetworkUpdate::new().reset_remotes())
.await
.unwrap();

let network = client
.update_network(NetworkUpdate::new().with_remotes(&[Remote {
name: "ms-00".to_string(),
url: "http://localhost:7700".to_string(),
search_api_key: Some("secret".to_string()),
}]))
.await
.unwrap();

assert_eq!(
network.remotes,
vec![Remote {
name: "ms-00".to_string(),
url: "http://localhost:7700".to_string(),
search_api_key: Some("secret".to_string()),
}]
);

let network = client
.update_network(NetworkUpdate::new().delete_remotes(&["ms-00"]))
.await
.unwrap();

assert_eq!(network.remotes, vec![]);
}

#[meilisearch_test]
async fn test_get_keys(client: Client) {
let keys = client.get_keys().await.unwrap();
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ pub mod features;
pub mod indexes;
/// Module containing the [`Key`](key::Key) struct.
pub mod key;
// Module containing the [`Network`](network::Network) struct.
pub mod network;
pub mod request;
/// Module related to search queries and results.
pub mod search;
Expand Down
Loading
Loading