-
Notifications
You must be signed in to change notification settings - Fork 3
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
49151c9
commit 5a2686c
Showing
6 changed files
with
171 additions
and
11 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
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,104 @@ | ||
use anyhow::Result; | ||
use reqwest::{Client, StatusCode}; | ||
use scraper::Html; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub(crate) struct Recipient { | ||
pub address: String, | ||
pub name: String, | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
#[serde(rename_all = "kebab-case")] | ||
pub(crate) struct Header { | ||
pub from: String, | ||
pub to: String, | ||
pub subject: String, | ||
pub content_type: String, | ||
pub content_transfer_encoding: String, | ||
pub date: String, | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub(crate) struct EnvelopeRecipient { | ||
pub address: String, | ||
pub args: bool, | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub(crate) struct Envelope { | ||
pub from: EnvelopeRecipient, | ||
pub to: Vec<EnvelopeRecipient>, | ||
pub host: String, | ||
pub remote_address: String, | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub(crate) struct Mail { | ||
pub html: String, | ||
pub headers: Header, | ||
pub subject: String, | ||
pub priority: String, | ||
pub from: Vec<Recipient>, | ||
pub to: Vec<Recipient>, | ||
pub date: String, | ||
pub id: String, | ||
pub time: String, | ||
pub read: bool, | ||
pub envelope: Envelope, | ||
pub source: String, | ||
pub size: usize, | ||
pub size_human: String, | ||
pub attachments: Option<Vec<String>>, | ||
pub calculated_bcc: Vec<String>, | ||
} | ||
|
||
impl Mail { | ||
pub fn html(&self) -> Html { | ||
Html::parse_fragment(&self.html) | ||
} | ||
} | ||
|
||
pub(crate) struct MailDevClient { | ||
pub client: Client, | ||
} | ||
|
||
impl MailDevClient { | ||
pub(crate) fn new() -> Self { | ||
let client = reqwest::Client::builder() | ||
.redirect(reqwest::redirect::Policy::none()) | ||
.build() | ||
.unwrap(); | ||
|
||
Self { client } | ||
} | ||
|
||
pub(crate) async fn latest(&self) -> Result<Option<Mail>> { | ||
let response = self | ||
.client | ||
.get("http://localhost:1080/email") | ||
.send() | ||
.await?; | ||
|
||
match response.status() { | ||
StatusCode::OK => { | ||
let response_body = response.json::<Vec<Mail>>().await?; | ||
|
||
if response_body.is_empty() { | ||
return Ok(None); | ||
} | ||
|
||
let mail = response_body.last().unwrap().clone().to_owned(); | ||
|
||
Ok(Some(mail)) | ||
} | ||
StatusCode::NOT_FOUND => Ok(None), | ||
_ => unreachable!(), | ||
} | ||
} | ||
} |
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,2 +1,3 @@ | ||
pub mod environment; | ||
pub mod http; | ||
pub mod maildev; |