Skip to main content

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