Skip to content
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
171 changes: 139 additions & 32 deletions api/src/main/java/com/messagebird/MessageBirdClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,34 @@ public TemplateList listWhatsAppTemplates(final int offset, final int limit)
return messageBirdService.requestList(url, offset, limit, TemplateList.class);
}

/**
* Gets a WhatsAppTemplate listing with specified pagination options and a wabaID or channelID filter.
*
* @param offset Number of objects to skip.
* @param limit Number of objects to take.
* @param wabaID The WABA ID to filter templates by.
* @param channelID A channel ID filter to return only templates that can be sent via that channel.
* @return List of templates.
* @throws UnauthorizedException if client is unauthorized
* @throws GeneralException general exception
* @throws IllegalArgumentException if the provided arguments are not valid
*/
public TemplateList listWhatsAppTemplates(final int offset, final int limit, final String wabaID, final String channelID)
throws UnauthorizedException, GeneralException, IllegalArgumentException {
validateWABAIDAndChannelIDArguments(wabaID, channelID);

Map<String, Object> map = new LinkedHashMap<>();
if (wabaID != null) map.put("wabaId", wabaID);
if (channelID != null) map.put("channelId", channelID);
String url = String.format(
"%s%s%s",
INTEGRATIONS_BASE_URL_V3,
INTEGRATIONS_WHATSAPP_PATH,
TEMPLATES_PATH
);
return messageBirdService.requestList(url, map, offset, limit, TemplateList.class);
}

