-
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.
Add email client (for verification) Work In Progress: this code exposes a compiler bug (1.72) encountered incremental compilation error with mir_shims(aa7d6ad7e64c8d82-9f4068ebffc54ad8) |
- Loading branch information
Showing
11 changed files
with
431 additions
and
161 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,119 @@ | ||
//! src/email_client.rs | ||
use reqwest::Client; | ||
use secrecy::{ExposeSecret, Secret}; | ||
|
||
use crate::domain::SubscriberEmail; | ||
|
||
#[derive(Clone)] | ||
pub struct EmailClient { | ||
http_client: Client, | ||
base_url: String, | ||
sender: SubscriberEmail, | ||
// We don't want to log this by accident | ||
authorization_token: Secret<String>, | ||
} | ||
|
||
impl EmailClient { | ||
pub fn new(base_url: String, sender: SubscriberEmail, | ||
authorization_token: Secret<String>) -> Self { | ||
Self { | ||
http_client: Client::new(), | ||
base_url, | ||
sender, | ||
authorization_token, | ||
} | ||
} | ||
|
||
|
||
pub async fn send_email( | ||
&self, | ||
recipient: SubscriberEmail, | ||
subject: &str, | ||
html_content: &str, | ||
text_content: &str, | ||
) -> Result<(), reqwest::Error> { | ||
let url = format!("{}/email", self.base_url); | ||
let request_body = SendEmailRequest { | ||
from: self.sender.as_ref().to_owned(), | ||
to: recipient.as_ref().to_owned(), | ||
subject: subject.to_owned(), | ||
html_body: html_content.to_owned(), | ||
text_body: text_content.to_owned(), | ||
}; | ||
|
||
self | ||
.http_client | ||
.post(&url) | ||
.header( | ||
"X-Postmark-Server-Token", | ||
self.authorization_token.expose_secret(), | ||
) | ||
.json(&request_body) | ||
.send() | ||
.await?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(serde::Serialize)] | ||
struct SendEmailRequest { | ||
from: String, | ||
to: String, | ||
subject: String, | ||
html_body: String, | ||
text_body: String, | ||
} | ||
|
||
/* password: KsgAcS!aaJm3WLj | ||
api token: | ||
*/ | ||
#[cfg(test)] | ||
mod tests { | ||
use fake::{Fake, Faker}; | ||
use fake::faker::internet::en::SafeEmail; | ||
use fake::faker::lorem::en::{Paragraph, Sentence}; | ||
use secrecy::Secret; | ||
|
||
use wiremock::{Mock, MockServer, Request, ResponseTemplate}; | ||
use wiremock::matchers::{header, header_exists, method, path}; | ||
|
||
use crate::domain::SubscriberEmail; | ||
use crate::email_client::EmailClient; | ||
|
||
struct SendEmailBodyMatcher; | ||
|
||
impl wiremock::Match for SendEmailBodyMatcher { | ||
fn matches(&self, request: &Request) -> bool { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn send_email_fires_a_request_to_base_url() { | ||
// Arrange | ||
let mock_server = MockServer::start().await; | ||
let sender = SubscriberEmail::parse(SafeEmail().fake()).unwrap(); | ||
let email_client = EmailClient::new( | ||
mock_server.uri(), | ||
sender, | ||
Secret::new(Faker.fake()), | ||
); | ||
Mock::given(header_exists("X-Postmark-Server-Token")) | ||
.and(header("Content-Type", "application/json")) | ||
.and(path("/email")) | ||
.and(method("POST")) | ||
.respond_with(ResponseTemplate::new(200)) | ||
.expect(1) | ||
.mount(&mock_server) | ||
.await; | ||
let subscriber_email = SubscriberEmail::parse(SafeEmail().fake()).unwrap(); | ||
let subject: String = Sentence(1..2).fake(); | ||
let content: String = Paragraph(1..10).fake(); | ||
// Act | ||
let _ = email_client | ||
.send_email(subscriber_email, &subject, &content, &content) | ||
.await; | ||
// Assert | ||
// When going out of scope, the Mock::expect() are checked | ||
} | ||
} |
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,11 +1,8 @@ | ||
//! lib.rs | ||
|
||
|
||
|
||
|
||
pub mod configuration; | ||
pub mod domain; | ||
pub mod email_client; | ||
pub mod routes; | ||
pub mod startup; | ||
pub mod telemetry; |
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