Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1956,3 +1956,22 @@ update_embedders_1: |-
.set_embedders(&embedders)
.await
.unwrap();
webhooks_get_1: |-
let webhooks = client.get_webhooks().await.unwrap();
webhooks_get_single_1: |-
let webhook = client.get_webhook("WEBHOOK_UUID").await.unwrap();
webhooks_post_1: |-
let mut payload = WebhookCreate::new("WEBHOOK_TARGET_URL");
payload
.insert_header("authorization", "SECURITY_KEY")
.insert_header("referer", "https://example.com");
let webhook = client.create_webhook(&payload).await.unwrap();
webhooks_patch_1: |-
let mut update = WebhookUpdate::new();
update.remove_header("referer");
let webhook = client
.update_webhook("WEBHOOK_UUID", &update)
.await
.unwrap();
webhooks_delete_1: |-
client.delete_webhook("WEBHOOK_UUID").await.unwrap();
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ meilisearch-index-setting-macro = { path = "meilisearch-index-setting-macro", ve
pin-project-lite = { version = "0.2.16", optional = true }
reqwest = { version = "0.12.22", optional = true, default-features = false, features = ["http2", "stream"] }
bytes = { version = "1.10.1", optional = true }
uuid = { version = "1.17.0", features = ["v4"] }
uuid = { version = "1.17.0", features = ["v4", "serde"] }
futures-core = "0.3.31"
futures-io = "0.3.31"
futures-channel = "0.3.31"
Expand All @@ -37,7 +37,7 @@ jsonwebtoken = { version = "9.3.1", default-features = false }
tokio = { version = "1.38", optional = true, features = ["time"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
uuid = { version = "1.17.0", default-features = false, features = ["v4", "js"] }
uuid = { version = "1.17.0", default-features = false, features = ["v4", "js", "serde"] }
web-sys = "0.3.77"
wasm-bindgen-futures = "0.4"

Expand Down
163 changes: 163 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
task_info::TaskInfo,
tasks::{Task, TasksCancelQuery, TasksDeleteQuery, TasksResults, TasksSearchQuery},
utils::SleepBackend,
webhooks::{WebhookCreate, WebhookInfo, WebhookList, WebhookUpdate},
DefaultHttpClient,
};

Expand Down Expand Up @@ -1189,6 +1190,168 @@ impl<Http: HttpClient> Client<Http> {
self.update_network_state(&update).await
}

/// List all webhooks registered on the Meilisearch instance.
///
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, webhooks::*};
/// #
/// # 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();
/// if let Ok(webhooks) = client.get_webhooks().await {
/// println!("{}", webhooks.results.len());
/// }
/// # });
/// ```
pub async fn get_webhooks(&self) -> Result<WebhookList, Error> {
self.http_client
.request::<(), (), WebhookList>(
&format!("{}/webhooks", self.host),
Method::Get { query: () },
200,
)
.await
}

/// Retrieve a single webhook by its UUID.
///
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, webhooks::*};
/// #
/// # 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();
/// # if let Ok(created) = client.create_webhook(&WebhookCreate::new("https://example.com")).await {
/// if let Ok(webhook) = client.get_webhook(&created.uuid.to_string()).await {
/// println!("{}", webhook.webhook.url);
/// # let _ = client.delete_webhook(&webhook.uuid.to_string()).await;
/// }
/// # }
/// # });
/// ```
pub async fn get_webhook(&self, uuid: impl AsRef<str>) -> Result<WebhookInfo, Error> {
self.http_client
.request::<(), (), WebhookInfo>(
&format!("{}/webhooks/{}", self.host, uuid.as_ref()),
Method::Get { query: () },
200,
)
.await
}

/// Create a new webhook.
///
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, webhooks::*};
/// #
/// # 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();
/// if let Ok(webhook) = client
/// .create_webhook(&WebhookCreate::new("https://example.com/webhook"))
/// .await
/// {
/// assert!(webhook.is_editable);
/// # let _ = client.delete_webhook(&webhook.uuid.to_string()).await;
/// }
/// # });
/// ```
pub async fn create_webhook(&self, webhook: &WebhookCreate) -> Result<WebhookInfo, Error> {
self.http_client
.request::<(), &WebhookCreate, WebhookInfo>(
&format!("{}/webhooks", self.host),
Method::Post {
query: (),
body: webhook,
},
201,
)
.await
}

/// Update an existing webhook.
///
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, webhooks::*};
/// #
/// # 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();
/// if let Ok(webhook) = client.create_webhook(&WebhookCreate::new("https://example.com")).await {
/// let mut update = WebhookUpdate::new();
/// update.set_header("authorization", "SECURITY_KEY");
/// let _ = client
/// .update_webhook(&webhook.uuid.to_string(), &update)
/// .await;
/// # let _ = client.delete_webhook(&webhook.uuid.to_string()).await;
/// }
/// # });
/// ```
pub async fn update_webhook(
&self,
uuid: impl AsRef<str>,
webhook: &WebhookUpdate,
) -> Result<WebhookInfo, Error> {
self.http_client
.request::<(), &WebhookUpdate, WebhookInfo>(
&format!("{}/webhooks/{}", self.host, uuid.as_ref()),
Method::Patch {
query: (),
body: webhook,
},
200,
)
.await
}

/// Delete a webhook by its UUID.
///
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, webhooks::*};
/// #
/// # 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();
/// if let Ok(webhook) = client.create_webhook(&WebhookCreate::new("https://example.com")).await {
/// let _ = client.delete_webhook(&webhook.uuid.to_string()).await;
/// }
/// # });
/// ```
pub async fn delete_webhook(&self, uuid: impl AsRef<str>) -> Result<(), Error> {
self.http_client
.request::<(), (), ()>(
&format!("{}/webhooks/{}", self.host, uuid.as_ref()),
Method::Delete { query: () },
204,
)
.await
}

fn sleep_backend(&self) -> SleepBackend {
SleepBackend::infer(self.http_client.is_tokio())
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ pub mod tasks;
mod tenant_tokens;
/// Module containing utilizes functions.
mod utils;
/// Module to manage webhooks.
pub mod webhooks;

#[cfg(feature = "reqwest")]
pub mod reqwest;
Expand Down
Loading