flipt/
lib.rs

1pub mod api;
2pub mod error;
3pub mod evaluation;
4pub mod flags;
5pub mod models;
6pub mod util;
7
8use reqwest::header::HeaderMap;
9use std::time::Duration;
10// reexport
11pub use url::Url;
12
13#[derive(Debug, Clone)]
14pub struct Config<T>
15where
16    T: AuthenticationStrategy,
17{
18    endpoint: Url,
19    timeout: Duration,
20    auth_strategy: Option<T>,
21    headers: Option<HeaderMap>,
22}
23
24#[derive(Debug, Clone)]
25pub struct ConfigBuilder<T>
26where
27    T: AuthenticationStrategy,
28{
29    endpoint: Option<Url>,
30    auth_strategy: Option<T>,
31    timeout: Option<Duration>,
32    headers: Option<HeaderMap>,
33}
34
35impl<T: AuthenticationStrategy> Default for ConfigBuilder<T> {
36    fn default() -> Self {
37        Self {
38            endpoint: Url::parse("http://localhost:8080").ok(),
39            auth_strategy: None,
40            timeout: Some(Duration::from_secs(60)), // Default timeout is 60 seconds
41            headers: None,
42        }
43    }
44}
45
46impl<T: AuthenticationStrategy> ConfigBuilder<T> {
47    pub fn with_endpoint(mut self, endpoint: Url) -> Self {
48        self.endpoint = Some(endpoint);
49        self
50    }
51
52    pub fn with_auth_strategy(mut self, auth_strategy: T) -> Self {
53        self.auth_strategy = Some(auth_strategy);
54        self
55    }
56
57    pub fn with_timeout(mut self, timeout: Duration) -> Self {
58        self.timeout = Some(timeout);
59        self
60    }
61
62    pub fn with_headers(mut self, headers: HeaderMap) -> Self {
63        self.headers = Some(headers);
64        self
65    }
66
67    pub fn build(self) -> Config<T> {
68        Config {
69            endpoint: self.endpoint.unwrap(),
70            auth_strategy: self.auth_strategy,
71            timeout: self.timeout.unwrap(),
72            headers: self.headers,
73        }
74    }
75}
76
77pub trait AuthenticationStrategy {
78    fn authenticate(self) -> HeaderMap;
79}
80
81pub struct NoneAuthentication {}
82
83impl NoneAuthentication {
84    pub fn new() -> Self {
85        Self {}
86    }
87}
88
89impl Default for NoneAuthentication {
90    fn default() -> Self {
91        Self::new()
92    }
93}
94
95impl AuthenticationStrategy for NoneAuthentication {
96    fn authenticate(self) -> HeaderMap {
97        HeaderMap::new()
98    }
99}
100
101pub struct JWTAuthentication {
102    jwt_token: String,
103}
104
105impl JWTAuthentication {
106    pub fn new(jwt_token: String) -> Self {
107        Self { jwt_token }
108    }
109}
110
111impl AuthenticationStrategy for JWTAuthentication {
112    fn authenticate(self) -> HeaderMap {
113        let mut header_map = HeaderMap::new();
114
115        header_map.insert(
116            "Authorization",
117            format!("JWT {}", self.jwt_token).parse().unwrap(),
118        );
119
120        header_map
121    }
122}
123
124pub struct ClientTokenAuthentication {
125    client_token: String,
126}
127
128impl ClientTokenAuthentication {
129    pub fn new(client_token: String) -> Self {
130        Self { client_token }
131    }
132}
133
134impl AuthenticationStrategy for ClientTokenAuthentication {
135    fn authenticate(self) -> HeaderMap {
136        let mut header_map = HeaderMap::new();
137
138        header_map.insert(
139            "Authorization",
140            format!("Bearer {}", self.client_token).parse().unwrap(),
141        );
142
143        header_map
144    }
145}