Skip to content

Commit

Permalink
Allow using custom client (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
RossyWhite authored Aug 30, 2024
1 parent c868476 commit 3e31294
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 13 deletions.
8 changes: 4 additions & 4 deletions examples/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion examples/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion examples/v3_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
2 changes: 1 addition & 1 deletion examples/v3_disable_tracking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}",),
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 18 additions & 4 deletions src/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Client>) -> 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<reqwest::blocking::Client>,
) -> 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<S: Into<String>>(&mut self, host: S) {
Expand Down

0 comments on commit 3e31294

Please sign in to comment.