openlark_security/models/
mod.rs1use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone)]
9pub struct SecurityConfig {
10 pub app_id: String,
12 pub app_secret: String,
14 pub base_url: String,
16}
17
18impl SecurityConfig {
19 pub fn new(app_id: impl Into<String>, app_secret: impl Into<String>) -> Self {
21 Self {
22 app_id: app_id.into(),
23 app_secret: app_secret.into(),
24 base_url: "https://open.feishu.cn".to_string(),
25 }
26 }
27
28 pub fn with_base_url(mut self, base_url: &str) -> Self {
30 self.base_url = base_url.to_string();
31 self
32 }
33
34 pub async fn get_app_access_token(&self) -> crate::SecurityResult<String> {
38 use openlark_auth::AuthTokenProvider;
39 use openlark_core::{
40 auth::{TokenProvider, TokenRequest},
41 config::Config,
42 };
43
44 let config = Config::builder()
46 .app_id(&self.app_id)
47 .app_secret(&self.app_secret)
48 .base_url(&self.base_url)
49 .build();
50
51 let token_provider = AuthTokenProvider::new(config);
53 let token: String = token_provider
54 .get_token(TokenRequest::app())
55 .await
56 .map_err(|e: openlark_core::CoreError| {
57 openlark_core::error::authentication_error(e.to_string())
58 })?;
59
60 Ok(token)
61 }
62}
63
64impl Default for SecurityConfig {
65 fn default() -> Self {
66 Self {
67 app_id: "".to_string(),
68 app_secret: "".to_string(),
69 base_url: "https://open.feishu.cn".to_string(),
70 }
71 }
72}
73
74#[derive(Debug, Clone, Serialize, Deserialize)]
76pub struct PageRequest {
77 pub page_size: Option<i32>,
79 pub page_token: Option<String>,
81}
82
83impl Default for PageRequest {
84 fn default() -> Self {
85 Self {
86 page_size: Some(20),
87 page_token: None,
88 }
89 }
90}
91
92#[derive(Debug, Clone, Serialize, Deserialize)]
94pub struct PageResponse<T> {
95 pub has_more: bool,
97 pub page_token: Option<String>,
99 pub data: Vec<T>,
101}
102
103#[derive(Debug, Clone, Serialize, Deserialize)]
105pub struct ApiResponse<T> {
106 pub code: i32,
108 pub msg: String,
110 pub data: Option<T>,
112}
113
114pub type Timestamp = i64;
116
117#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
119#[serde(rename_all = "lowercase")]
120pub enum Status {
121 Active,
123 Disabled,
125 Pending,
127 Deleted,
129 Expired,
131}
132
133pub mod acs;
135pub mod common;
136
137pub use acs::{
140 AccessRecord, AccessResult, DeviceBindRuleRequest, DeviceInfo, DeviceStatus, DeviceType,
141 FaceImageInfo, HostInfo, PermissionRuleInfo, PermissionRuleRequest, UserInfo, UserListResponse,
142 UserRequest, VerificationMethod, VisitorInfo, VisitorRequest, VisitorStatus,
143};
144
145pub mod security_and_compliance;
147pub use security_and_compliance::{
149 ApplyStatus, ComplianceCheckResult, ComplianceResult, ComplianceRuleType, ComplianceStatus,
150 DeviceApplyRecord, DeviceApplyRecordApproveRequest, DeviceComplianceRule, DeviceRecord,
151 DeviceRecordRequest, DeviceRecordStatus, DeviceRecordUpdateRequest, OpenApiLog,
152 OpenApiLogQueryRequest,
153};
154
155pub use common::{
158 BatchOperationError, BatchOperationRequest, BatchOperationResponse, DeviceBase, ExtensionMap,
159 FileUploadResponse, GeoLocation, KeyValue, OperationResponse, PermissionBase, QueryCondition,
160 SortCondition, SortDirection, TimeRange, UserBase,
161};
162
163#[cfg(test)]
164mod tests {
165
166 use serde_json;
167
168 #[test]
169 fn test_serialization_roundtrip() {
170 let json = r#"{"test": "value"}"#;
172 assert!(serde_json::from_str::<serde_json::Value>(json).is_ok());
173 }
174
175 #[test]
176 fn test_deserialization_from_json() {
177 let json = r#"{"field": "data"}"#;
179 let value: serde_json::Value = serde_json::from_str(json).expect("JSON 反序列化失败");
180 assert_eq!(value["field"], "data");
181 }
182}