casdoor_rust_sdk/
webhook.rs1use 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#[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 pub max_retries: i32,
56 pub retry_interval: i32,
58 pub use_exponential_backoff: bool,
59}
60
61impl Client {
62 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 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 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 pub async fn add_webhook(&self, webhook: &Webhook) -> Result<bool> {
94 Ok(self.modify_webhook("add-webhook", webhook, &[]).await?.1)
95 }
96
97 pub async fn update_webhook(&self, webhook: &Webhook) -> Result<bool> {
99 Ok(self.modify_webhook("update-webhook", webhook, &[]).await?.1)
100 }
101
102 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}