lago_client/
credentials.rs

1use lago_types::error::LagoError;
2
3/// API credentials for authenticating with the Lago API
4///
5/// This struct contains the API key required to authenticate requests to the Lago API.
6#[derive(Clone)]
7pub struct Credentials {
8    api_key: String,
9}
10
11impl Credentials {
12    /// Creates new credentials with the provided API key
13    ///
14    /// # Arguments
15    /// * `api_key` - The API key for authentication
16    ///
17    /// # Returns
18    /// A new `Credentials` instance
19    pub fn new(api_key: impl Into<String>) -> Self {
20        Self {
21            api_key: api_key.into(),
22        }
23    }
24
25    /// Returns the API key
26    ///
27    /// # Returns
28    /// A reference to the API key string
29    pub fn api_key(&self) -> &str {
30        &self.api_key
31    }
32}
33
34/// Trait for providing credentials to the Lago client
35///
36/// This trait allows for different methods of credential provision,
37/// such as static credentials, environment variables, or other sources.
38pub trait CredentialsProvider: Send + Sync {
39    /// Provides credentials for API authentication
40    ///
41    /// # Returns
42    /// A `Result` containing `Credentials` or an error if credentials cannot be provided
43    fn provider_credentials(&self) -> Result<Credentials, LagoError>;
44}
45
46/// A credentials provider that uses static, pre-configured credentials
47///
48/// This provider holds credentials that were provided at creation time
49/// and returns them on every request.
50#[derive(Clone)]
51pub struct StaticCredentialsProvider {
52    credentials: Credentials,
53}
54
55impl StaticCredentialsProvider {
56    /// Creates a new static credentials provider with the given credentials
57    ///
58    /// # Arguments
59    /// * `credentials` - The credentials to use for all requests
60    ///
61    /// # Returns
62    /// A new `StaticCredentialsProvider` instance
63    pub fn new(credentials: Credentials) -> Self {
64        Self { credentials }
65    }
66}
67
68impl CredentialsProvider for StaticCredentialsProvider {
69    /// Returns the static credentials
70    ///
71    /// # Returns
72    /// A `Result` containing a clone of the stored credentials
73    fn provider_credentials(&self) -> Result<Credentials, LagoError> {
74        Ok(self.credentials.clone())
75    }
76}
77
78/// A credentials provider that loads credentials from environment variables
79///
80/// This provider attempts to load the API key from the `LAGO_API_KEY` environment variable.
81pub struct EnvironmentCredentialsProvider;
82
83impl EnvironmentCredentialsProvider {
84    /// Creates a new environment credentials provider
85    ///
86    /// # Returns
87    /// A new `EnvironmentCredentialsProvider` instance
88    pub fn new() -> Self {
89        Self
90    }
91}
92
93impl Default for EnvironmentCredentialsProvider {
94    /// Creates a default environment credentials provider
95    ///
96    /// This is equivalent to calling `EnvironmentCredentialsProvider::new()`.
97    fn default() -> Self {
98        Self::new()
99    }
100}
101
102impl CredentialsProvider for EnvironmentCredentialsProvider {
103    /// Loads credentials from the `LAGO_API_KEY` environment variable
104    ///
105    /// # Returns
106    /// A `Result` containing `Credentials` loaded from the environment,
107    /// or an error if the environment variable is not set
108    fn provider_credentials(&self) -> Result<Credentials, LagoError> {
109        std::env::var("LAGO_API_KEY")
110            .map(Credentials::new)
111            .map_err(|_| {
112                LagoError::Configuration("LAGO_API_KEY environment variable not found".to_string())
113            })
114    }
115}