openlark_client/
client.rs1#[macro_use]
6mod macros;
7
8mod builder;
9#[cfg(test)]
10mod catalog_wiring_tests;
11#[cfg(test)]
12mod tests;
13
14pub use builder::ClientBuilder;
15
16use crate::{
17 Result,
18 error::{with_context, with_operation_context},
19 validation_error,
20};
21
22#[cfg(feature = "auth")]
24#[derive(Debug, Clone)]
25pub struct AuthClient {
26 pub app: openlark_auth::AuthService,
28 pub user: openlark_auth::AuthenService,
30 pub oauth: openlark_auth::OAuthService,
32}
33
34#[cfg(feature = "auth")]
35impl AuthClient {
36 fn new(config: openlark_core::config::Config) -> Self {
37 Self {
38 app: openlark_auth::AuthService::new(config.clone()),
39 user: openlark_auth::AuthenService::new(config.clone()),
40 oauth: openlark_auth::OAuthService::new(config),
41 }
42 }
43}
44
45macro_rules! append_catalog_entries {
50 ($({
51 feature: $c_feature:literal,
52 field: $c_field:ident,
53 ty: $c_ty:ty,
54 doc: $c_doc:literal,
55 init: |$c_core:ident, $c_base:ident| $c_init:block $(,)?
56 }),* $(,)?) => {
57 declare_client! {
58 $(
59 {
60 feature: $c_feature,
61 field: $c_field,
62 ty: $c_ty,
63 doc: $c_doc,
64 init: |$c_core, $c_base| $c_init,
65 },
66 )*
67 }
68 };
69}
70
71crate::capability::for_each_compiled_capability!(append_catalog_entries);
72
73impl Client {
74 pub fn from_env() -> Result<Self> {
94 Self::builder().from_env().build()
95 }
96
97 pub fn builder() -> ClientBuilder {
99 ClientBuilder::new()
100 }
101
102 pub fn config(&self) -> &openlark_core::config::Config {
104 &self.config
105 }
106
107 pub fn core_config(&self) -> &openlark_core::config::Config {
111 &self.config
112 }
113
114 pub fn api_config(&self) -> &openlark_core::config::Config {
120 &self.config
121 }
122
123 pub fn is_configured(&self) -> bool {
125 !self.config.app_id().is_empty() && !self.config.app_secret().is_empty()
126 }
127
128 pub fn with_core_config(config: openlark_core::config::Config) -> Result<Self> {
133 Self::with_checked_core_config(config, "Client::with_core_config")
134 }
135
136 pub(crate) fn with_checked_core_config(
145 base_core_config: openlark_core::config::Config,
146 operation: &str,
147 ) -> Result<Self> {
148 if let Err(err) = base_core_config.validate() {
149 return with_context(Err(err), "operation", operation);
150 }
151 if base_core_config
152 .req_timeout()
153 .is_some_and(|timeout| timeout.is_zero())
154 {
155 return with_context(
156 Err(validation_error("timeout", "timeout必须大于0")),
157 "operation",
158 operation,
159 );
160 }
161
162 #[cfg(feature = "auth")]
163 let core_config = {
164 use openlark_auth::AuthTokenProvider;
165 let provider = AuthTokenProvider::new(base_core_config.clone());
166 base_core_config.with_token_provider(provider)
167 };
168 #[cfg(not(feature = "auth"))]
169 let core_config = base_core_config.clone();
170
171 Self::from_parts(base_core_config, core_config)
172 }
173
174 pub async fn execute_with_context<F, T>(&self, operation: &str, f: F) -> Result<T>
176 where
177 F: std::future::Future<Output = Result<T>>,
178 {
179 let result = f.await;
180 with_operation_context(result, operation, "Client")
181 }
182}