faucet_source_xml/
config.rs1use reqwest::header::HeaderMap;
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
9#[serde(tag = "type")]
10pub enum XmlAuth {
11 None,
13 Bearer(String),
15 Basic { username: String, password: String },
17 #[serde(skip)]
19 Custom(HeaderMap),
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
24#[serde(tag = "type")]
25pub enum XmlPagination {
26 PageNumber {
28 param_name: String,
29 start_page: usize,
30 page_size: Option<usize>,
31 page_size_param: Option<String>,
32 },
33 Offset {
35 offset_param: String,
36 limit_param: String,
37 limit: usize,
38 },
39}
40
41#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
43pub struct XmlStreamConfig {
44 pub base_url: String,
46 pub path: String,
48 #[serde(with = "crate::serde_helpers::http_method")]
50 #[schemars(with = "String")]
51 pub method: reqwest::Method,
52 pub auth: XmlAuth,
54 #[serde(skip, default)]
56 pub headers: HeaderMap,
57 pub body: Option<String>,
59 pub records_element_path: Option<String>,
62 pub pagination: Option<XmlPagination>,
64 pub max_pages: Option<usize>,
66 pub query_params: std::collections::HashMap<String, String>,
68}
69
70impl XmlStreamConfig {
71 pub fn new(base_url: impl Into<String>, path: impl Into<String>) -> Self {
73 Self {
74 base_url: base_url.into(),
75 path: path.into(),
76 method: reqwest::Method::GET,
77 auth: XmlAuth::None,
78 headers: HeaderMap::new(),
79 body: None,
80 records_element_path: None,
81 pagination: None,
82 max_pages: None,
83 query_params: std::collections::HashMap::new(),
84 }
85 }
86
87 pub fn method(mut self, method: reqwest::Method) -> Self {
89 self.method = method;
90 self
91 }
92
93 pub fn auth(mut self, auth: XmlAuth) -> Self {
95 self.auth = auth;
96 self
97 }
98
99 pub fn headers(mut self, headers: HeaderMap) -> Self {
101 self.headers = headers;
102 self
103 }
104
105 pub fn body(mut self, body: impl Into<String>) -> Self {
107 self.body = Some(body.into());
108 self
109 }
110
111 pub fn records_element_path(mut self, path: impl Into<String>) -> Self {
113 self.records_element_path = Some(path.into());
114 self
115 }
116
117 pub fn pagination(mut self, pagination: XmlPagination) -> Self {
119 self.pagination = Some(pagination);
120 self
121 }
122
123 pub fn max_pages(mut self, max: usize) -> Self {
125 self.max_pages = Some(max);
126 self
127 }
128
129 pub fn query_param(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
131 self.query_params.insert(key.into(), value.into());
132 self
133 }
134}
135
136#[cfg(test)]
137mod tests {
138 use super::*;
139
140 #[test]
141 fn default_config() {
142 let config = XmlStreamConfig::new("https://api.example.com", "/users");
143 assert_eq!(config.base_url, "https://api.example.com");
144 assert_eq!(config.path, "/users");
145 assert_eq!(config.method, reqwest::Method::GET);
146 assert!(config.records_element_path.is_none());
147 }
148
149 #[test]
150 fn soap_config() {
151 let config = XmlStreamConfig::new("https://api.example.com", "/soap")
152 .method(reqwest::Method::POST)
153 .body("<Envelope><Body><GetUsers/></Body></Envelope>")
154 .records_element_path("Envelope.Body.GetUsersResponse.Users.User");
155 assert_eq!(config.method, reqwest::Method::POST);
156 assert!(config.body.is_some());
157 assert_eq!(
158 config.records_element_path.unwrap(),
159 "Envelope.Body.GetUsersResponse.Users.User"
160 );
161 }
162}