crankshaft_config/backend/tes/
http.rs

1//! Configuration related to HTTP within the TES execution backend.
2
3use base64::Engine;
4use base64::engine::general_purpose::STANDARD;
5use serde::Deserialize;
6use serde::Serialize;
7
8/// Represents HTTP authentication configuration.
9#[derive(Clone, Debug, Deserialize, Serialize)]
10#[serde(rename_all = "kebab-case", tag = "type")]
11pub enum HttpAuthConfig {
12    /// Use basic authentication.
13    Basic {
14        /// The username for the authentication.
15        username: String,
16        /// The password for the authentication.
17        password: String,
18    },
19    /// Use bearer token authentication.
20    Bearer {
21        /// The bearer token for authentication.
22        token: String,
23    },
24}
25
26impl HttpAuthConfig {
27    /// Gets the `Authorization` header value based on the config.
28    pub fn header_value(&self) -> String {
29        match self {
30            Self::Basic { username, password } => format!(
31                "Basic {encoded}",
32                encoded = STANDARD.encode(format!("{username}:{password}"))
33            ),
34            Self::Bearer { token } => format!("Bearer {token}"),
35        }
36    }
37}
38
39/// A configuration object for HTTP settings within the TES execution backend.
40// **NOTE:** all default values for this struct need to be tested below to
41// ensure the defaults never change.
42#[derive(Clone, Debug, Default, Deserialize, Serialize)]
43#[serde(rename_all = "kebab-case")]
44pub struct Config {
45    /// The HTTP authentication to use.
46    pub auth: Option<HttpAuthConfig>,
47
48    /// The number of retries for each request.
49    pub retries: Option<u32>,
50
51    /// The maximum number of concurrent requests.
52    pub max_concurrency: Option<usize>,
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn defaults() {
61        let options = Config::default();
62        assert!(options.auth.is_none());
63        assert!(options.retries.is_none());
64        assert!(options.max_concurrency.is_none());
65    }
66}