Skip to main content

bitwarden_core/client/
client.rs

1use std::sync::Arc;
2
3use super::internal::InternalClient;
4use crate::{
5    auth::auth_tokens::TokenHandler,
6    client::{builder::ClientBuilder, client_settings::ClientSettings},
7};
8
9/// The main struct to interact with the Bitwarden SDK.
10#[derive(Clone)]
11pub struct Client {
12    // Important: The [`Client`] struct requires its `Clone` implementation to return an owned
13    // reference to the same instance. This is required to properly use the FFI API, where we can't
14    // just use normal Rust references effectively. For this to happen, any mutable state needs
15    // to be behind an Arc, ideally as part of the existing [`InternalClient`] struct.
16    #[doc(hidden)]
17    pub internal: Arc<InternalClient>,
18}
19
20impl Client {
21    /// Create a new Bitwarden client with default settings and a no-op token handler.
22    pub fn new(settings: Option<ClientSettings>) -> Self {
23        let mut builder = ClientBuilder::new();
24        if let Some(s) = settings {
25            builder = builder.with_settings(s);
26        }
27        builder.build()
28    }
29
30    /// Create a new Bitwarden client with the specified token handler for managing authentication
31    /// tokens.
32    pub fn new_with_token_handler(
33        settings: Option<ClientSettings>,
34        token_handler: Arc<dyn TokenHandler>,
35    ) -> Self {
36        let mut builder = ClientBuilder::new().with_token_handler(token_handler);
37        if let Some(s) = settings {
38            builder = builder.with_settings(s);
39        }
40        builder.build()
41    }
42
43    /// Returns a [`ClientBuilder`] for constructing a new [`Client`].
44    pub fn builder() -> ClientBuilder {
45        ClientBuilder::new()
46    }
47}