Skip to main content

casdoor_rust_sdk/
notification.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::Serialize;
16
17use crate::client::{Client, PostBody};
18use crate::error::Result;
19
20#[derive(Serialize)]
21struct NotificationForm<'a> {
22    content: &'a str,
23    #[serde(skip_serializing_if = "is_empty")]
24    recipient: &'a str,
25}
26
27fn is_empty(value: &&str) -> bool {
28    value.is_empty()
29}
30
31impl Client {
32    /// Send a notification with the notification provider of the organization.
33    /// `recipient` can be empty to use the default recipient of the provider.
34    pub async fn send_notification(&self, content: &str, recipient: &str) -> Result<()> {
35        let post_bytes = serde_json::to_vec(&NotificationForm { content, recipient })?;
36
37        self.do_post("send-notification", &[], PostBody::Raw(post_bytes))
38            .await?;
39
40        Ok(())
41    }
42}