Skip to main content

casdoor_rust_sdk/
pricing.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/// Pricing has the same definition as
22/// <https://github.com/casdoor/casdoor/blob/master/object/pricing.go>.
23#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
24#[serde(rename_all = "camelCase", default)]
25pub struct Pricing {
26    pub owner: String,
27    pub name: String,
28    pub created_time: String,
29    pub display_name: String,
30    pub description: String,
31
32    #[serde(deserialize_with = "null_to_default")]
33    pub plans: Vec<String>,
34    pub is_enabled: bool,
35    pub trial_duration: i32,
36    pub application: String,
37}
38
39impl Client {
40    /// Get the pricings of the client's organization.
41    pub async fn get_pricings(&self) -> Result<Vec<Pricing>> {
42        self.get_list("get-pricings", &[("owner", &self.config.organization_name)])
43            .await
44    }
45
46    /// Get one page of the pricings of the client's organization, together with the total
47    /// number of the pricings.
48    pub async fn get_pagination_pricings(
49        &self,
50        p: i32,
51        page_size: i32,
52        query: &[(&str, &str)],
53    ) -> Result<(Vec<Pricing>, i32)> {
54        self.get_pagination(
55            "get-pricings",
56            &self.config.organization_name,
57            p,
58            page_size,
59            query,
60        )
61        .await
62    }
63
64    /// Get one pricing by name.
65    pub async fn get_pricing(&self, name: &str) -> Result<Option<Pricing>> {
66        self.get_object("get-pricing", &[("id", &self.get_id(name))])
67            .await
68    }
69
70    /// Add a pricing.
71    pub async fn add_pricing(&self, pricing: &Pricing) -> Result<bool> {
72        Ok(self.modify_pricing("add-pricing", pricing, &[]).await?.1)
73    }
74
75    /// Update a pricing.
76    pub async fn update_pricing(&self, pricing: &Pricing) -> Result<bool> {
77        Ok(self.modify_pricing("update-pricing", pricing, &[]).await?.1)
78    }
79
80    /// Delete a pricing.
81    pub async fn delete_pricing(&self, pricing: &Pricing) -> Result<bool> {
82        Ok(self.modify_pricing("delete-pricing", pricing, &[]).await?.1)
83    }
84
85    async fn modify_pricing(
86        &self,
87        action: &str,
88        pricing: &Pricing,
89        columns: &[&str],
90    ) -> Result<(Response, bool)> {
91        let mut pricing = pricing.clone();
92        pricing.owner = get_owner(&pricing.owner, &self.config.organization_name);
93
94        let id = format!("{}/{}", pricing.owner, pricing.name);
95        self.modify_object(action, &id, &pricing, columns).await
96    }
97}