Skip to content

Commit 653c367

Browse files
committed
Polish webhook helpers
1 parent db9f85e commit 653c367

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

.code-samples.meilisearch.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,13 +1961,13 @@ webhooks_get_1: |-
19611961
webhooks_get_single_1: |-
19621962
let webhook = client.get_webhook("WEBHOOK_UUID").await.unwrap();
19631963
webhooks_post_1: |-
1964-
let mut payload = WebhookCreate::new("WEBHOOK_TARGET_URL");
1964+
let mut payload = meilisearch_sdk::webhooks::WebhookCreate::new("WEBHOOK_TARGET_URL");
19651965
payload
19661966
.insert_header("authorization", "SECURITY_KEY")
19671967
.insert_header("referer", "https://example.com");
19681968
let webhook = client.create_webhook(&payload).await.unwrap();
19691969
webhooks_patch_1: |-
1970-
let mut update = WebhookUpdate::new();
1970+
let mut update = meilisearch_sdk::webhooks::WebhookUpdate::new();
19711971
update.remove_header("referer");
19721972
let webhook = client
19731973
.update_webhook("WEBHOOK_UUID", &update)

src/webhooks.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use serde::Deserialize;
22
use serde::{ser::SerializeMap, Serialize, Serializer};
3-
use serde_json::Value;
43
use std::collections::BTreeMap;
54
use uuid::Uuid;
65

@@ -93,6 +92,12 @@ impl WebhookUpdate {
9392
Self::default()
9493
}
9594

95+
#[must_use]
96+
pub fn with_url_owned(mut self, url: impl Into<String>) -> Self {
97+
self.url = Some(url.into());
98+
self
99+
}
100+
96101
/// Updates the webhook target URL.
97102
pub fn with_url(&mut self, url: impl Into<String>) -> &mut Self {
98103
self.url = Some(url.into());
@@ -156,7 +161,8 @@ impl Serialize for WebhookUpdate {
156161
match &self.headers {
157162
HeadersUpdate::NotSet => {}
158163
HeadersUpdate::Reset => {
159-
map.serialize_entry("headers", &Value::Null)?;
164+
let none: Option<()> = None;
165+
map.serialize_entry("headers", &none)?;
160166
}
161167
HeadersUpdate::Set(values) => {
162168
map.serialize_entry("headers", values)?;
@@ -228,6 +234,13 @@ mod test {
228234
Some(&"value".to_string())
229235
);
230236

237+
let mut clear = WebhookUpdate::new();
238+
clear.reset_headers();
239+
let cleared = client
240+
.update_webhook(&created.uuid.to_string(), &clear)
241+
.await?;
242+
assert!(cleared.webhook.headers.is_empty());
243+
231244
client.delete_webhook(&created.uuid.to_string()).await?;
232245

233246
let remaining = client.get_webhooks().await?;

0 commit comments

Comments
 (0)