zero4rs 2.0.0

zero4rs is a powerful, pragmatic, and extremely fast web framework for Rust
Documentation
use lazy_static::lazy_static;

use reqwest::Client;

lazy_static! {
    static ref POST_MARK_FROM_EMAIL: &'static str = "something@gmail.com";
    static ref POST_MARK_BASE_URL: &'static str = "https://api.postmarkapp.com";
    static ref POST_MARK_API_TOKEN: &'static str = "YOUR_TOKEN_HERE";
}

pub struct EmailClient {
    http_client: Client,
    sender: &'static str,
}

#[derive(serde::Serialize)]
// Lifetime parameters always start with an apostrophe, `'`
struct SendEmailRequest<'a> {
    from: &'a str,
    to: &'a str,
    subject: &'a str,
    html_body: &'a str,
}

// curl "https://api.postmarkapp.com/email" \
//   -X POST \
//   -H "Accept: application/json" \
//   -H "Content-Type: application/json" \
//   -H "X-Postmark-Server-Token: server token" \
//   -d '{
//   "From": "sender@example.com",
//   "To": "receiver@example.com",
//   "Subject": "Postmark test",
//   "TextBody": "Hello dear Postmark user.",
//   "HtmlBody": "<html><body><strong>Hello</strong> dear Postmark user.</body></html>"
// }'

impl EmailClient {
    pub fn new() -> Self {
        Self {
            http_client: Client::new(),
            sender: &POST_MARK_FROM_EMAIL,
        }
    }

    pub async fn send_email(
        &self,
        recipient: SubscriberEmail,
        subject: &str,
        email_body: &str,
    ) -> Result<(), reqwest::Error> {
        let url = format!("{}/email", POST_MARK_BASE_URL);

        // Refactoring: Avoid Unnecessary Memory Allocations
        let request_body = SendEmailRequest {
            from: self.sender,
            to: recipient.as_ref(),
            subject,
            html_body: email_body,
        };

        self.http_client
            .post(&url)
            .header("X-Postmark-Server-Token", POST_MARK_API_TOKEN)
            .json(&request_body)
            .send()
            .await?
            .error_for_status()?;

        Ok(())
    }
}