Skip to content

Commit b42b757

Browse files
authored
Merge pull request #20 from TheEmeraldBee/master
Added Proxy to Options
2 parents e06c10a + 74418c0 commit b42b757

File tree

12 files changed

+46
-16
lines changed

12 files changed

+46
-16
lines changed

smarty-rust-proc-macro/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(clippy::manual_unwrap_or_default)]
2+
13
extern crate darling;
24
extern crate proc_macro;
35

@@ -106,12 +108,12 @@ fn impl_smarty_api_macro(attrs: &MacroArgs, ast: &mut syn::DeriveInput) -> Token
106108

107109
impl #name {
108110
/// Creates a new client with the given options
109-
pub fn new(options: Options) -> Result<Self, ParseError> {
111+
pub fn new(options: Options) -> Result<Self, SmartyError> {
110112
Self::new_custom_base_url(#default_url.parse()?, options)
111113
}
112114

113115
/// Creates a new client with the given options that points to a different url.
114-
pub fn new_custom_base_url(base_url: Url, options: Options) -> Result<Self, ParseError> {
116+
pub fn new_custom_base_url(base_url: Url, options: Options) -> Result<Self, SmartyError> {
115117
Ok(Self {client: Client::new(base_url, options, #api_path)?})
116118
}
117119
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::sdk::options::Options;
66
use crate::sdk::send_request;
77
use reqwest::Method;
88
use smarty_rust_proc_macro::smarty_api;
9-
use url::{ParseError, Url};
9+
use url::Url;
1010

1111
#[smarty_api(
1212
api_path = "v2/lookup/",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::sdk::options::Options;
66
use crate::sdk::send_request;
77
use reqwest::Method;
88
use smarty_rust_proc_macro::smarty_api;
9-
use url::{ParseError, Url};
9+
use url::Url;
1010

1111
#[smarty_api(
1212
api_path = "verify",

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use crate::sdk::options::Options;
33
use crate::sdk::VERSION;
44
use reqwest::header::USER_AGENT;
55
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware, RequestBuilder};
6-
use url::{ParseError, Url};
6+
use url::Url;
77

8-
use super::retry_strategy::SmartyRetryMiddleware;
8+
use super::{error::SmartyError, retry_strategy::SmartyRetryMiddleware};
99

1010
/// The base client for all of Smarty's rust sdk
1111
pub(crate) struct Client {
@@ -19,11 +19,21 @@ impl Client {
1919
base_url: Url,
2020
options: Options,
2121
api_path: &str,
22-
) -> Result<Client, ParseError> {
22+
) -> Result<Client, SmartyError> {
2323
let url = &mut base_url.join(api_path)?;
2424

25-
let mut client_builder = ClientBuilder::new(reqwest::Client::new())
26-
.with(SmartyRetryMiddleware::new(options.num_retries));
25+
let mut reqwest_client_builder = reqwest::ClientBuilder::new();
26+
27+
if let Some(proxy) = options.proxy.clone() {
28+
reqwest_client_builder = reqwest_client_builder.proxy(proxy);
29+
}
30+
31+
let mut client_builder = ClientBuilder::new(
32+
reqwest_client_builder
33+
.build()
34+
.map_err(SmartyError::RequestProcess)?,
35+
)
36+
.with(SmartyRetryMiddleware::new(options.num_retries));
2737

2838
if options.logging_enabled {
2939
client_builder = client_builder.with(LoggingMiddleware);

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use reqwest::Proxy;
2+
13
use crate::sdk::authentication::Authenticate;
24

35
/// A builder for the options
@@ -17,6 +19,8 @@ pub struct OptionsBuilder {
1719
logging_enabled: bool,
1820
headers: Vec<(String, String)>,
1921
authentication: Option<Box<dyn Authenticate>>,
22+
23+
proxy: Option<Proxy>,
2024
}
2125

2226
// Allowing this because it is a builder pattern
@@ -30,6 +34,8 @@ impl OptionsBuilder {
3034
logging_enabled: false,
3135
headers: vec![],
3236
authentication,
37+
38+
proxy: None,
3339
}
3440
}
3541

@@ -42,6 +48,8 @@ impl OptionsBuilder {
4248
logging_enabled: self.logging_enabled,
4349
headers: self.headers,
4450
authentication: self.authentication,
51+
52+
proxy: self.proxy,
4553
}
4654
}
4755

@@ -68,6 +76,12 @@ impl OptionsBuilder {
6876
self.headers = headers;
6977
self
7078
}
79+
80+
/// Adds a custom proxy for the request to point to.
81+
pub fn with_proxy(mut self, proxy: Proxy) -> Self {
82+
self.proxy = Some(proxy);
83+
self
84+
}
7185
}
7286

7387
/// Options that can be passed into a new client
@@ -89,6 +103,9 @@ pub struct Options {
89103

90104
// Authentication
91105
pub(crate) authentication: Option<Box<dyn Authenticate>>,
106+
107+
// Proxy
108+
pub(crate) proxy: Option<Proxy>,
92109
}
93110

94111
impl Clone for Options {
@@ -99,6 +116,7 @@ impl Clone for Options {
99116
logging_enabled: self.logging_enabled,
100117
headers: self.headers.clone(),
101118
authentication: self.authentication.as_ref().map(|x| x.clone_box()),
119+
proxy: self.proxy.clone(),
102120
}
103121
}
104122
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::us_autocomplete_api::lookup::Lookup;
66
use crate::us_autocomplete_api::suggestion::SuggestionListing;
77
use reqwest::Method;
88
use smarty_rust_proc_macro::smarty_api;
9-
use url::{ParseError, Url};
9+
use url::Url;
1010

1111
#[smarty_api(
1212
api_path = "suggest",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::us_autocomplete_pro_api::lookup::Lookup;
66
use crate::us_autocomplete_pro_api::suggestion::SuggestionListing;
77
use reqwest::Method;
88
use smarty_rust_proc_macro::smarty_api;
9-
use url::{ParseError, Url};
9+
use url::Url;
1010

1111
#[smarty_api(
1212
api_path = "lookup",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::us_enrichment_api::results::EnrichmentResponse;
77
use reqwest::Method;
88
use serde::de::DeserializeOwned;
99
use smarty_rust_proc_macro::smarty_api;
10-
use url::{ParseError, Url};
10+
use url::Url;
1111

1212
#[smarty_api(
1313
api_path = "lookup",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::sdk::send_request;
55
use crate::us_extract_api::lookup::Lookup;
66
use reqwest::Method;
77
use smarty_rust_proc_macro::smarty_api;
8-
use url::{ParseError, Url};
8+
use url::Url;
99

1010
#[smarty_api(
1111
api_path = "",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::us_reverse_geo_api::address::Results;
66
use crate::us_reverse_geo_api::lookup::Lookup;
77
use reqwest::Method;
88
use smarty_rust_proc_macro::smarty_api;
9-
use url::{ParseError, Url};
9+
use url::Url;
1010

1111
#[smarty_api(
1212
api_path = "lookup",

0 commit comments

Comments
 (0)