Skip to content
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

Add email-templates endpoints #117

Merged
merged 7 commits into from
Jun 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ The Management API is divided into different entities. Each of them have the lis
* **User Blocks:** See [Docs](https://auth0.com/docs/api/management/v2#!/User_Blocks/get_user_blocks). Access the methods by calling `mgmt.userBlocks()`.
* **Users:** See [this](https://auth0.com/docs/api/management/v2#!/Users/get_users) and [this](https://auth0.com/docs/api/management/v2#!/Users_By_Email) doc. Access the methods by calling `mgmt.users()`.
* **Blacklists:** See [Docs](https://auth0.com/docs/api/management/v2#!/Blacklists/get_tokens). Access the methods by calling `mgmt.blacklists()`.
* **Emails:** See [Docs](https://auth0.com/docs/api/management/v2#!/Emails/get_provider). Access the methods by calling `mgmt.emailProvider()`.
* **Email Providers:** See [Docs](https://auth0.com/docs/api/management/v2#!/Emails/get_provider). Access the methods by calling `mgmt.emailProvider()`.
* **Email Templates:** See [Docs](https://auth0.com/docs/api/management/v2#!/Email_Templates/get_email_templates_by_templateName). Access the methods by calling `mgmt.emailTemplates()`.
* **Guardian:** See [Docs](https://auth0.com/docs/api/management/v2#!/Guardian/get_factors). Access the methods by calling `mgmt.guardian()`.
* **Stats:** See [Docs](https://auth0.com/docs/api/management/v2#!/Stats/get_active_users). Access the methods by calling `mgmt.stats()`.
* **Tenants:** See [Docs](https://auth0.com/docs/api/management/v2#!/Tenants/get_settings). Access the methods by calling `mgmt.tenants()`.
Expand Down
97 changes: 97 additions & 0 deletions src/main/java/com/auth0/client/mgmt/EmailTemplatesEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.auth0.client.mgmt;

import com.auth0.json.mgmt.EmailTemplate;
import com.auth0.net.CustomRequest;
import com.auth0.net.Request;
import com.auth0.utils.Asserts;
import com.fasterxml.jackson.core.type.TypeReference;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;

/**
* Class that provides an implementation of the Email Templates methods of the Management API as defined in https://auth0.com/docs/api/management/v2#!/Email_Templates
*/
@SuppressWarnings({"WeakerAccess", "unused"})
public class EmailTemplatesEntity extends BaseManagementEntity {

public static final String TEMPLATE_VERIFY_EMAIL = "verify_email";
public static final String TEMPLATE_RESET_EMAIL = "reset_email";
public static final String TEMPLATE_WELCOME_EMAIL = "welcome_email";
public static final String TEMPLATE_BLOCKED_ACCOUNT = "blocked_account";
public static final String TEMPLATE_STOLEN_CREDENTIALS = "stolen_credentials";
public static final String TEMPLATE_ENROLLMENT_EMAIL = "enrollment_email";
public static final String TEMPLATE_CHANGE_PASSWORD = "change_password";
public static final String TEMPLATE_PASSWORD_RESET = "password_reset";
public static final String TEMPLATE_MFA_OOB_CODE = "mfa_oob_code";

EmailTemplatesEntity(OkHttpClient client, HttpUrl baseUrl, String apiToken) {
super(client, baseUrl, apiToken);
}

/**
* Request the Email Templates. A token with scope read:email_templates is needed.
* See https://auth0.com/docs/api/management/v2#!/Email_Templates/get_email_templates_by_templateName
*
* @param templateName the template name to request. You can use any of the constants defined in {@link EmailTemplatesEntity}
* @return a Request to execute.
*/
public Request<EmailTemplate> get(String templateName) {
Asserts.assertNotNull(templateName, "template name");
HttpUrl.Builder builder = baseUrl
.newBuilder()
.addPathSegments("api/v2/email-templates")
.addPathSegment(templateName);
String url = builder.build().toString();
CustomRequest<EmailTemplate> request = new CustomRequest<>(client, url, "GET", new TypeReference<EmailTemplate>() {
});
request.addHeader("Authorization", "Bearer " + apiToken);
return request;
}

/**
* Create an Email Template. A token with scope create:email_templates is needed.
* See https://auth0.com/docs/api/management/v2#!/Email_Templates/post_email_templates
*
* @param template the template data to set
* @return a Request to execute.
*/
public Request<EmailTemplate> create(EmailTemplate template) {
Asserts.assertNotNull(template, "template");

String url = baseUrl
.newBuilder()
.addPathSegments("api/v2/email-templates")
.build()
.toString();
CustomRequest<EmailTemplate> request = new CustomRequest<>(this.client, url, "POST", new TypeReference<EmailTemplate>() {
});
request.addHeader("Authorization", "Bearer " + apiToken);
request.setBody(template);
return request;
}

/**
* Patches the existing Email Template. A token with scope update:email_templates is needed.
* See https://auth0.com/docs/api/management/v2#!/Email_Templates/patch_email_templates_by_templateName
*
* @param templateName the name of the template to update. You can use any of the constants defined in {@link EmailTemplatesEntity}
* @param template the email template data to set.
* @return a Request to execute.
*/
public Request<EmailTemplate> update(String templateName, EmailTemplate template) {
Asserts.assertNotNull(templateName, "template name");
Asserts.assertNotNull(template, "template");

String url = baseUrl
.newBuilder()
.addPathSegments("api/v2/email-templates")
.addPathSegment(templateName)
.build()
.toString();
CustomRequest<EmailTemplate> request = new CustomRequest<>(this.client, url, "PATCH", new TypeReference<EmailTemplate>() {
});
request.addHeader("Authorization", "Bearer " + apiToken);
request.setBody(template);
return request;
}
}
11 changes: 10 additions & 1 deletion src/main/java/com/auth0/client/mgmt/ManagementAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ public BlacklistsEntity blacklists() {
return new BlacklistsEntity(client, baseUrl, apiToken);
}

/**
* Getter for the Email Templates entity.
*
* @return the Email Templates entity.
*/
public EmailTemplatesEntity emailTemplates() {
return new EmailTemplatesEntity(client, baseUrl, apiToken);
}

/**
* Getter for the Email Provider entity.
*
Expand Down Expand Up @@ -219,7 +228,7 @@ public TicketsEntity tickets() {
*
* @return the Resource Servers entity.
*/
public ResourceServerEntity resourceServers(){
public ResourceServerEntity resourceServers() {
return new ResourceServerEntity(client, baseUrl, apiToken);
}
}
196 changes: 196 additions & 0 deletions src/main/java/com/auth0/json/mgmt/EmailTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
package com.auth0.json.mgmt;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Class that represents an Email Template object. Related to the {@link com.auth0.client.mgmt.EmailTemplatesEntity} entity.
*/
@SuppressWarnings({"unused", "WeakerAccess"})
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class EmailTemplate {

@JsonProperty("template")
private String name;
@JsonProperty("body")
private String body;
@JsonProperty("from")
private String from;
@JsonProperty("resultUrl")
private String resultUrl;
@JsonProperty("subject")
private String subject;
@JsonProperty("syntax")
private String syntax;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be an enum for additional validation? Although only supported value is liquid

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HMMM. I discussed this with the server guys. The only allowed value is liquid but they say the option is only there for backwards compatibility, so we shouldn't be hardcoding it to liquid and lowering the param count for example. I don't think offering an enum/constant helps but I don't mind adding one. Would you prefer me to add it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you set the param default to liquid ? Then they don't need to think about it but in the future if they need to, they can change.

@JsonProperty("urlLifetimeInSeconds")
private Integer urlLifetimeInSeconds;
@JsonProperty("enabled")
private Boolean enabled;

public EmailTemplate() {
//Only here to set the syntax default value
this.syntax = "liquid";
}

/**
* Getter for the name of the template.
*
* @return the name.
*/
@JsonProperty("template")
public String getName() {
return name;
}

/**
* Sets the name of the template.
*/
@JsonProperty("template")
public void setName(String name) {
this.name = name;
}

/**
* Getter for the template code.
*
* @return the template code.
*/
@JsonProperty("body")
public String getBody() {
return body;
}

/**
* Sets the template code
*
* @param body the code this template will have
*/
@JsonProperty("body")
public void setBody(String body) {
this.body = body;
}

/**
* Getter for the sender of the email
*
* @return the sender of the email
*/
@JsonProperty("from")
public String getFrom() {
return from;
}

/**
* Sets the sender of the email
*
* @param from the sender of the email
*/
@JsonProperty("from")
public void setFrom(String from) {
this.from = from;
}

/**
* Getter the URL to redirect the user to after a successful action.
*
* @return the URL to redirect the user to after a successful action.
*/
@JsonProperty("resultUrl")
public String getResultUrl() {
return resultUrl;
}

/**
* Sets the URL to redirect the user to after a successful action.
*
* @param resultUrl the URL to redirect the user to after a successful action.
*/
@JsonProperty("resultUrl")
public void setResultUrl(String resultUrl) {
this.resultUrl = resultUrl;
}

/**
* Getter for the subject of the email.
*
* @return the subject of the email.
*/
@JsonProperty("subject")
public String getSubject() {
return subject;
}

/**
* Sets the subject of the email.
*
* @param subject the subject of the email.
*/
@JsonProperty("subject")
public void setSubject(String subject) {
this.subject = subject;
}

/**
* Getter for the syntax used in the template's code.
*
* @return the syntax used in the template's code.
*/
@JsonProperty("syntax")
public String getSyntax() {
return syntax;
}

/**
* Sets for the syntax to be used in the template's code.
* Default value is 'liquid'
*
* @param syntax the syntax to be used in the template's code.
*/
@JsonProperty("syntax")
public void setSyntax(String syntax) {
this.syntax = syntax;
}

/**
* Getter for the lifetime in seconds that the link within the email will be valid for.
*
* @return the lifetime in seconds that the link within the email will be valid for.
*/
@JsonProperty("urlLifetimeInSeconds")
public Integer getUrlLifetimeInSeconds() {
return urlLifetimeInSeconds;
}

/**
* Sets the lifetime in seconds that the link within the email will be valid for.
*
* @param urlLifetimeInSeconds the lifetime in seconds that the link within the email will be valid for.
*/
@JsonProperty("urlLifetimeInSeconds")
public void setUrlLifetimeInSeconds(Integer urlLifetimeInSeconds) {
this.urlLifetimeInSeconds = urlLifetimeInSeconds;
}

/**
* Whether or not this template is enabled.
*
* @return true if this template is enabled, false otherwise.
*/
@JsonProperty("enabled")
public Boolean isEnabled() {
return enabled;
}

/**
* Enables or disables this template.
*
* @param enabled whether this template is enabled or not.
*/
@JsonProperty("enabled")
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}

}
1 change: 1 addition & 0 deletions src/test/java/com/auth0/client/MockServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class MockServer {
public static final String MGMT_USER_BLOCKS = "src/test/resources/mgmt/user_blocks.json";
public static final String MGMT_BLACKLISTED_TOKENS_LIST = "src/test/resources/mgmt/blacklisted_tokens_list.json";
public static final String MGMT_EMAIL_PROVIDER = "src/test/resources/mgmt/email_provider.json";
public static final String MGMT_EMAIL_TEMPLATE = "src/test/resources/mgmt/email_template.json";
public static final String MGMT_USERS_LIST = "src/test/resources/mgmt/users_list.json";
public static final String MGMT_USERS_PAGED_LIST = "src/test/resources/mgmt/users_paged_list.json";
public static final String MGMT_USER = "src/test/resources/mgmt/user.json";
Expand Down
Loading