/**
* Gets a template listing with default pagination options.
*
Expand All @@ -1913,12 +1941,13 @@ public TemplateList listWhatsAppTemplates() throws UnauthorizedException, Genera
*
* @param templateName A name as returned by getWhatsAppTemplateBy in the name variable
* @return {@code List<TemplateResponse>} template list
* @throws UnauthorizedException if client is unauthorized
* @throws GeneralException general exception
* @throws NotFoundException if template name is not found
* @throws UnauthorizedException if client is unauthorized
* @throws GeneralException general exception
* @throws NotFoundException if template name is not found
* @throws IllegalArgumentException if the provided arguments are not valid
*/
public List<TemplateResponse> getWhatsAppTemplatesBy(final String templateName)
throws GeneralException, UnauthorizedException, NotFoundException {
throws GeneralException, UnauthorizedException, NotFoundException, IllegalArgumentException {
if (templateName == null) {
throw new IllegalArgumentException("Template name must be specified.");
}
Expand All @@ -1933,19 +1962,50 @@ public List<TemplateResponse> getWhatsAppTemplatesBy(final String templateName)
return messageBirdService.requestByIdAsList(url, templateName, TemplateResponse.class);
}

/**
* Retrieves the template of an existing template name.
*
* @param templateName A name as returned by getWhatsAppTemplateBy in the name variable
* @param wabaID An optional WABA ID to look for the template ID under.
* @param channelID An optional channel ID to specify. If the template can be sent via the channel, it will return the template.
*
* @return {@code List<TemplateResponse>} template list
* @throws UnauthorizedException if client is unauthorized
* @throws GeneralException general exception
* @throws NotFoundException if template name is not found under the given WABA or cannot be sent under the supplied channel ID
* @throws IllegalArgumentException if the provided arguments are not valid
*/
public List<TemplateResponse> getWhatsAppTemplatesBy(final String templateName, final String wabaID, final String channelID)
throws GeneralException, UnauthorizedException, NotFoundException, IllegalArgumentException {
if (templateName == null) {
throw new IllegalArgumentException("Template name must be specified.");
}

String id = String.format("%s%s", templateName, getWabaIDOrChannelIDQuery(wabaID, channelID));
String url = String.format(
"%s%s%s",
INTEGRATIONS_BASE_URL_V2,
INTEGRATIONS_WHATSAPP_PATH,
TEMPLATES_PATH
);

return messageBirdService.requestByIdAsList(url, id, TemplateResponse.class);
}

/**
* Retrieves the template of an existing template name and language.
* Retrieves the template of an existing template name and language under the first waba connected to the requesting user.
*
* @param templateName A name as returned by getWhatsAppTemplateBy in the name variable
* @param language A language code as returned by getWhatsAppTemplateBy in the language variable
*
* @return {@code TemplateResponse} template list
* @return {@code TemplateResponse} template
* @throws UnauthorizedException if client is unauthorized
* @throws GeneralException general exception
* @throws NotFoundException if template name and language are not found
* @throws NotFoundException if template name and language are not found under the first waba connected to the requesting user.
* @throws IllegalArgumentException if the provided arguments are not valid
*/
public TemplateResponse fetchWhatsAppTemplateBy(final String templateName, final String language)
throws GeneralException, UnauthorizedException, NotFoundException {
throws GeneralException, UnauthorizedException, NotFoundException, IllegalArgumentException {
if (templateName == null || language == null) {
throw new IllegalArgumentException("Template name and language must be specified.");
}
Expand All @@ -1962,51 +2022,98 @@ public TemplateResponse fetchWhatsAppTemplateBy(final String templateName, final
}

/**
* Delete templates of an existing template name.
* Retrieves the template of an existing template name and language under a WABA or for a channel.
*
* @param templateName A template name which is created on the MessageBird platform
* @throws UnauthorizedException if client is unauthorized
* @throws GeneralException general exception
* @throws NotFoundException if template name is not found
* @param templateName A name as returned by getWhatsAppTemplateBy in the name variable
* @param language A language code as returned by getWhatsAppTemplateBy in the language variable
* @param wabaID An optional WABA ID to look for the template ID under.
* @param channelID An optional channel ID to specify. If the template can be sent via the channel, it will return the template.
*
* @return {@code TemplateResponse} template
* @throws UnauthorizedException if client is unauthorized
* @throws GeneralException general exception
* @throws NotFoundException if template name and language are not found under the given WABA or cannot be sent under the supplied channel ID.
* @throws IllegalArgumentException if the provided arguments are not valid
*/
public void deleteTemplatesBy(final String templateName)
throws UnauthorizedException, GeneralException, NotFoundException {
if (templateName == null) {
throw new IllegalArgumentException("Template name must be specified.");
public TemplateResponse fetchWhatsAppTemplateBy(final String templateName, final String language, final String wabaID, final String channelID)
throws GeneralException, UnauthorizedException, NotFoundException, IllegalArgumentException {
if (templateName == null || language == null) {
throw new IllegalArgumentException("Template name and language must be specified.");
}

String url = String.format(
"%s%s%s/%s",
INTEGRATIONS_BASE_URL_V2,
INTEGRATIONS_WHATSAPP_PATH,
TEMPLATES_PATH,
templateName
"%s%s%s/%s/%s%s",
INTEGRATIONS_BASE_URL_V2,
INTEGRATIONS_WHATSAPP_PATH,
TEMPLATES_PATH,
templateName,
language,
getWabaIDOrChannelIDQuery(wabaID, channelID)
);
messageBirdService.delete(url, null);
return messageBirdService.request(url, TemplateResponse.class);
}

/**
* Delete template of an existing template name and language.
* Validates the WABA ID and Channel ID argument pair.
*
* @param wabaID A WABA ID.
* @param channelID A channel ID.
* @throws IllegalArgumentException if the argument pair is invalid.
*/
private void validateWABAIDAndChannelIDArguments(String wabaID, String channelID)
throws IllegalArgumentException {
if (wabaID == null && channelID == null) {
throw new IllegalArgumentException("wabaID or channelID must be specified");
}

if (wabaID != null && channelID != null) {
throw new IllegalArgumentException("only supply wabaID or channelID - not both");
}
}

/**
* Validates the WABA ID and Channel ID argument pair and returns a valid query parameter string.
*
* @param wabaID A WABA ID.
* @param channelID A channel ID.
* @throws IllegalArgumentException if the argument pair is invalid.
*/
private String getWabaIDOrChannelIDQuery(String wabaID, String channelID)
throws IllegalArgumentException {
validateWABAIDAndChannelIDArguments(wabaID, channelID);

String query = "";

if (wabaID != null) {
query = String.format("?wabaId=%s", wabaID);
}
if (channelID != null) {
query = String.format("?channelId=%s", channelID);
}

return query;
}

/**
* Delete templates of an existing template name.
*
* @param templateName A template name which is created on the MessageBird platform
* @param language A language which is created on the MessageBird platform
* @throws UnauthorizedException if client is unauthorized
* @throws GeneralException general exception
* @throws NotFoundException if template name or language are not found
* @throws NotFoundException if template name is not found
*/
public void deleteTemplatesBy(final String templateName, final String language)
public void deleteTemplatesBy(final String templateName)
throws UnauthorizedException, GeneralException, NotFoundException {
if (templateName == null || language == null) {
throw new IllegalArgumentException("Template name and language must be specified.");
if (templateName == null) {
throw new IllegalArgumentException("Template name must be specified.");
}

String url = String.format(
"%s%s%s/%s/%s",
"%s%s%s/%s",
INTEGRATIONS_BASE_URL_V2,
INTEGRATIONS_WHATSAPP_PATH,
TEMPLATES_PATH,
templateName,
language
templateName
);
messageBirdService.delete(url, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ public class Template {

private String name;
private String language;
private String wabaID;
private List<HSMComponent> components;
private HSMCategory category;

public Template() {
}

public Template(String name, String language,
List<HSMComponent> components, HSMCategory category) {
public Template(String name, String language, String wabaID,
List<HSMComponent> components, HSMCategory category) {
this.name = name;
this.language = language;
this.wabaID = wabaID;
this.components = components;
this.category = category;
}
Expand All @@ -42,6 +44,14 @@ public void setLanguage(String language) {
this.language = language;
}

public String getWABAID() {
return wabaID;
}

public void setWABAID(String wabaID) {
this.wabaID = wabaID;
}

public List<HSMComponent> getComponents() {
return components;
}
Expand All @@ -63,6 +73,7 @@ public String toString() {
return "WhatsAppTemplate{" +
"name='" + name + '\'' +
", language='" + language + '\'' +
", wabaID='" + wabaID + '\'' +
", components=" + components +
", category='" + category + '\'' +
'}';
Expand All @@ -77,6 +88,7 @@ public void validate() throws IllegalArgumentException {
this.validateComponents();
this.validateName();
this.validateLanguage();
this.validateWABAID();
this.validateCategory();
}

Expand Down Expand Up @@ -123,6 +135,19 @@ private void validateLanguage() {
}
}

/**
* Check if wabaID field is valid.
*
* @throws IllegalArgumentException If wabaID field is null or empty string.
*/
private void validateWABAID() {
if (this.wabaID == null) {
throw new IllegalArgumentException("A \"wabaID\" field is required.");
} else if (this.wabaID.length() == 0) {
throw new IllegalArgumentException("A \"wabaID\" field can not be an empty string.");
}
}

/**
* Check if category field is valid.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class TemplateResponse implements Serializable {
private List<HSMComponent> components;
private HSMStatus status;
private String rejectedReason;
private String wabaID;
private String namespace;
private Date createdAt;
private Date updatedAt;

Expand Down Expand Up @@ -73,6 +75,22 @@ public void setRejectedReason(String rejectedReason) {
this.rejectedReason = rejectedReason;
}

public String getWabaID() {
return wabaID;
}

public void setWabaID(String wabaID) {
this.wabaID = wabaID;
}

public String getNamespace() {
return namespace;
}

public void setNamespace(String namespace) {
this.namespace = namespace;
}

public Date getCreatedAt() {
return createdAt;
}
Expand All @@ -98,6 +116,8 @@ public String toString() {
", components=" + components +
", status='" + status + '\'' +
", rejectedReason='" + rejectedReason + '\'' +
", wabaID='" + wabaID + '\'' +
", namespace='" + namespace + '\'' +
", createdAt=" + createdAt +
", updatedAt=" + updatedAt +
'}';
Expand Down
Loading