Skip to main content

casdoor_rust_sdk/
subscription.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;
19
20/// The subscription is waiting for its payment.
21pub const SUB_STATE_PENDING: &str = "Pending";
22/// The subscription failed.
23pub const SUB_STATE_ERROR: &str = "Error";
24/// The subscription was suspended by the admin.
25pub const SUB_STATE_SUSPENDED: &str = "Suspended";
26/// The subscription is running.
27pub const SUB_STATE_ACTIVE: &str = "Active";
28/// The subscription has not started yet.
29pub const SUB_STATE_UPCOMING: &str = "Upcoming";
30/// The subscription has ended.
31pub const SUB_STATE_EXPIRED: &str = "Expired";
32
33/// Subscription has the same definition as
34/// <https://github.com/casdoor/casdoor/blob/master/object/subscription.go>.
35#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
36#[serde(rename_all = "camelCase", default)]
37pub struct Subscription {
38    pub owner: String,
39    pub name: String,
40    pub display_name: String,
41    pub created_time: String,
42    pub description: String,
43
44    pub user: String,
45    pub pricing: String,
46    pub plan: String,
47    pub payment: String,
48
49    pub start_time: String,
50    pub end_time: String,
51    pub period: String,
52    /// One of the `SUB_STATE_*` constants of this module.
53    pub state: String,
54}
55
56impl Client {
57    /// Get the subscriptions of the client's organization.
58    pub async fn get_subscriptions(&self) -> Result<Vec<Subscription>> {
59        self.get_list(
60            "get-subscriptions",
61            &[("owner", &self.config.organization_name)],
62        )
63        .await
64    }
65
66    /// Get one page of the subscriptions of the client's organization, together with the
67    /// total number of the subscriptions.
68    pub async fn get_pagination_subscriptions(
69        &self,
70        p: i32,
71        page_size: i32,
72        query: &[(&str, &str)],
73    ) -> Result<(Vec<Subscription>, i32)> {
74        self.get_pagination(
75            "get-subscriptions",
76            &self.config.organization_name,
77            p,
78            page_size,
79            query,
80        )
81        .await
82    }
83
84    /// Get one subscription by name.
85    pub async fn get_subscription(&self, name: &str) -> Result<Option<Subscription>> {
86        self.get_object("get-subscription", &[("id", &self.get_id(name))])
87            .await
88    }
89
90    /// Add a subscription.
91    pub async fn add_subscription(&self, subscription: &Subscription) -> Result<bool> {
92        Ok(self
93            .modify_subscription("add-subscription", subscription, &[])
94            .await?
95            .1)
96    }
97
98    /// Update a subscription.
99    pub async fn update_subscription(&self, subscription: &Subscription) -> Result<bool> {
100        Ok(self
101            .modify_subscription("update-subscription", subscription, &[])
102            .await?
103            .1)
104    }
105
106    /// Delete a subscription.
107    pub async fn delete_subscription(&self, subscription: &Subscription) -> Result<bool> {
108        Ok(self
109            .modify_subscription("delete-subscription", subscription, &[])
110            .await?
111            .1)
112    }
113
114    async fn modify_subscription(
115        &self,
116        action: &str,
117        subscription: &Subscription,
118        columns: &[&str],
119    ) -> Result<(Response, bool)> {
120        let mut subscription = subscription.clone();
121        subscription.owner = get_owner(&subscription.owner, &self.config.organization_name);
122
123        let id = format!("{}/{}", subscription.owner, subscription.name);
124        self.modify_object(action, &id, &subscription, columns)
125            .await
126    }
127}