smarty_rust_sdk/sdk/
options.rs1use std::{collections::HashMap};
2use reqwest::Proxy;
3use url::Url;
4
5use crate::sdk::authentication::Authenticate;
6
7pub struct OptionsBuilder {
19 license: String,
20 num_retries: u64,
21 logging_enabled: bool,
22 headers: Vec<(String, String)>,
23 authentication: Option<Box<dyn Authenticate>>,
24 custom_queries: Option<HashMap<String, String>>,
25
26 url: Option<Url>,
27
28 proxy: Option<Proxy>,
29}
30
31#[allow(clippy::new_without_default)]
33impl OptionsBuilder {
34 pub fn new(authentication: Option<Box<dyn Authenticate>>) -> Self {
36 Self {
37 license: "".to_string(),
38 num_retries: 10,
39 logging_enabled: false,
40 headers: vec![],
41 authentication,
42 custom_queries: None,
43
44 url: None,
45
46 proxy: None,
47 }
48 }
49
50 pub fn build(self) -> Options {
53 Options {
54 license: self.license,
55 num_retries: self.num_retries,
56 logging_enabled: self.logging_enabled,
57 headers: self.headers,
58 authentication: self.authentication,
59 custom_queries: self.custom_queries,
60
61 url: self.url,
62
63 proxy: self.proxy,
64 }
65 }
66
67 pub fn with_license(mut self, license: &str) -> Self {
69 self.license = license.to_string();
70 self
71 }
72
73 pub fn with_retries(mut self, num_retries: u64) -> Self {
75 self.num_retries = num_retries;
76 self
77 }
78
79 pub fn with_logging(mut self) -> Self {
81 self.logging_enabled = true;
82 self
83 }
84
85 pub fn with_headers(mut self, headers: Vec<(String, String)>) -> Self {
87 self.headers = headers;
88 self
89 }
90
91 pub fn with_url(mut self, url: Url) -> Self {
93 self.url = Some(url);
94 self
95 }
96
97 pub fn with_proxy(mut self, proxy: Proxy) -> Self {
99 self.proxy = Some(proxy);
100 self
101 }
102
103 pub fn with_custom_query(mut self, key: &str, value: &str) -> Self {
105 self.custom_queries.get_or_insert_with(HashMap::new).insert(key.to_string(), value.to_string());
106 self
107 }
108
109 pub fn with_custom_comma_separated_query(mut self, key: &str, value: &str) -> Self {
111 self.custom_queries
112 .get_or_insert_with(HashMap::new)
113 .entry(key.to_string())
114 .and_modify(|existing| {
115 if !existing.is_empty() {
116 existing.push(',');
117 }
118 existing.push_str(value);
119 })
120 .or_insert_with(|| value.to_string());
121 self
122 }
123
124 pub fn with_component_analysis(self) -> Self {
126 self.with_custom_comma_separated_query("features", "component-analysis")
127 }
128}
129
130pub struct Options {
136 pub(crate) license: String,
137
138 pub(crate) num_retries: u64,
140
141 pub(crate) logging_enabled: bool,
143
144 pub(crate) headers: Vec<(String, String)>,
146
147 pub(crate) authentication: Option<Box<dyn Authenticate>>,
149
150 pub(crate) custom_queries: Option<HashMap<String, String>>,
152
153 pub(crate) url: Option<Url>,
155
156 pub(crate) proxy: Option<Proxy>,
158}
159
160impl Clone for Options {
161 fn clone(&self) -> Self {
162 Self {
163 license: self.license.clone(),
164 num_retries: self.num_retries,
165 logging_enabled: self.logging_enabled,
166 headers: self.headers.clone(),
167 authentication: self.authentication.as_ref().map(|x| x.clone_box()),
168 custom_queries: self.custom_queries.clone(),
169 url: self.url.clone(),
170 proxy: self.proxy.clone(),
171 }
172 }
173}