Skip to content

Commit

Permalink
impl new_blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
RossyWhite committed Aug 30, 2024
1 parent 4591528 commit 62358ce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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) => key.1,
None => panic!("Must supply API key in environment variables to use!"),
Expand Down
23 changes: 17 additions & 6 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,10 +200,7 @@ pub struct ASM {

impl Sender {
/// Construct a new V3 message sender.
pub fn new(
api_key: String,
client: Option<Client>,
) -> Sender {
pub fn new(api_key: String, client: Option<Client>) -> Sender {
Sender {
api_key,
client: client.unwrap_or_default(),
Expand All @@ -214,6 +210,21 @@ impl Sender {
}
}

/// 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 62358ce

Please sign in to comment.