bitwarden_core/client/client.rs
1use std::sync::Arc;
2
3use super::{gov_mode, internal::InternalClient};
4use crate::{
5 auth::auth_tokens::TokenHandler,
6 client::{
7 builder::ClientBuilder, client_settings::ClientSettings,
8 tracing_middleware::ReqwestTracingMiddleware,
9 },
10};
11
12/// The main struct to interact with the Bitwarden SDK.
13#[derive(Clone)]
14pub struct Client {
15 // Important: The [`Client`] struct requires its `Clone` implementation to return an owned
16 // reference to the same instance. This is required to properly use the FFI API, where we can't
17 // just use normal Rust references effectively. For this to happen, any mutable state needs
18 // to be behind an Arc, ideally as part of the existing [`InternalClient`] struct.
19 #[doc(hidden)]
20 pub internal: Arc<InternalClient>,
21}
22
23impl Client {
24 /// Create a new Bitwarden client with default settings and a no-op token handler.
25 pub fn new(settings: Option<ClientSettings>) -> Self {
26 let mut builder = ClientBuilder::new();
27 if let Some(s) = settings {
28 builder = builder.with_settings(s);
29 }
30 builder = builder.with_middleware(vec![Arc::new(ReqwestTracingMiddleware)]);
31 builder.build()
32 }
33
34 /// Create a new Bitwarden client with the specified token handler for managing authentication
35 /// tokens.
36 pub fn new_with_token_handler(
37 settings: Option<ClientSettings>,
38 token_handler: Arc<dyn TokenHandler>,
39 ) -> Self {
40 let mut builder = ClientBuilder::new().with_token_handler(token_handler);
41 if let Some(s) = settings {
42 builder = builder.with_settings(s);
43 }
44 builder = builder.with_middleware(vec![Arc::new(ReqwestTracingMiddleware)]);
45 builder.build()
46 }
47
48 /// Returns a [`ClientBuilder`] for constructing a new [`Client`].
49 pub fn builder() -> ClientBuilder {
50 ClientBuilder::new()
51 }
52
53 /// Whether the client is in Gov Mode.
54 ///
55 /// Inferred today from the configured API URL host. PM-36520 will
56 /// replace the URL inference with a read of the (not yet developed)
57 /// server-published gov_mode from /api/config. The signature does not
58 /// change across that migration.
59 pub fn gov_mode(&self) -> bool {
60 gov_mode::is_gov_mode(&self.internal)
61 }
62}