-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from OLoKo64/feature/v0.4.5
Feat: v0.4.5
- Loading branch information
Showing
3 changed files
with
83 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
|
@@ -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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ use serde::Serialize; | |
use std::{ | ||
error::Error, | ||
fmt, | ||
time::{SystemTime, UNIX_EPOCH}, | ||
time::{Duration, SystemTime, UNIX_EPOCH}, | ||
}; | ||
|
||
pub enum ContentType { | ||
|
@@ -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, | ||
} | ||
|
||
|
@@ -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 | ||
|
@@ -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 | ||
/// ``` | ||
|
@@ -326,6 +363,7 @@ impl SendgridBuilder { | |
status_code: None, | ||
} | ||
})?, | ||
request_timeout: self.request_timeout, | ||
send_at: self.sendgrid_email.send_at, | ||
}) | ||
} | ||
|
@@ -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") | ||
|
@@ -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") | ||
|
@@ -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>() {} | ||
|