casdoor_rust_sdk/
subscription.rs1use serde::{Deserialize, Serialize};
16
17use crate::client::{get_owner, Client, Response};
18use crate::error::Result;
19
20pub const SUB_STATE_PENDING: &str = "Pending";
22pub const SUB_STATE_ERROR: &str = "Error";
24pub const SUB_STATE_SUSPENDED: &str = "Suspended";
26pub const SUB_STATE_ACTIVE: &str = "Active";
28pub const SUB_STATE_UPCOMING: &str = "Upcoming";
30pub const SUB_STATE_EXPIRED: &str = "Expired";
32
33#[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 pub state: String,
54}
55
56impl Client {
57 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 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 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 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 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 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}