Skip to content

Commit d9afffd

Browse files
authored
Merge pull request #22 from TheEmeraldBee/master
Adjust Options to allow for custom url instead of using it through Client.
2 parents 0f47ed3 + 8e3e063 commit d9afffd

File tree

11 files changed

+25
-23
lines changed

11 files changed

+25
-23
lines changed

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,17 @@ fn impl_smarty_api_macro(attrs: &MacroArgs, ast: &mut syn::DeriveInput) -> Token
101101

102102
// Lets make sure that the API Type has the values it needs.
103103
let mut result = quote! {
104-
105-
pub struct #name {
106-
pub(crate) client: Client
107-
}
108-
109-
impl #name {
110-
/// Creates a new client with the given options
111-
pub fn new(options: Options) -> Result<Self, SmartyError> {
112-
Self::new_custom_base_url(#default_url.parse()?, options)
104+
pub struct #name {
105+
pub(crate) client: Client
113106
}
114107

115-
/// Creates a new client with the given options that points to a different url.
116-
pub fn new_custom_base_url(base_url: Url, options: Options) -> Result<Self, SmartyError> {
117-
Ok(Self {client: Client::new(base_url, options, #api_path)?})
108+
impl #name {
109+
/// Creates a new client with the given options
110+
pub fn new(options: Options) -> Result<Self, SmartyError> {
111+
let url = options.url.clone().unwrap_or(#default_url.parse().expect("Parsing Constant should be OK"));
112+
Ok(Self {client: Client::new(url, options, #api_path)?})
113+
}
118114
}
119-
}
120-
121115
};
122116

123117
let lookup_handler = match attrs.result_handler.lookup {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ 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::Url;
109

1110
#[smarty_api(
1211
api_path = "v2/lookup/",

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ 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::Url;
109

1110
#[smarty_api(
1211
api_path = "verify",

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use reqwest::Proxy;
2+
use url::Url;
23

34
use crate::sdk::authentication::Authenticate;
45

@@ -20,6 +21,8 @@ pub struct OptionsBuilder {
2021
headers: Vec<(String, String)>,
2122
authentication: Option<Box<dyn Authenticate>>,
2223

24+
url: Option<Url>,
25+
2326
proxy: Option<Proxy>,
2427
}
2528

@@ -35,6 +38,8 @@ impl OptionsBuilder {
3538
headers: vec![],
3639
authentication,
3740

41+
url: None,
42+
3843
proxy: None,
3944
}
4045
}
@@ -49,6 +54,8 @@ impl OptionsBuilder {
4954
headers: self.headers,
5055
authentication: self.authentication,
5156

57+
url: self.url,
58+
5259
proxy: self.proxy,
5360
}
5461
}
@@ -77,6 +84,12 @@ impl OptionsBuilder {
7784
self
7885
}
7986

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+
8093
/// Adds a custom proxy for the request to point to.
8194
pub fn with_proxy(mut self, proxy: Proxy) -> Self {
8295
self.proxy = Some(proxy);
@@ -104,6 +117,9 @@ pub struct Options {
104117
// Authentication
105118
pub(crate) authentication: Option<Box<dyn Authenticate>>,
106119

120+
// Url
121+
pub(crate) url: Option<Url>,
122+
107123
// Proxy
108124
pub(crate) proxy: Option<Proxy>,
109125
}
@@ -116,6 +132,7 @@ impl Clone for Options {
116132
logging_enabled: self.logging_enabled,
117133
headers: self.headers.clone(),
118134
authentication: self.authentication.as_ref().map(|x| x.clone_box()),
135+
url: self.url.clone(),
119136
proxy: self.proxy.clone(),
120137
}
121138
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ 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::Url;
109

1110
#[smarty_api(
1211
api_path = "suggest",

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ 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::Url;
109

1110
#[smarty_api(
1211
api_path = "lookup",

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ 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::Url;
1110

1211
#[smarty_api(
1312
api_path = "lookup",

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ 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::Url;
98

109
#[smarty_api(
1110
api_path = "",

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ 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::Url;
109

1110
#[smarty_api(
1211
api_path = "lookup",

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::sdk::batch::Batch;
22
use crate::sdk::client::Client;
33
use reqwest::Method;
44
use smarty_rust_proc_macro::smarty_api;
5-
use url::Url;
65

76
use crate::sdk::error::SmartyError;
87
use crate::sdk::options::Options;

0 commit comments

Comments
 (0)