Skip to main content

gen_linkedin/
auth.rs

1use crate::Error;
2
3/// Provides bearer tokens for authenticating API requests.
4pub trait TokenProvider: Send + Sync {
5    /// Returns a bearer token string.
6    fn bearer_token(&self) -> Result<String, Error>;
7}
8
9/// A token provider backed by a fixed string.
10pub struct StaticTokenProvider(pub String);
11impl TokenProvider for StaticTokenProvider {
12    fn bearer_token(&self) -> Result<String, Error> {
13        Ok(self.0.clone())
14    }
15}
16
17/// Reads the token from an environment variable.
18pub struct EnvTokenProvider {
19    /// The environment variable name that stores the token.
20    pub var: String,
21}
22impl TokenProvider for EnvTokenProvider {
23    fn bearer_token(&self) -> Result<String, Error> {
24        std::env::var(&self.var).map_err(|_| {
25            Error::Config(format!(
26                "missing env var {} for linkedin access token",
27                self.var
28            ))
29        })
30    }
31}