Skip to content

Commit

Permalink
Merge pull request #22 from TheEmeraldBee/master
Browse files Browse the repository at this point in the history
Adjust Options to allow for custom url instead of using it through Client.
  • Loading branch information
RyanLCox1 authored Jul 25, 2024
2 parents 0f47ed3 + 8e3e063 commit d9afffd
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 23 deletions.
22 changes: 8 additions & 14 deletions smarty-rust-proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, SmartyError> {
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<Self, SmartyError> {
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<Self, SmartyError> {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand Down
1 change: 0 additions & 1 deletion smarty-rust-sdk/src/international_street_api/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 17 additions & 0 deletions smarty-rust-sdk/src/sdk/options.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use reqwest::Proxy;
use url::Url;

use crate::sdk::authentication::Authenticate;

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

url: Option<Url>,

proxy: Option<Proxy>,
}

Expand All @@ -35,6 +38,8 @@ impl OptionsBuilder {
headers: vec![],
authentication,

url: None,

proxy: None,
}
}
Expand All @@ -49,6 +54,8 @@ impl OptionsBuilder {
headers: self.headers,
authentication: self.authentication,

url: self.url,

proxy: self.proxy,
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -104,6 +117,9 @@ pub struct Options {
// Authentication
pub(crate) authentication: Option<Box<dyn Authenticate>>,

// Url
pub(crate) url: Option<Url>,

// Proxy
pub(crate) proxy: Option<Proxy>,
}
Expand All @@ -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(),
}
}
Expand Down
1 change: 0 additions & 1 deletion smarty-rust-sdk/src/us_autocomplete_api/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion smarty-rust-sdk/src/us_autocomplete_pro_api/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion smarty-rust-sdk/src/us_enrichment_api/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion smarty-rust-sdk/src/us_extract_api/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "",
Expand Down
1 change: 0 additions & 1 deletion smarty-rust-sdk/src/us_reverse_geo_api/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion smarty-rust-sdk/src/us_street_api/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion smarty-rust-sdk/src/us_zipcode_api/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand Down

0 comments on commit d9afffd

Please sign in to comment.