@@ -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 }
0 commit comments