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::{
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}