smarty_rust_sdk/sdk/
options.rs

1use std::{collections::HashMap};
2use reqwest::Proxy;
3use url::Url;
4
5use crate::sdk::authentication::Authenticate;
6
7/// A builder for the options
8///
9/// Example:
10/// ```ignore
11/// let authentication = SecretKeyCredential::new("test".to_string(), "test".to_string());
12///
13/// OptionsBuilder::new(authentication)
14///     .with_license("test_license")
15///     .with_logging()
16///     .build()
17/// ```
18pub 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// Allowing this because it is a builder pattern
32#[allow(clippy::new_without_default)]
33impl OptionsBuilder {
34    /// Creates a new OptionsBuilder, taking in the authentication for the options.
35    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    /// Builds the builder into options with the parameters you set
51    /// Returns an error if authentication is not set
52    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    /// Adds a license string to the options
68    pub fn with_license(mut self, license: &str) -> Self {
69        self.license = license.to_string();
70        self
71    }
72
73    /// Forces a maximum number of retries that a request will attempt to handle.
74    pub fn with_retries(mut self, num_retries: u64) -> Self {
75        self.num_retries = num_retries;
76        self
77    }
78
79    /// Enables Logging
80    pub fn with_logging(mut self) -> Self {
81        self.logging_enabled = true;
82        self
83    }
84
85    /// Adds a set of custom headers to your request.
86    pub fn with_headers(mut self, headers: Vec<(String, String)>) -> Self {
87        self.headers = headers;
88        self
89    }
90
91    /// Sets the base url that the request should use.
92    pub fn with_url(mut self, url: Url) -> Self {
93        self.url = Some(url);
94        self
95    }
96
97    /// Adds a custom proxy for the request to point to.
98    pub fn with_proxy(mut self, proxy: Proxy) -> Self {
99        self.proxy = Some(proxy);
100        self
101    }
102
103    /// Adds a custom query to the request.
104    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    /// Appends a custom set of values to the existing query parameter.
110    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    /// Adds component analysis feature to the request.
125    pub fn with_component_analysis(self) -> Self {
126        self.with_custom_comma_separated_query("features", "component-analysis")
127    }
128}
129
130/// Options that can be passed into a new client
131/// <num_retries>: the number of retries that the client with run before giving up.
132/// <logging_enabled>: whether we should send logging data
133/// <headers>: Custom headers that you can pass in
134/// <authentication>: A authentication for Smarty
135pub struct Options {
136    pub(crate) license: String,
137
138    // Retry Sender
139    pub(crate) num_retries: u64,
140
141    // Logger
142    pub(crate) logging_enabled: bool,
143
144    // Custom Headers
145    pub(crate) headers: Vec<(String, String)>,
146
147    // Authentication
148    pub(crate) authentication: Option<Box<dyn Authenticate>>,
149
150    // Custom Queries
151    pub(crate) custom_queries: Option<HashMap<String, String>>,
152
153    // Url
154    pub(crate) url: Option<Url>,
155
156    // Proxy
157    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}