Skip to content

Commit

Permalink
Merge pull request #9 from OLoKo64/feature/v0.4.5
Browse files Browse the repository at this point in the history
Feat: v0.4.5
  • Loading branch information
oloko64 authored Apr 28, 2023
2 parents 723a021 + b5ee114 commit d64ee4a
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 8 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sendgrid_thin"
version = "0.4.4"
version = "0.4.5"
edition = "2021"
authors = ["OLoKo64 <[email protected]>"]
description = "A small unofficial library to send emails using Sendgrid."
Expand All @@ -17,9 +17,9 @@ default = ["blocking"]
blocking = []

[dependencies]
reqwest = { version = "0.11.14", features = ["blocking"] }
serde = { version = "1.0.156", features = ["derive"] }
serde_json = "1.0.94"
reqwest = { version = "0.11.16", features = ["blocking"] }
serde = { version = "1.0.160", features = ["derive"] }
serde_json = "1.0.96"

[dev-dependencies]
tokio = { version = "1.26.0", features = ["macros", "rt"] }
tokio = { version = "1.28.0", features = ["macros", "rt"] }
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ I recommend the [dotenvy](https://crates.io/crates/dotenvy) crate to load the AP
)
.set_content_type(ContentType::Html)
.set_send_at(1668281500)
.set_request_timeout(std::time::Duration::from_secs(10))
.set_cc_emails(["[email protected]", "[email protected]"])
.build()
.unwrap();
Expand Down
80 changes: 77 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::Serialize;
use std::{
error::Error,
fmt,
time::{SystemTime, UNIX_EPOCH},
time::{Duration, SystemTime, UNIX_EPOCH},
};

pub enum ContentType {
Expand Down Expand Up @@ -35,13 +35,15 @@ impl Error for SendgridError {}
pub struct Sendgrid {
api_key: String,
send_at: Option<u64>,
request_timeout: Option<Duration>,
sendgrid_request_body: String,
}

#[derive(Debug, Clone, PartialEq)]
#[must_use]
pub struct SendgridBuilder {
api_key: String,
request_timeout: Option<Duration>,
sendgrid_email: SendgridEmail,
}

Expand Down Expand Up @@ -170,6 +172,7 @@ impl SendgridBuilder {
{
SendgridBuilder {
api_key: api_key.into(),
request_timeout: None,
sendgrid_email: {
let mut sendgrid_email = SendgridEmail::default();
sendgrid_email.get_first_personalization().to = to_emails
Expand Down Expand Up @@ -291,6 +294,40 @@ impl SendgridBuilder {
self
}

/// Enables a request timeout.
///
/// The timeout is applied from when the request starts connecting until the response body has finished.
///
/// Default is no timeout for async requests and 30 seconds for blocking requests.
///
/// # Example
/// ```
/// use sendgrid_thin::Sendgrid;
///
/// #[tokio::main]
/// async fn main() {
/// let sendgrid = Sendgrid::builder(
/// "SENDGRID_API_KEY",
/// "[email protected]",
/// ["[email protected]","[email protected]"],
/// "subject of email",
/// "body of email",
/// )
/// .set_request_timeout(std::time::Duration::from_secs(10))
/// .build()
/// .unwrap();
///
/// match sendgrid.send().await {
/// Ok(message) => println!("{message}"),
/// Err(err) => println!("Error sending email: {err}"),
/// }
/// }
/// ```
pub fn set_request_timeout(mut self, request_timeout: Duration) -> SendgridBuilder {
self.request_timeout = Some(request_timeout);
self
}

/// Builds the Sendgrid struct.
/// # Example
/// ```
Expand Down Expand Up @@ -326,6 +363,7 @@ impl SendgridBuilder {
status_code: None,
}
})?,
request_timeout: self.request_timeout,
send_at: self.sendgrid_email.send_at,
})
}
Expand Down Expand Up @@ -417,7 +455,18 @@ impl Sendgrid {
/// Returns an error if the request fails.
#[cfg(feature = "blocking")]
pub fn send_blocking(&self) -> Result<String, SendgridError> {
let client = reqwest::blocking::Client::new();
let client;
if let Some(request_timeout) = self.request_timeout {
client = reqwest::blocking::Client::builder()
.timeout(request_timeout)
.build()
.map_err(|err| SendgridError {
message: err.to_string(),
status_code: None,
})?;
} else {
client = reqwest::blocking::Client::new();
}

let response = client
.post("https://api.sendgrid.com/v3/mail/send")
Expand Down Expand Up @@ -475,7 +524,18 @@ impl Sendgrid {
/// # Errors
/// Returns an error if the request fails.
pub async fn send(&self) -> Result<String, SendgridError> {
let client = reqwest::Client::new();
let client;
if let Some(request_timeout) = self.request_timeout {
client = reqwest::Client::builder()
.timeout(request_timeout)
.build()
.map_err(|err| SendgridError {
message: err.to_string(),
status_code: None,
})?;
} else {
client = reqwest::Client::new();
}

let response = client
.post("https://api.sendgrid.com/v3/mail/send")
Expand Down Expand Up @@ -645,6 +705,20 @@ mod tests {
assert_eq!(sendgrid.sendgrid_email.send_at, Some(1668271500));
}

#[test]
fn test_set_request_timeout() {
let sendgrid = Sendgrid::builder(
"SENDGRID_API_KEY",
"[email protected]",
["[email protected]"],
"subject",
"body",
);
assert_eq!(sendgrid.request_timeout, None);
let sendgrid = sendgrid.set_request_timeout(Duration::from_secs(10));
assert_eq!(sendgrid.request_timeout, Some(Duration::from_secs(10)));
}

#[test]
fn test_traits() {
fn assert_sync_traits<T: Sized + Send + Sync + Unpin>() {}
Expand Down

0 comments on commit d64ee4a

Please sign in to comment.