Skip to content

Commit

Permalink
feat(trait): add emailprovider to match async architecture (#7)
Browse files Browse the repository at this point in the history
* Also update mod to fix all isues in providers folder
  • Loading branch information
jonperron authored Nov 9, 2024
1 parent 3333b49 commit 19109ef
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ tokio = { version = "1", features = ["full"] }
config = "0.14.0"
serde_yaml = "0.9.34+deprecated"

# HTTP calls
reqwest = "0.12.9"

# Logging
tracing = "0.1.26"

Expand Down
18 changes: 15 additions & 3 deletions src/providers/mailgun.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use axum::async_trait;
use reqwest::Client;
use serde::Deserialize;

use crate::providers::EmailNotification;
use crate::providers::errors::ProviderError;
use crate::providers::notifications::EmailNotification;
use crate::providers::providers::EmailProvider;

#[derive(Debug, Deserialize, Clone)]
pub struct MailgunConfig {
Expand All @@ -26,7 +28,10 @@ impl MailgunProvider {
async fn send_email(&self, notification: &EmailNotification) -> Result<(), ProviderError> {
let url = format!(
"{}/v3/{}/messages",
self.config.base_url.clone().unwrap_or_else(|| "https://api.mailgun.net".to_string()),
self.config
.base_url
.clone()
.unwrap_or_else(|| "https://api.mailgun.net".to_string()),
self.config.domain
);

Expand Down Expand Up @@ -57,4 +62,11 @@ impl MailgunProvider {
)))
}
}
}
}

#[async_trait]
impl EmailProvider for MailgunProvider {
async fn send(&self, notification: EmailNotification) -> Result<(), ProviderError> {
self.send_email(&notification).await
}
}
13 changes: 4 additions & 9 deletions src/providers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use serde:: {Serialize, Deserialize};

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct EmailNotification {
pub from: String,
pub to: String,
pub subject: String,
pub body: String,
}
mod errors;
mod mailgun;
mod notifications;
mod providers;
9 changes: 9 additions & 0 deletions src/providers/notifications.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct EmailNotification {
pub from: String,
pub to: String,
pub subject: String,
pub body: String,
}
9 changes: 9 additions & 0 deletions src/providers/providers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use axum::async_trait;

use crate::providers::errors::ProviderError;
use crate::providers::notifications::EmailNotification;

#[async_trait]
pub trait EmailProvider {
async fn send(&self, notification: EmailNotification) -> Result<(), ProviderError>;
}

0 comments on commit 19109ef

Please sign in to comment.