diff --git a/smarty-rust-proc-macro/src/lib.rs b/smarty-rust-proc-macro/src/lib.rs index 1660464..111ddd0 100644 --- a/smarty-rust-proc-macro/src/lib.rs +++ b/smarty-rust-proc-macro/src/lib.rs @@ -1,3 +1,5 @@ +#![allow(clippy::manual_unwrap_or_default)] + extern crate darling; extern crate proc_macro; @@ -106,12 +108,12 @@ fn impl_smarty_api_macro(attrs: &MacroArgs, ast: &mut syn::DeriveInput) -> Token impl #name { /// Creates a new client with the given options - pub fn new(options: Options) -> Result { + pub fn new(options: Options) -> Result { Self::new_custom_base_url(#default_url.parse()?, options) } /// Creates a new client with the given options that points to a different url. - pub fn new_custom_base_url(base_url: Url, options: Options) -> Result { + pub fn new_custom_base_url(base_url: Url, options: Options) -> Result { Ok(Self {client: Client::new(base_url, options, #api_path)?}) } } diff --git a/smarty-rust-sdk/src/international_autocomplete_api/client.rs b/smarty-rust-sdk/src/international_autocomplete_api/client.rs index 64cc082..4503d3e 100644 --- a/smarty-rust-sdk/src/international_autocomplete_api/client.rs +++ b/smarty-rust-sdk/src/international_autocomplete_api/client.rs @@ -6,7 +6,7 @@ use crate::sdk::options::Options; use crate::sdk::send_request; use reqwest::Method; use smarty_rust_proc_macro::smarty_api; -use url::{ParseError, Url}; +use url::Url; #[smarty_api( api_path = "v2/lookup/", diff --git a/smarty-rust-sdk/src/international_street_api/client.rs b/smarty-rust-sdk/src/international_street_api/client.rs index 42f2e42..5af974e 100644 --- a/smarty-rust-sdk/src/international_street_api/client.rs +++ b/smarty-rust-sdk/src/international_street_api/client.rs @@ -6,7 +6,7 @@ use crate::sdk::options::Options; use crate::sdk::send_request; use reqwest::Method; use smarty_rust_proc_macro::smarty_api; -use url::{ParseError, Url}; +use url::Url; #[smarty_api( api_path = "verify", diff --git a/smarty-rust-sdk/src/sdk/client.rs b/smarty-rust-sdk/src/sdk/client.rs index 40a81e8..7c565e2 100644 --- a/smarty-rust-sdk/src/sdk/client.rs +++ b/smarty-rust-sdk/src/sdk/client.rs @@ -3,9 +3,9 @@ use crate::sdk::options::Options; use crate::sdk::VERSION; use reqwest::header::USER_AGENT; use reqwest_middleware::{ClientBuilder, ClientWithMiddleware, RequestBuilder}; -use url::{ParseError, Url}; +use url::Url; -use super::retry_strategy::SmartyRetryMiddleware; +use super::{error::SmartyError, retry_strategy::SmartyRetryMiddleware}; /// The base client for all of Smarty's rust sdk pub(crate) struct Client { @@ -19,11 +19,21 @@ impl Client { base_url: Url, options: Options, api_path: &str, - ) -> Result { + ) -> Result { let url = &mut base_url.join(api_path)?; - let mut client_builder = ClientBuilder::new(reqwest::Client::new()) - .with(SmartyRetryMiddleware::new(options.num_retries)); + let mut reqwest_client_builder = reqwest::ClientBuilder::new(); + + if let Some(proxy) = options.proxy.clone() { + reqwest_client_builder = reqwest_client_builder.proxy(proxy); + } + + let mut client_builder = ClientBuilder::new( + reqwest_client_builder + .build() + .map_err(SmartyError::RequestProcess)?, + ) + .with(SmartyRetryMiddleware::new(options.num_retries)); if options.logging_enabled { client_builder = client_builder.with(LoggingMiddleware); diff --git a/smarty-rust-sdk/src/sdk/options.rs b/smarty-rust-sdk/src/sdk/options.rs index 5478ed4..31d7c27 100644 --- a/smarty-rust-sdk/src/sdk/options.rs +++ b/smarty-rust-sdk/src/sdk/options.rs @@ -1,3 +1,5 @@ +use reqwest::Proxy; + use crate::sdk::authentication::Authenticate; /// A builder for the options @@ -17,6 +19,8 @@ pub struct OptionsBuilder { logging_enabled: bool, headers: Vec<(String, String)>, authentication: Option>, + + proxy: Option, } // Allowing this because it is a builder pattern @@ -30,6 +34,8 @@ impl OptionsBuilder { logging_enabled: false, headers: vec![], authentication, + + proxy: None, } } @@ -42,6 +48,8 @@ impl OptionsBuilder { logging_enabled: self.logging_enabled, headers: self.headers, authentication: self.authentication, + + proxy: self.proxy, } } @@ -68,6 +76,12 @@ impl OptionsBuilder { self.headers = headers; self } + + /// Adds a custom proxy for the request to point to. + pub fn with_proxy(mut self, proxy: Proxy) -> Self { + self.proxy = Some(proxy); + self + } } /// Options that can be passed into a new client @@ -89,6 +103,9 @@ pub struct Options { // Authentication pub(crate) authentication: Option>, + + // Proxy + pub(crate) proxy: Option, } impl Clone for Options { @@ -99,6 +116,7 @@ impl Clone for Options { logging_enabled: self.logging_enabled, headers: self.headers.clone(), authentication: self.authentication.as_ref().map(|x| x.clone_box()), + proxy: self.proxy.clone(), } } } diff --git a/smarty-rust-sdk/src/us_autocomplete_api/client.rs b/smarty-rust-sdk/src/us_autocomplete_api/client.rs index 27277bb..3d0cfa5 100644 --- a/smarty-rust-sdk/src/us_autocomplete_api/client.rs +++ b/smarty-rust-sdk/src/us_autocomplete_api/client.rs @@ -6,7 +6,7 @@ use crate::us_autocomplete_api::lookup::Lookup; use crate::us_autocomplete_api::suggestion::SuggestionListing; use reqwest::Method; use smarty_rust_proc_macro::smarty_api; -use url::{ParseError, Url}; +use url::Url; #[smarty_api( api_path = "suggest", diff --git a/smarty-rust-sdk/src/us_autocomplete_pro_api/client.rs b/smarty-rust-sdk/src/us_autocomplete_pro_api/client.rs index bee9ae6..31b495c 100644 --- a/smarty-rust-sdk/src/us_autocomplete_pro_api/client.rs +++ b/smarty-rust-sdk/src/us_autocomplete_pro_api/client.rs @@ -6,7 +6,7 @@ use crate::us_autocomplete_pro_api::lookup::Lookup; use crate::us_autocomplete_pro_api::suggestion::SuggestionListing; use reqwest::Method; use smarty_rust_proc_macro::smarty_api; -use url::{ParseError, Url}; +use url::Url; #[smarty_api( api_path = "lookup", diff --git a/smarty-rust-sdk/src/us_enrichment_api/client.rs b/smarty-rust-sdk/src/us_enrichment_api/client.rs index c4a122c..530030d 100644 --- a/smarty-rust-sdk/src/us_enrichment_api/client.rs +++ b/smarty-rust-sdk/src/us_enrichment_api/client.rs @@ -7,7 +7,7 @@ use crate::us_enrichment_api::results::EnrichmentResponse; use reqwest::Method; use serde::de::DeserializeOwned; use smarty_rust_proc_macro::smarty_api; -use url::{ParseError, Url}; +use url::Url; #[smarty_api( api_path = "lookup", diff --git a/smarty-rust-sdk/src/us_extract_api/client.rs b/smarty-rust-sdk/src/us_extract_api/client.rs index 79da26c..58c4a9a 100644 --- a/smarty-rust-sdk/src/us_extract_api/client.rs +++ b/smarty-rust-sdk/src/us_extract_api/client.rs @@ -5,7 +5,7 @@ use crate::sdk::send_request; use crate::us_extract_api::lookup::Lookup; use reqwest::Method; use smarty_rust_proc_macro::smarty_api; -use url::{ParseError, Url}; +use url::Url; #[smarty_api( api_path = "", diff --git a/smarty-rust-sdk/src/us_reverse_geo_api/client.rs b/smarty-rust-sdk/src/us_reverse_geo_api/client.rs index 840e0a7..cbc89e9 100644 --- a/smarty-rust-sdk/src/us_reverse_geo_api/client.rs +++ b/smarty-rust-sdk/src/us_reverse_geo_api/client.rs @@ -6,7 +6,7 @@ use crate::us_reverse_geo_api::address::Results; use crate::us_reverse_geo_api::lookup::Lookup; use reqwest::Method; use smarty_rust_proc_macro::smarty_api; -use url::{ParseError, Url}; +use url::Url; #[smarty_api( api_path = "lookup", diff --git a/smarty-rust-sdk/src/us_street_api/client.rs b/smarty-rust-sdk/src/us_street_api/client.rs index e808878..9795d2a 100644 --- a/smarty-rust-sdk/src/us_street_api/client.rs +++ b/smarty-rust-sdk/src/us_street_api/client.rs @@ -2,7 +2,7 @@ use crate::sdk::batch::Batch; use crate::sdk::client::Client; use reqwest::Method; use smarty_rust_proc_macro::smarty_api; -use url::{ParseError, Url}; +use url::Url; use crate::sdk::error::SmartyError; use crate::sdk::options::Options; diff --git a/smarty-rust-sdk/src/us_zipcode_api/client.rs b/smarty-rust-sdk/src/us_zipcode_api/client.rs index 1990704..40e127c 100644 --- a/smarty-rust-sdk/src/us_zipcode_api/client.rs +++ b/smarty-rust-sdk/src/us_zipcode_api/client.rs @@ -7,7 +7,7 @@ use crate::us_zipcode_api::candidate::ZipcodeResult; use crate::us_zipcode_api::lookup::Lookup; use reqwest::Method; use smarty_rust_proc_macro::smarty_api; -use url::{ParseError, Url}; +use url::Url; #[smarty_api( default_url = "https://us-zipcode.api.smarty.com/",