From add51c95b03d67d29102366630d75b332c4493c3 Mon Sep 17 00:00:00 2001 From: brightoncox Date: Wed, 17 Jul 2024 21:32:59 -0600 Subject: [PATCH] Changed Options to follow better format to set custom url. Removed Client::new_custom_base_url() to instead be through the options builder. --- smarty-rust-proc-macro/src/lib.rs | 22 +++++++------------ .../international_autocomplete_api/client.rs | 1 - .../src/international_street_api/client.rs | 1 - smarty-rust-sdk/src/sdk/options.rs | 17 ++++++++++++++ .../src/us_autocomplete_api/client.rs | 1 - .../src/us_autocomplete_pro_api/client.rs | 1 - .../src/us_enrichment_api/client.rs | 1 - smarty-rust-sdk/src/us_extract_api/client.rs | 1 - .../src/us_reverse_geo_api/client.rs | 1 - smarty-rust-sdk/src/us_street_api/client.rs | 1 - smarty-rust-sdk/src/us_zipcode_api/client.rs | 1 - 11 files changed, 25 insertions(+), 23 deletions(-) diff --git a/smarty-rust-proc-macro/src/lib.rs b/smarty-rust-proc-macro/src/lib.rs index 111ddd0..c347c87 100644 --- a/smarty-rust-proc-macro/src/lib.rs +++ b/smarty-rust-proc-macro/src/lib.rs @@ -101,23 +101,17 @@ fn impl_smarty_api_macro(attrs: &MacroArgs, ast: &mut syn::DeriveInput) -> Token // Lets make sure that the API Type has the values it needs. let mut result = quote! { - - pub struct #name { - pub(crate) client: Client - } - - impl #name { - /// Creates a new client with the given options - pub fn new(options: Options) -> Result { - Self::new_custom_base_url(#default_url.parse()?, options) + pub struct #name { + pub(crate) client: Client } - /// 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 { - Ok(Self {client: Client::new(base_url, options, #api_path)?}) + impl #name { + /// Creates a new client with the given options + pub fn new(options: Options) -> Result { + let url = options.url.clone().unwrap_or(#default_url.parse().expect("Parsing Constant should be OK")); + Ok(Self {client: Client::new(url, options, #api_path)?}) + } } - } - }; let lookup_handler = match attrs.result_handler.lookup { diff --git a/smarty-rust-sdk/src/international_autocomplete_api/client.rs b/smarty-rust-sdk/src/international_autocomplete_api/client.rs index 4503d3e..eef6aa2 100644 --- a/smarty-rust-sdk/src/international_autocomplete_api/client.rs +++ b/smarty-rust-sdk/src/international_autocomplete_api/client.rs @@ -6,7 +6,6 @@ use crate::sdk::options::Options; use crate::sdk::send_request; use reqwest::Method; use smarty_rust_proc_macro::smarty_api; -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 5af974e..c033cd7 100644 --- a/smarty-rust-sdk/src/international_street_api/client.rs +++ b/smarty-rust-sdk/src/international_street_api/client.rs @@ -6,7 +6,6 @@ use crate::sdk::options::Options; use crate::sdk::send_request; use reqwest::Method; use smarty_rust_proc_macro::smarty_api; -use url::Url; #[smarty_api( api_path = "verify", diff --git a/smarty-rust-sdk/src/sdk/options.rs b/smarty-rust-sdk/src/sdk/options.rs index 31d7c27..66b3a99 100644 --- a/smarty-rust-sdk/src/sdk/options.rs +++ b/smarty-rust-sdk/src/sdk/options.rs @@ -1,4 +1,5 @@ use reqwest::Proxy; +use url::Url; use crate::sdk::authentication::Authenticate; @@ -20,6 +21,8 @@ pub struct OptionsBuilder { headers: Vec<(String, String)>, authentication: Option>, + url: Option, + proxy: Option, } @@ -35,6 +38,8 @@ impl OptionsBuilder { headers: vec![], authentication, + url: None, + proxy: None, } } @@ -49,6 +54,8 @@ impl OptionsBuilder { headers: self.headers, authentication: self.authentication, + url: self.url, + proxy: self.proxy, } } @@ -77,6 +84,12 @@ impl OptionsBuilder { self } + /// Sets the base url that the request should use. + pub fn with_url(mut self, url: Url) -> Self { + self.url = Some(url); + self + } + /// Adds a custom proxy for the request to point to. pub fn with_proxy(mut self, proxy: Proxy) -> Self { self.proxy = Some(proxy); @@ -104,6 +117,9 @@ pub struct Options { // Authentication pub(crate) authentication: Option>, + // Url + pub(crate) url: Option, + // Proxy pub(crate) proxy: Option, } @@ -116,6 +132,7 @@ impl Clone for Options { logging_enabled: self.logging_enabled, headers: self.headers.clone(), authentication: self.authentication.as_ref().map(|x| x.clone_box()), + url: self.url.clone(), 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 3d0cfa5..b7960b2 100644 --- a/smarty-rust-sdk/src/us_autocomplete_api/client.rs +++ b/smarty-rust-sdk/src/us_autocomplete_api/client.rs @@ -6,7 +6,6 @@ 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::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 31b495c..70ebf3a 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,6 @@ 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::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 530030d..2a83498 100644 --- a/smarty-rust-sdk/src/us_enrichment_api/client.rs +++ b/smarty-rust-sdk/src/us_enrichment_api/client.rs @@ -7,7 +7,6 @@ use crate::us_enrichment_api::results::EnrichmentResponse; use reqwest::Method; use serde::de::DeserializeOwned; use smarty_rust_proc_macro::smarty_api; -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 58c4a9a..e578336 100644 --- a/smarty-rust-sdk/src/us_extract_api/client.rs +++ b/smarty-rust-sdk/src/us_extract_api/client.rs @@ -5,7 +5,6 @@ use crate::sdk::send_request; use crate::us_extract_api::lookup::Lookup; use reqwest::Method; use smarty_rust_proc_macro::smarty_api; -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 cbc89e9..db551b1 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,6 @@ 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::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 9795d2a..7a5c398 100644 --- a/smarty-rust-sdk/src/us_street_api/client.rs +++ b/smarty-rust-sdk/src/us_street_api/client.rs @@ -2,7 +2,6 @@ use crate::sdk::batch::Batch; use crate::sdk::client::Client; use reqwest::Method; use smarty_rust_proc_macro::smarty_api; -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 40e127c..73aac23 100644 --- a/smarty-rust-sdk/src/us_zipcode_api/client.rs +++ b/smarty-rust-sdk/src/us_zipcode_api/client.rs @@ -7,7 +7,6 @@ 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::Url; #[smarty_api( default_url = "https://us-zipcode.api.smarty.com/",