Skip to main content

faucet_stream/
config.rs

1//! Stream configuration and builder.
2
3use crate::auth::Auth;
4use crate::pagination::PaginationStyle;
5use reqwest::{
6    Method,
7    header::{HeaderMap, HeaderName, HeaderValue},
8};
9use serde_json::Value;
10use std::collections::HashMap;
11use std::time::Duration;
12
13/// Configuration for a RestStream.
14#[derive(Debug, Clone)]
15pub struct RestStreamConfig {
16    pub base_url: String,
17    pub path: String,
18    pub method: Method,
19    pub auth: Auth,
20    pub headers: HeaderMap,
21    pub query_params: HashMap<String, String>,
22    pub body: Option<Value>,
23    pub pagination: PaginationStyle,
24    pub records_path: Option<String>,
25    pub max_pages: Option<usize>,
26    pub request_delay: Option<Duration>,
27    pub timeout: Option<Duration>,
28    pub max_retries: u32,
29    pub retry_backoff: Duration,
30}
31
32impl Default for RestStreamConfig {
33    fn default() -> Self {
34        Self {
35            base_url: String::new(),
36            path: String::new(),
37            method: Method::GET,
38            auth: Auth::None,
39            headers: HeaderMap::new(),
40            query_params: HashMap::new(),
41            body: None,
42            pagination: PaginationStyle::None,
43            records_path: None,
44            max_pages: Some(100),
45            request_delay: None,
46            timeout: Some(Duration::from_secs(30)),
47            max_retries: 3,
48            retry_backoff: Duration::from_secs(1),
49        }
50    }
51}
52
53impl RestStreamConfig {
54    pub fn new(base_url: &str, path: &str) -> Self {
55        Self {
56            base_url: base_url.trim_end_matches('/').to_string(),
57            path: path.to_string(),
58            ..Default::default()
59        }
60    }
61
62    pub fn method(mut self, m: Method) -> Self {
63        self.method = m;
64        self
65    }
66    pub fn auth(mut self, a: Auth) -> Self {
67        self.auth = a;
68        self
69    }
70
71    pub fn header(mut self, k: &str, v: &str) -> Self {
72        self.headers.insert(
73            HeaderName::from_bytes(k.as_bytes()).expect("invalid header name"),
74            HeaderValue::from_str(v).expect("invalid header value"),
75        );
76        self
77    }
78
79    pub fn query(mut self, k: &str, v: &str) -> Self {
80        self.query_params.insert(k.into(), v.into());
81        self
82    }
83
84    pub fn body(mut self, b: Value) -> Self {
85        self.body = Some(b);
86        self
87    }
88    pub fn pagination(mut self, p: PaginationStyle) -> Self {
89        self.pagination = p;
90        self
91    }
92    pub fn records_path(mut self, p: &str) -> Self {
93        self.records_path = Some(p.into());
94        self
95    }
96    pub fn max_pages(mut self, n: usize) -> Self {
97        self.max_pages = Some(n);
98        self
99    }
100    pub fn request_delay(mut self, d: Duration) -> Self {
101        self.request_delay = Some(d);
102        self
103    }
104    pub fn timeout(mut self, d: Duration) -> Self {
105        self.timeout = Some(d);
106        self
107    }
108    pub fn max_retries(mut self, n: u32) -> Self {
109        self.max_retries = n;
110        self
111    }
112
113    pub fn retry_backoff(mut self, d: Duration) -> Self {
114        self.retry_backoff = d;
115        self
116    }
117}