-
Notifications
You must be signed in to change notification settings - Fork 15
added templates (draft) #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BatiGencho
wants to merge
1
commit into
otterandrye:main
Choose a base branch
from
BatiGencho:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,224 @@ | ||
| use std::collections::HashMap; | ||
| use serde::{de::DeserializeOwned, Deserialize, Serialize}; | ||
| use crate::{Credentials, MailgunResult}; | ||
|
|
||
| const TEMPLATES_ENDPOINT: &str = "templates"; | ||
| const TEMPLATE_VERSIONS_ENDPOINT: &str = "versions"; | ||
|
|
||
| #[derive(Serialize, Deserialize, Debug, PartialEq)] | ||
| #[serde(rename_all = "camelCase")] | ||
| pub struct CreateTemplateResponse { | ||
| pub message: String, | ||
| pub template: TemplateResponse, | ||
| } | ||
|
|
||
| #[derive(Serialize, Deserialize, Debug, PartialEq)] | ||
| #[serde(rename_all = "camelCase")] | ||
| pub struct GetTemplatesResponse { | ||
| pub items: Vec<TemplateResponse>, | ||
| } | ||
|
|
||
| #[derive(Serialize, Deserialize, Debug, PartialEq)] | ||
| #[serde(rename_all = "camelCase")] | ||
| pub struct GetSingleTemplateResponse { | ||
| pub template: TemplateResponse, | ||
| } | ||
|
|
||
| #[derive(Serialize, Deserialize, Debug, PartialEq)] | ||
| #[serde(rename_all = "camelCase")] | ||
| pub struct TemplateResponse { | ||
| pub created_at: String, | ||
| pub created_by: String, | ||
| pub description: String, | ||
| pub name: String, | ||
| pub id: String, | ||
| pub version: Option<VersionResponse>, | ||
| pub versions: Option<Vec<VersionResponse>>, | ||
| } | ||
|
|
||
| #[derive(Serialize, Deserialize, Debug, PartialEq)] | ||
| #[serde(rename_all = "camelCase")] | ||
| pub struct VersionResponse { | ||
| created_at: String, | ||
| engine: String, | ||
| tag: String, | ||
| comment: String, | ||
| mjml: String, | ||
| template: Option<String>, | ||
| id: Option<String>, | ||
| active: bool, | ||
| } | ||
|
|
||
| #[derive(Serialize, Default, Deserialize, Debug, PartialEq)] | ||
| #[serde(rename_all = "camelCase")] | ||
| pub struct Template { | ||
| pub name: String, | ||
| pub description: String, | ||
| pub template: Option<String>, | ||
| pub tag: Option<String>, | ||
| pub engine: Option<String>, | ||
| pub comment: Option<String>, | ||
| } | ||
|
|
||
| impl Template { | ||
| fn into_params(self) -> HashMap<String, String> { | ||
| let mut params = HashMap::new(); | ||
|
|
||
| params.insert("name".to_string(), self.name); | ||
| params.insert("description".to_string(), self.description); | ||
|
|
||
| if let Some(template) = self.template { | ||
| params.insert("template".to_string(), template); | ||
| } | ||
|
|
||
| if let Some(tag) = self.tag { | ||
| params.insert("tag".to_string(), tag); | ||
| } | ||
|
|
||
| if let Some(engine) = self.engine { | ||
| params.insert("engine".to_string(), engine); | ||
| } | ||
|
|
||
| if let Some(comment) = self.comment { | ||
| params.insert("comment".to_string(), comment); | ||
| } | ||
|
|
||
| params | ||
| } | ||
| } | ||
|
|
||
| //------------------------------ | ||
| pub async fn create_template( | ||
| creds: &Credentials, | ||
| template: Template, | ||
| ) -> MailgunResult<CreateTemplateResponse> { | ||
| let client = reqwest::Client::new(); | ||
| create_template_with_client(&client, creds, template).await | ||
| } | ||
|
|
||
| pub async fn create_template_with_client( | ||
| client: &reqwest::Client, | ||
| creds: &Credentials, | ||
| template: Template, | ||
| ) -> MailgunResult<CreateTemplateResponse> { | ||
| let url = format!("{}/{}/{}", creds.api_base, creds.domain, TEMPLATES_ENDPOINT); | ||
| let request_builder = client.post(&url); | ||
| create_template_with_request_builder(request_builder, creds, template).await | ||
| } | ||
|
|
||
| pub async fn create_template_with_request_builder( | ||
| request_builder: reqwest::RequestBuilder, | ||
| creds: &Credentials, | ||
| template: Template, | ||
| ) -> MailgunResult<CreateTemplateResponse> { | ||
| let params = template.into_params(); | ||
|
|
||
| let res = request_builder | ||
| .basic_auth("api", Some(creds.api_key.clone())) | ||
| .form(¶ms) | ||
| .send() | ||
| .await? | ||
| .error_for_status()?; | ||
|
|
||
| let parsed: CreateTemplateResponse = res.json().await?; | ||
| Ok(parsed) | ||
| } | ||
| //------------------------------ | ||
|
|
||
| pub async fn get_templates( | ||
| creds: &Credentials, | ||
| template_name: Option<String>, | ||
| do_fetch_versions: bool, | ||
| ) -> MailgunResult<GetTemplatesResponse> { | ||
| let client = reqwest::Client::new(); | ||
| get_templates_with_client(&client, creds, template_name, do_fetch_versions).await | ||
| } | ||
|
|
||
| pub async fn get_templates_with_client( | ||
| client: &reqwest::Client, | ||
| creds: &Credentials, | ||
| template_name: Option<String>, | ||
| do_fetch_versions: bool, | ||
| ) -> MailgunResult<GetTemplatesResponse> { | ||
| let url = if let Some(template_name) = template_name.clone() { | ||
| if do_fetch_versions { | ||
| format!("{}/{}/{}/{}/{}", creds.api_base, creds.domain, TEMPLATES_ENDPOINT, template_name, TEMPLATE_VERSIONS_ENDPOINT) | ||
| } else { | ||
| format!("{}/{}/{}/{}", creds.api_base, creds.domain, TEMPLATES_ENDPOINT, template_name) | ||
| } | ||
| } else { | ||
| format!("{}/{}/{}", creds.api_base, creds.domain, TEMPLATES_ENDPOINT) | ||
| }; | ||
| let request_builder = client.get(&url); | ||
| get_templates_with_request_builder(request_builder, creds, template_name).await | ||
| } | ||
|
|
||
| pub async fn get_templates_with_request_builder( | ||
| request_builder: reqwest::RequestBuilder, | ||
| creds: &Credentials, | ||
| template_name: Option<String>, | ||
| ) -> MailgunResult<GetTemplatesResponse> { | ||
| let res = request_builder | ||
| .basic_auth("api", Some(creds.api_key.clone())) | ||
| .send() | ||
| .await? | ||
| .error_for_status()?; | ||
|
|
||
| let response = if template_name.is_some() { | ||
| let parsed: GetSingleTemplateResponse = res.json().await?; | ||
| GetTemplatesResponse { | ||
| items: vec![parsed.template] | ||
| } | ||
| } else { | ||
| let parsed: GetTemplatesResponse = res.json().await?; | ||
| parsed | ||
| }; | ||
|
|
||
| Ok(response) | ||
| } | ||
|
|
||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| const DOMAIN: &str = "xxxxxxxxx.mailgun.org"; | ||
| const KEY: &str = "xxxxxxxx-yyyyyyyyy-zzzzzzzz"; | ||
|
|
||
| #[tokio::test] | ||
| async fn test_create_template() { | ||
| let creds = Credentials::new(&KEY, &DOMAIN); | ||
| let template = Template { | ||
| name: "mytemplate12".to_string(), | ||
| description: "template description12".to_string(), | ||
| template: Some("{{fname}} {{lname}}".to_string()), | ||
| tag: None, | ||
| engine: Some("handlebars".to_string()), | ||
| comment: None, | ||
| }; | ||
| let res = create_template(&creds, template).await; | ||
| println!("response = {:?}", &res); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_get_all_templates() { | ||
| let creds = Credentials::new(&KEY, &DOMAIN); | ||
| let res = get_templates(&creds, None, false).await; | ||
| println!("response = {:?}", &res); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_get_single_template_no_version() { | ||
| let creds = Credentials::new(&KEY, &DOMAIN); | ||
| let res = get_templates(&creds, Some("mytemplate12".to_string()), false).await; | ||
| println!("response = {:?}", &res); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_get_single_template_all_versions() { | ||
| let creds = Credentials::new(&KEY, &DOMAIN); | ||
| let res = get_templates(&creds, Some("mytemplate12".to_string()), true).await; | ||
| println!("response = {:?}", &res); | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we make this a single-value enum so that it's only possible to pass in
handlebarsfor now?