Skip to main content

casdoor_rust_sdk/
webhook.rs

1// Copyright 2026 The Casdoor Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use serde::{Deserialize, Serialize};
16
17use crate::client::{get_owner, Client, Response};
18use crate::error::Result;
19use crate::serde_util::null_to_default;
20
21#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
22#[serde(rename_all = "camelCase", default)]
23pub struct Header {
24    pub name: String,
25    pub value: String,
26}
27
28/// Webhook has the same definition as
29/// <https://github.com/casdoor/casdoor/blob/master/object/webhook.go>.
30#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
31#[serde(rename_all = "camelCase", default)]
32pub struct Webhook {
33    pub owner: String,
34    pub name: String,
35    pub created_time: String,
36
37    pub organization: String,
38
39    pub url: String,
40    pub method: String,
41    pub content_type: String,
42    #[serde(deserialize_with = "null_to_default")]
43    pub headers: Vec<Header>,
44    #[serde(deserialize_with = "null_to_default")]
45    pub events: Vec<String>,
46    #[serde(deserialize_with = "null_to_default")]
47    pub token_fields: Vec<String>,
48    #[serde(deserialize_with = "null_to_default")]
49    pub object_fields: Vec<String>,
50    pub is_user_extended: bool,
51    pub single_org_only: bool,
52    pub is_enabled: bool,
53
54    // Retry configuration.
55    pub max_retries: i32,
56    /// The retry interval in seconds.
57    pub retry_interval: i32,
58    pub use_exponential_backoff: bool,
59}
60
61impl Client {
62    /// Get the webhooks of the client's organization.
63    pub async fn get_webhooks(&self) -> Result<Vec<Webhook>> {
64        self.get_list("get-webhooks", &[("owner", &self.config.organization_name)])
65            .await
66    }
67
68    /// Get one page of the webhooks of the client's organization, together with the total
69    /// number of the webhooks.
70    pub async fn get_pagination_webhooks(
71        &self,
72        p: i32,
73        page_size: i32,
74        query: &[(&str, &str)],
75    ) -> Result<(Vec<Webhook>, i32)> {
76        self.get_pagination(
77            "get-webhooks",
78            &self.config.organization_name,
79            p,
80            page_size,
81            query,
82        )
83        .await
84    }
85
86    /// Get one webhook by name.
87    pub async fn get_webhook(&self, name: &str) -> Result<Option<Webhook>> {
88        self.get_object("get-webhook", &[("id", &self.get_id(name))])
89            .await
90    }
91
92    /// Add a webhook.
93    pub async fn add_webhook(&self, webhook: &Webhook) -> Result<bool> {
94        Ok(self.modify_webhook("add-webhook", webhook, &[]).await?.1)
95    }
96
97    /// Update a webhook.
98    pub async fn update_webhook(&self, webhook: &Webhook) -> Result<bool> {
99        Ok(self.modify_webhook("update-webhook", webhook, &[]).await?.1)
100    }
101
102    /// Delete a webhook.
103    pub async fn delete_webhook(&self, webhook: &Webhook) -> Result<bool> {
104        Ok(self.modify_webhook("delete-webhook", webhook, &[]).await?.1)
105    }
106
107    async fn modify_webhook(
108        &self,
109        action: &str,
110        webhook: &Webhook,
111        columns: &[&str],
112    ) -> Result<(Response, bool)> {
113        let mut webhook = webhook.clone();
114        webhook.owner = get_owner(&webhook.owner, &self.config.organization_name);
115
116        let id = format!("{}/{}", webhook.owner, webhook.name);
117        self.modify_object(action, &id, &webhook, columns).await
118    }
119}