Skip to content

Commit db9f85e

Browse files
committed
feat: Add support for webhook API
1 parent 5e84bdf commit db9f85e

File tree

5 files changed

+427
-2
lines changed

5 files changed

+427
-2
lines changed

.code-samples.meilisearch.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,3 +1956,22 @@ update_embedders_1: |-
19561956
.set_embedders(&embedders)
19571957
.await
19581958
.unwrap();
1959+
webhooks_get_1: |-
1960+
let webhooks = client.get_webhooks().await.unwrap();
1961+
webhooks_get_single_1: |-
1962+
let webhook = client.get_webhook("WEBHOOK_UUID").await.unwrap();
1963+
webhooks_post_1: |-
1964+
let mut payload = WebhookCreate::new("WEBHOOK_TARGET_URL");
1965+
payload
1966+
.insert_header("authorization", "SECURITY_KEY")
1967+
.insert_header("referer", "https://example.com");
1968+
let webhook = client.create_webhook(&payload).await.unwrap();
1969+
webhooks_patch_1: |-
1970+
let mut update = WebhookUpdate::new();
1971+
update.remove_header("referer");
1972+
let webhook = client
1973+
.update_webhook("WEBHOOK_UUID", &update)
1974+
.await
1975+
.unwrap();
1976+
webhooks_delete_1: |-
1977+
client.delete_webhook("WEBHOOK_UUID").await.unwrap();

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ meilisearch-index-setting-macro = { path = "meilisearch-index-setting-macro", ve
2626
pin-project-lite = { version = "0.2.16", optional = true }
2727
reqwest = { version = "0.12.22", optional = true, default-features = false, features = ["http2", "stream"] }
2828
bytes = { version = "1.10.1", optional = true }
29-
uuid = { version = "1.17.0", features = ["v4"] }
29+
uuid = { version = "1.17.0", features = ["v4", "serde"] }
3030
futures-core = "0.3.31"
3131
futures-io = "0.3.31"
3232
futures-channel = "0.3.31"
@@ -37,7 +37,7 @@ jsonwebtoken = { version = "9.3.1", default-features = false }
3737
tokio = { version = "1.38", optional = true, features = ["time"] }
3838

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

src/client.rs

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::{
1414
task_info::TaskInfo,
1515
tasks::{Task, TasksCancelQuery, TasksDeleteQuery, TasksResults, TasksSearchQuery},
1616
utils::SleepBackend,
17+
webhooks::{WebhookCreate, WebhookInfo, WebhookList, WebhookUpdate},
1718
DefaultHttpClient,
1819
};
1920

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

1193+
/// List all webhooks registered on the Meilisearch instance.
1194+
///
1195+
///
1196+
/// # Example
1197+
///
1198+
/// ```
1199+
/// # use meilisearch_sdk::{client::*, webhooks::*};
1200+
/// #
1201+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
1202+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1203+
/// #
1204+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
1205+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
1206+
/// if let Ok(webhooks) = client.get_webhooks().await {
1207+
/// println!("{}", webhooks.results.len());
1208+
/// }
1209+
/// # });
1210+
/// ```
1211+
pub async fn get_webhooks(&self) -> Result<WebhookList, Error> {
1212+
self.http_client
1213+
.request::<(), (), WebhookList>(
1214+
&format!("{}/webhooks", self.host),
1215+
Method::Get { query: () },
1216+
200,
1217+
)
1218+
.await
1219+
}
1220+
1221+
/// Retrieve a single webhook by its UUID.
1222+
///
1223+
///
1224+
/// # Example
1225+
///
1226+
/// ```
1227+
/// # use meilisearch_sdk::{client::*, webhooks::*};
1228+
/// #
1229+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
1230+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1231+
/// #
1232+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
1233+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
1234+
/// # if let Ok(created) = client.create_webhook(&WebhookCreate::new("https://example.com")).await {
1235+
/// if let Ok(webhook) = client.get_webhook(&created.uuid.to_string()).await {
1236+
/// println!("{}", webhook.webhook.url);
1237+
/// # let _ = client.delete_webhook(&webhook.uuid.to_string()).await;
1238+
/// }
1239+
/// # }
1240+
/// # });
1241+
/// ```
1242+
pub async fn get_webhook(&self, uuid: impl AsRef<str>) -> Result<WebhookInfo, Error> {
1243+
self.http_client
1244+
.request::<(), (), WebhookInfo>(
1245+
&format!("{}/webhooks/{}", self.host, uuid.as_ref()),
1246+
Method::Get { query: () },
1247+
200,
1248+
)
1249+
.await
1250+
}
1251+
1252+
/// Create a new webhook.
1253+
///
1254+
///
1255+
/// # Example
1256+
///
1257+
/// ```
1258+
/// # use meilisearch_sdk::{client::*, webhooks::*};
1259+
/// #
1260+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
1261+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1262+
/// #
1263+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
1264+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
1265+
/// if let Ok(webhook) = client
1266+
/// .create_webhook(&WebhookCreate::new("https://example.com/webhook"))
1267+
/// .await
1268+
/// {
1269+
/// assert!(webhook.is_editable);
1270+
/// # let _ = client.delete_webhook(&webhook.uuid.to_string()).await;
1271+
/// }
1272+
/// # });
1273+
/// ```
1274+
pub async fn create_webhook(&self, webhook: &WebhookCreate) -> Result<WebhookInfo, Error> {
1275+
self.http_client
1276+
.request::<(), &WebhookCreate, WebhookInfo>(
1277+
&format!("{}/webhooks", self.host),
1278+
Method::Post {
1279+
query: (),
1280+
body: webhook,
1281+
},
1282+
201,
1283+
)
1284+
.await
1285+
}
1286+
1287+
/// Update an existing webhook.
1288+
///
1289+
///
1290+
/// # Example
1291+
///
1292+
/// ```
1293+
/// # use meilisearch_sdk::{client::*, webhooks::*};
1294+
/// #
1295+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
1296+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1297+
/// #
1298+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
1299+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
1300+
/// if let Ok(webhook) = client.create_webhook(&WebhookCreate::new("https://example.com")).await {
1301+
/// let mut update = WebhookUpdate::new();
1302+
/// update.set_header("authorization", "SECURITY_KEY");
1303+
/// let _ = client
1304+
/// .update_webhook(&webhook.uuid.to_string(), &update)
1305+
/// .await;
1306+
/// # let _ = client.delete_webhook(&webhook.uuid.to_string()).await;
1307+
/// }
1308+
/// # });
1309+
/// ```
1310+
pub async fn update_webhook(
1311+
&self,
1312+
uuid: impl AsRef<str>,
1313+
webhook: &WebhookUpdate,
1314+
) -> Result<WebhookInfo, Error> {
1315+
self.http_client
1316+
.request::<(), &WebhookUpdate, WebhookInfo>(
1317+
&format!("{}/webhooks/{}", self.host, uuid.as_ref()),
1318+
Method::Patch {
1319+
query: (),
1320+
body: webhook,
1321+
},
1322+
200,
1323+
)
1324+
.await
1325+
}
1326+
1327+
/// Delete a webhook by its UUID.
1328+
///
1329+
///
1330+
/// # Example
1331+
///
1332+
/// ```
1333+
/// # use meilisearch_sdk::{client::*, webhooks::*};
1334+
/// #
1335+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
1336+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1337+
/// #
1338+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
1339+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
1340+
/// if let Ok(webhook) = client.create_webhook(&WebhookCreate::new("https://example.com")).await {
1341+
/// let _ = client.delete_webhook(&webhook.uuid.to_string()).await;
1342+
/// }
1343+
/// # });
1344+
/// ```
1345+
pub async fn delete_webhook(&self, uuid: impl AsRef<str>) -> Result<(), Error> {
1346+
self.http_client
1347+
.request::<(), (), ()>(
1348+
&format!("{}/webhooks/{}", self.host, uuid.as_ref()),
1349+
Method::Delete { query: () },
1350+
204,
1351+
)
1352+
.await
1353+
}
1354+
11921355
fn sleep_backend(&self) -> SleepBackend {
11931356
SleepBackend::infer(self.http_client.is_tokio())
11941357
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ pub mod tasks;
264264
mod tenant_tokens;
265265
/// Module containing utilizes functions.
266266
mod utils;
267+
/// Module to manage webhooks.
268+
pub mod webhooks;
267269

268270
#[cfg(feature = "reqwest")]
269271
pub mod reqwest;

0 commit comments

Comments
 (0)