Skip to content

Commit

Permalink
'new' Accept client
Browse files Browse the repository at this point in the history
  • Loading branch information
RossyWhite committed Aug 28, 2024
1 parent d3d67f7 commit b5ea713
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 20 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
18 changes: 5 additions & 13 deletions src/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,13 @@ 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(),
Expand All @@ -217,17 +220,6 @@ impl Sender {
self.host = host.into();
}

/// Sets client to use for the API. This is useful if you want to customize the client.
pub fn set_client(&mut self, client: Client) {
self.client = client;
}

/// Sets blocking client to use for the API. This is useful if you want to customize the client.
#[cfg(feature = "blocking")]
pub fn set_blocking_client(&mut self, blocking_client: reqwest::blocking::Client) {
self.blocking_client = blocking_client;
}

fn get_headers(&self) -> Result<HeaderMap, InvalidHeaderValue> {
let mut headers = HeaderMap::with_capacity(3);
headers.insert(
Expand Down

0 comments on commit b5ea713

Please sign in to comment.