Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions smarty-rust-proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::manual_unwrap_or_default)]

extern crate darling;
extern crate proc_macro;

Expand Down Expand Up @@ -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<Self, ParseError> {
pub fn new(options: Options) -> Result<Self, SmartyError> {
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<Self, ParseError> {
pub fn new_custom_base_url(base_url: Url, options: Options) -> Result<Self, SmartyError> {
Ok(Self {client: Client::new(base_url, options, #api_path)?})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand Down
2 changes: 1 addition & 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,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",
Expand Down
20 changes: 15 additions & 5 deletions smarty-rust-sdk/src/sdk/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -19,11 +19,21 @@ impl Client {
base_url: Url,
options: Options,
api_path: &str,
) -> Result<Client, ParseError> {
) -> Result<Client, SmartyError> {
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);
Expand Down
18 changes: 18 additions & 0 deletions smarty-rust-sdk/src/sdk/options.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use reqwest::Proxy;

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

/// A builder for the options
Expand All @@ -17,6 +19,8 @@ pub struct OptionsBuilder {
logging_enabled: bool,
headers: Vec<(String, String)>,
authentication: Option<Box<dyn Authenticate>>,

proxy: Option<Proxy>,
}

// Allowing this because it is a builder pattern
Expand All @@ -30,6 +34,8 @@ impl OptionsBuilder {
logging_enabled: false,
headers: vec![],
authentication,

proxy: None,
}
}

Expand All @@ -42,6 +48,8 @@ impl OptionsBuilder {
logging_enabled: self.logging_enabled,
headers: self.headers,
authentication: self.authentication,

proxy: self.proxy,
}
}

Expand All @@ -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
Expand All @@ -89,6 +103,9 @@ pub struct Options {

// Authentication
pub(crate) authentication: Option<Box<dyn Authenticate>>,

// Proxy
pub(crate) proxy: Option<Proxy>,
}

impl Clone for Options {
Expand All @@ -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(),
}
}
}
2 changes: 1 addition & 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,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",
Expand Down
2 changes: 1 addition & 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,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",
Expand Down
2 changes: 1 addition & 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,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",
Expand Down
2 changes: 1 addition & 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,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 = "",
Expand Down
2 changes: 1 addition & 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,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",
Expand Down
2 changes: 1 addition & 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,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;
Expand Down
2 changes: 1 addition & 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,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/",
Expand Down