smarty_rust_sdk/sdk/
options.rs

1use reqwest::Proxy;
2use url::Url;
3
4use crate::sdk::authentication::Authenticate;
5
6/// A builder for the options
7///
8/// Example:
9/// ```ignore
10/// let authentication = SecretKeyCredential::new("test".to_string(), "test".to_string());
11///
12/// OptionsBuilder::new(authentication)
13///     .with_license("test_license")
14///     .with_logging()
15///     .build()
16/// ```
17pub struct OptionsBuilder {
18    license: String,
19    num_retries: u64,
20    logging_enabled: bool,
21    headers: Vec<(String, String)>,
22    authentication: Option<Box<dyn Authenticate>>,
23
24    url: Option<Url>,
25
26    proxy: Option<Proxy>,
27}
28
29// Allowing this because it is a builder pattern
30#[allow(clippy::new_without_default)]
31impl OptionsBuilder {
32    /// Creates a new OptionsBuilder, taking in the authentication for the options.
33    pub fn new(authentication: Option<Box<dyn Authenticate>>) -> Self {
34        Self {
35            license: "".to_string(),
36            num_retries: 10,
37            logging_enabled: false,
38            headers: vec![],
39            authentication,
40
41            url: None,
42
43            proxy: None,
44        }
45    }
46
47    /// Builds the builder into options with the parameters you set
48    /// Returns an error if authentication is not set
49    pub fn build(self) -> Options {
50        Options {
51            license: self.license,
52            num_retries: self.num_retries,
53            logging_enabled: self.logging_enabled,
54            headers: self.headers,
55            authentication: self.authentication,
56
57            url: self.url,
58
59            proxy: self.proxy,
60        }
61    }
62
63    /// Adds a license string to the options
64    pub fn with_license(mut self, license: &str) -> Self {
65        self.license = license.to_string();
66        self
67    }
68
69    /// Forces a maximum number of retries that a request will attempt to handle.
70    pub fn with_retries(mut self, num_retries: u64) -> Self {
71        self.num_retries = num_retries;
72        self
73    }
74
75    /// Enables Logging
76    pub fn with_logging(mut self) -> Self {
77        self.logging_enabled = true;
78        self
79    }
80
81    /// Adds a set of custom headers to your request.
82    pub fn with_headers(mut self, headers: Vec<(String, String)>) -> Self {
83        self.headers = headers;
84        self
85    }
86
87    /// Sets the base url that the request should use.
88    pub fn with_url(mut self, url: Url) -> Self {
89        self.url = Some(url);
90        self
91    }
92
93    /// Adds a custom proxy for the request to point to.
94    pub fn with_proxy(mut self, proxy: Proxy) -> Self {
95        self.proxy = Some(proxy);
96        self
97    }
98}
99
100/// Options that can be passed into a new client
101/// <num_retries>: the number of retries that the client with run before giving up.
102/// <logging_enabled>: whether we should send logging data
103/// <headers>: Custom headers that you can pass in
104/// <authentication>: A authentication for Smarty
105pub struct Options {
106    pub(crate) license: String,
107
108    // Retry Sender
109    pub(crate) num_retries: u64,
110
111    // Logger
112    pub(crate) logging_enabled: bool,
113
114    // Custom Headers
115    pub(crate) headers: Vec<(String, String)>,
116
117    // Authentication
118    pub(crate) authentication: Option<Box<dyn Authenticate>>,
119
120    // Url
121    pub(crate) url: Option<Url>,
122
123    // Proxy
124    pub(crate) proxy: Option<Proxy>,
125}
126
127impl Clone for Options {
128    fn clone(&self) -> Self {
129        Self {
130            license: self.license.clone(),
131            num_retries: self.num_retries,
132            logging_enabled: self.logging_enabled,
133            headers: self.headers.clone(),
134            authentication: self.authentication.as_ref().map(|x| x.clone_box()),
135            url: self.url.clone(),
136            proxy: self.proxy.clone(),
137        }
138    }
139}