Skip to content

Commit 3806d2c

Browse files
authored
Merge pull request #35 from smarty/carter/custom-query
Add custom query option.
2 parents 290d060 + bdfb47d commit 3806d2c

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

smarty-rust-sdk/src/sdk/client.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ impl Client {
6868
format!("smarty (sdk:rust@{})", VERSION),
6969
);
7070

71+
if let Some(queries) = self.options.custom_queries.clone() {
72+
for (key, value) in queries {
73+
builder = builder.query(&[(key, value)]);
74+
}
75+
}
76+
7177
builder
7278
}
7379
}

smarty-rust-sdk/src/sdk/options.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::{collections::HashMap};
12
use reqwest::Proxy;
23
use url::Url;
34

@@ -20,6 +21,7 @@ pub struct OptionsBuilder {
2021
logging_enabled: bool,
2122
headers: Vec<(String, String)>,
2223
authentication: Option<Box<dyn Authenticate>>,
24+
custom_queries: Option<HashMap<String, String>>,
2325

2426
url: Option<Url>,
2527

@@ -37,6 +39,7 @@ impl OptionsBuilder {
3739
logging_enabled: false,
3840
headers: vec![],
3941
authentication,
42+
custom_queries: None,
4043

4144
url: None,
4245

@@ -53,6 +56,7 @@ impl OptionsBuilder {
5356
logging_enabled: self.logging_enabled,
5457
headers: self.headers,
5558
authentication: self.authentication,
59+
custom_queries: self.custom_queries,
5660

5761
url: self.url,
5862

@@ -95,6 +99,32 @@ impl OptionsBuilder {
9599
self.proxy = Some(proxy);
96100
self
97101
}
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+
}
98128
}
99129

100130
/// Options that can be passed into a new client
@@ -117,6 +147,9 @@ pub struct Options {
117147
// Authentication
118148
pub(crate) authentication: Option<Box<dyn Authenticate>>,
119149

150+
// Custom Queries
151+
pub(crate) custom_queries: Option<HashMap<String, String>>,
152+
120153
// Url
121154
pub(crate) url: Option<Url>,
122155

@@ -132,6 +165,7 @@ impl Clone for Options {
132165
logging_enabled: self.logging_enabled,
133166
headers: self.headers.clone(),
134167
authentication: self.authentication.as_ref().map(|x| x.clone_box()),
168+
custom_queries: self.custom_queries.clone(),
135169
url: self.url.clone(),
136170
proxy: self.proxy.clone(),
137171
}

0 commit comments

Comments
 (0)