diff --git a/examples/main.rs b/examples/main.rs index f45a553..c3b4ee5 100644 --- a/examples/main.rs +++ b/examples/main.rs @@ -4,11 +4,11 @@ use sendgrid::{Destination, Mail}; fn main() { let mut env_vars = std::env::vars(); let api_key_check = env_vars.find(|var| var.0 == "SENDGRID_API_KEY"); - let api_key: String; - match api_key_check { - Some(key) => api_key = key.1, + + let api_key: String = match api_key_check { + Some(key) => key.1, None => panic!("Must supply API key in environment variables to use!"), - } + }; let sg = SGClient::new(api_key); diff --git a/examples/v3.rs b/examples/v3.rs index 964b674..cb53432 100644 --- a/examples/v3.rs +++ b/examples/v3.rs @@ -19,7 +19,7 @@ fn main() { .add_personalization(p); let api_key = ::std::env::var("SG_API_KEY").unwrap(); - let sender = Sender::new(api_key); + let sender = Sender::new(api_key, None); let code = sender.blocking_send(&m); println!("{:?}", code); } diff --git a/examples/v3_async.rs b/examples/v3_async.rs index df82749..a0eff2a 100644 --- a/examples/v3_async.rs +++ b/examples/v3_async.rs @@ -22,7 +22,7 @@ async fn main() -> Result<(), SendgridError> { let mut env_vars = ::std::env::vars(); let api_key = env_vars.find(|v| v.0 == "SG_API_KEY").unwrap(); - let sender = Sender::new(api_key.1); + let sender = Sender::new(api_key.1, None); let resp = sender.send(&m).await?; println!("status: {}", resp.status()); diff --git a/examples/v3_disable_tracking.rs b/examples/v3_disable_tracking.rs index 1bf40cc..38ac0ff 100644 --- a/examples/v3_disable_tracking.rs +++ b/examples/v3_disable_tracking.rs @@ -43,7 +43,7 @@ fn main() { }) .add_personalization(person); - let sender = Sender::new(api_key.to_owned()); + let sender = Sender::new(api_key.to_owned(), None); match sender.blocking_send(&message) { Ok(res) => println!("sent {}", res.status()), Err(err) => eprintln!("err: {err}",), diff --git a/src/lib.rs b/src/lib.rs index da1504c..b07b316 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,9 +21,9 @@ //! # Features //! The projects has the following feature flags: //! * `rustls`: this feature flag switches the default SSL provider in the operating system (usually -//! OpenSSL) with RusTLS, which is a TLS implementation in Rust. +//! OpenSSL) with RusTLS, which is a TLS implementation in Rust. //! * `native-tls`: enabled by default, this feature flag enabled the default SSL provider in the -//! operating system (usually OpenSSL). +//! operating system (usually OpenSSL). //! * `blocking`: this feature flag allows you to construct a synchronous `SGClient`. //! //! ## Build Dependencies diff --git a/src/v3.rs b/src/v3.rs index 14a9d2a..016c489 100644 --- a/src/v3.rs +++ b/src/v3.rs @@ -8,12 +8,11 @@ use reqwest::header::{self, HeaderMap, HeaderValue, InvalidHeaderValue}; use serde::Serialize; use serde_json::{to_value, value::Value, value::Value::Object, Map}; +use crate::error::{RequestNotSuccessful, SendgridError, SendgridResult}; #[cfg(feature = "blocking")] use reqwest::blocking::Response as BlockingResponse; use reqwest::{Client, Response}; -use crate::error::{RequestNotSuccessful, SendgridError, SendgridResult}; - const V3_API_URL: &str = "https://api.sendgrid.com/v3/mail/send"; /// Just a redefinition of a map to store string keys and values. @@ -201,16 +200,31 @@ pub struct ASM { impl Sender { /// Construct a new V3 message sender. - pub fn new(api_key: String) -> Sender { + pub fn new(api_key: String, client: Option) -> Sender { Sender { api_key, - client: Client::new(), + client: client.unwrap_or_default(), #[cfg(feature = "blocking")] blocking_client: reqwest::blocking::Client::new(), host: V3_API_URL.to_string(), } } + /// Construct a new V3 message sender with a blocking client. + #[cfg(feature = "blocking")] + pub fn new_blocking( + api_key: String, + blocking_client: Option, + ) -> Sender { + Sender { + api_key, + client: Client::new(), + #[cfg(feature = "blocking")] + blocking_client: blocking_client.unwrap_or_default(), + host: V3_API_URL.to_string(), + } + } + /// Sets the host to use for the API. This is useful if you are using a proxy or a local /// development server. It should be a full URL, including the protocol. pub fn set_host>(&mut self, host: S) {