-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c7af6d
commit b674f84
Showing
3 changed files
with
83 additions
and
5 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
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
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,7 @@ | ||
use std::sync::Arc; | ||
|
||
use reqwest::Method; | ||
use serde::{Deserialize, Deserializer}; | ||
|
||
use crate::types::{CreateEmailBaseOptions, CreateEmailResponse, Email}; | ||
use crate::{Config, Result}; | ||
|
@@ -33,8 +34,6 @@ impl EmailsSvc { | |
|
||
let request = self.0.build(Method::GET, &path); | ||
let response = self.0.send(request).await?; | ||
// dbg!(response.text().await); | ||
// todo!(); | ||
let content = response.json::<Email>().await?; | ||
|
||
Ok(content) | ||
|
@@ -48,6 +47,8 @@ pub mod types { | |
use ecow::EcoString; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::emails::parse_nullable_vec; | ||
|
||
/// Unique [`Email`] identifier. | ||
#[derive(Debug, Clone, Deserialize)] | ||
pub struct EmailId(EcoString); | ||
|
@@ -350,11 +351,13 @@ pub mod types { | |
/// The HTML body of the email. | ||
pub html: Option<String>, | ||
/// The plain text body of the email. | ||
pub text: String, | ||
pub text: Option<String>, | ||
|
||
/// The email addresses of the blind carbon copy recipients. | ||
#[serde(deserialize_with = "parse_nullable_vec")] | ||
pub bcc: Vec<String>, | ||
/// The email addresses of the carbon copy recipients. | ||
#[serde(deserialize_with = "parse_nullable_vec")] | ||
pub cc: Vec<String>, | ||
/// The email addresses to which replies should be sent. | ||
pub reply_to: Option<Vec<String>>, | ||
|
@@ -363,9 +366,20 @@ pub mod types { | |
} | ||
} | ||
|
||
/// Turns: | ||
/// - `null` -> `[]` | ||
/// - `["text"]` -> `["text"]` | ||
fn parse_nullable_vec<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let opt = Option::deserialize(deserializer)?; | ||
Ok(opt.unwrap_or_else(Vec::new)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::types::{CreateEmailBaseOptions, Tag}; | ||
use crate::types::{CreateEmailBaseOptions, Email, Tag}; | ||
use crate::{tests::CLIENT, Resend, Result}; | ||
|
||
#[tokio::test] | ||
|
@@ -393,6 +407,57 @@ mod test { | |
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn deserialize_test() { | ||
let email = r#"{ | ||
"object": "email", | ||
"id": "6757a66c-3a5b-49ee-98cc-fca7a5f423c0", | ||
"to": [ | ||
"[email protected]" | ||
], | ||
"from": "[email protected]>", | ||
"created_at": "2024-07-11 07:49:53.682607+00", | ||
"subject": "Subject", | ||
"bcc": null, | ||
"cc": null, | ||
"reply_to": null, | ||
"last_event": "delivered", | ||
"html": "<div></div>", | ||
"text": null | ||
}"#; | ||
|
||
let res = serde_json::from_str::<Email>(email); | ||
assert!(res.is_ok()); | ||
let res = res.unwrap(); | ||
assert!(res.cc.is_empty()); | ||
assert!(res.bcc.is_empty()); | ||
assert!(res.text.is_none()); | ||
|
||
let email = r#"{ | ||
"object": "email", | ||
"id": "6757a66c-3a5b-49ee-98cc-fca7a5f423c0", | ||
"to": [ | ||
"[email protected]" | ||
], | ||
"from": "[email protected]>", | ||
"created_at": "2024-07-11 07:49:53.682607+00", | ||
"subject": "Subject", | ||
"bcc": ["hello", "world"], | ||
"cc": ["!"], | ||
"reply_to": null, | ||
"last_event": "delivered", | ||
"html": "<div></div>", | ||
"text": "Not null" | ||
}"#; | ||
|
||
let res = serde_json::from_str::<Email>(email); | ||
assert!(res.is_ok()); | ||
let res = res.unwrap(); | ||
assert!(!res.cc.is_empty()); | ||
assert!(!res.bcc.is_empty()); | ||
assert!(res.text.is_some()); | ||
} | ||
|
||
#[test] | ||
#[cfg(feature = "blocking")] | ||
fn all_blocking() -> Result<()> { | ||
|