Skip to content

Commit 434c092

Browse files
authored
Merge pull request #160 from ssk910/master
feat: Add features for integrations API
2 parents d6891ae + cbac784 commit 434c092

21 files changed

+1326
-20
lines changed

api/src/main/java/com/messagebird/MessageBirdClient.java

Lines changed: 175 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
import com.messagebird.objects.PhoneNumbersResponse;
2525
import com.messagebird.objects.PurchasedNumber;
2626
import com.messagebird.objects.PurchasedNumberCreatedResponse;
27-
import com.messagebird.objects.PurchasedNumbersResponse;
2827
import com.messagebird.objects.PurchasedNumbersFilter;
28+
import com.messagebird.objects.PurchasedNumbersResponse;
2929
import com.messagebird.objects.Verify;
3030
import com.messagebird.objects.VerifyMessage;
3131
import com.messagebird.objects.VerifyRequest;
@@ -46,6 +46,9 @@
4646
import com.messagebird.objects.conversations.ConversationWebhookCreateRequest;
4747
import com.messagebird.objects.conversations.ConversationWebhookList;
4848
import com.messagebird.objects.conversations.ConversationWebhookUpdateRequest;
49+
import com.messagebird.objects.integrations.WhatsAppTemplate;
50+
import com.messagebird.objects.integrations.WhatsAppTemplateList;
51+
import com.messagebird.objects.integrations.WhatsAppTemplateResponse;
4952
import com.messagebird.objects.voicecalls.RecordingResponse;
5053
import com.messagebird.objects.voicecalls.TranscriptionResponse;
5154
import com.messagebird.objects.voicecalls.VoiceCall;
@@ -59,20 +62,19 @@
5962
import com.messagebird.objects.voicecalls.Webhook;
6063
import com.messagebird.objects.voicecalls.WebhookList;
6164
import com.messagebird.objects.voicecalls.WebhookResponseData;
62-
6365
import java.io.UnsupportedEncodingException;
6466
import java.math.BigInteger;
65-
import java.nio.charset.StandardCharsets;
6667
import java.net.URLEncoder;
68+
import java.nio.charset.StandardCharsets;
6769
import java.util.Arrays;
6870
import java.util.HashMap;
71+
import java.util.HashSet;
6972
import java.util.LinkedHashMap;
7073
import java.util.LinkedList;
7174
import java.util.List;
7275
import java.util.Locale;
7376
import java.util.Map;
7477
import java.util.Set;
75-
import java.util.HashSet;
7678

7779
/**
7880
* Message bird general client
@@ -102,6 +104,8 @@ public class MessageBirdClient {
102104
static final String VOICE_CALLS_BASE_URL = "https://voice.messagebird.com";
103105
static final String NUMBERS_CALLS_BASE_URL = "https://numbers.messagebird.com/v1";
104106
static final String MESSAGING_BASE_URL = "https://messaging.messagebird.com/v1";
107+
static final String INTEGRATIONS_BASE_URL_V2 = "https://integrations.messagebird.com/v2";
108+
static final String INTEGRATIONS_BASE_URL_V3 = "https://integrations.messagebird.com/v3";
105109
private static String[] supportedLanguages = {"de-DE", "en-AU", "en-UK", "en-US", "es-ES", "es-LA", "fr-FR", "it-IT", "nl-NL", "pt-BR"};
106110

107111
private static final String BALANCEPATH = "/balance";
@@ -118,6 +122,7 @@ public class MessageBirdClient {
118122
private static final String CONVERSATION_SEND_PATH = "/send";
119123
private static final String CONVERSATION_MESSAGE_PATH = "/messages";
120124
private static final String CONVERSATION_WEBHOOK_PATH = "/webhooks";
125+
private static final String INTEGRATIONS_WHATSAPP_PATH = "/platforms/whatsapp";
121126
static final String VOICECALLSPATH = "/calls";
122127
static final String LEGSPATH = "/legs";
123128
static final String RECORDINGPATH = "/recordings";
@@ -126,6 +131,7 @@ public class MessageBirdClient {
126131
static final String VOICECALLFLOWPATH = "/call-flows";
127132
private static final String VOICELEGS_SUFFIX_PATH = "/legs";
128133
static final String FILES_PATH = "/files";
134+
static final String TEMPLATES_PATH = "/templates";
129135

130136
static final String RECORDING_DOWNLOAD_FORMAT = ".wav";
131137

@@ -1848,4 +1854,168 @@ public String downloadFile(String id, String filename, String basePath) throws G
18481854
final String url = String.format("%s%s/%s", MESSAGING_BASE_URL, FILES_PATH, id);
18491855
return messageBirdService.getBinaryData(url, basePath, filename);
18501856
}
1851-
}
1857+
1858+
/****************************************************************************************************/
1859+
/** WhatsApp Templates **/
1860+
/****************************************************************************************************/
1861+
1862+
/**
1863+
* Create a WhatsApp message template through messagebird.
1864+
*
1865+
* @param template {@link WhatsAppTemplate} object to be created
1866+
* @return {@link WhatsAppTemplateResponse} response object
1867+
* @throws UnauthorizedException if client is unauthorized
1868+
* @throws GeneralException general exception or invalid template format
1869+
*/
1870+
public WhatsAppTemplateResponse createWhatsAppTemplate(final WhatsAppTemplate template)
1871+
throws UnauthorizedException, GeneralException {
1872+
template.validate();
1873+
1874+
String url = String.format(
1875+
"%s%s%s",
1876+
INTEGRATIONS_BASE_URL_V2,
1877+
INTEGRATIONS_WHATSAPP_PATH,
1878+
TEMPLATES_PATH
1879+
);
1880+
return messageBirdService.sendPayLoad(url, template, WhatsAppTemplateResponse.class);
1881+
}
1882+
1883+
/**
1884+
* Gets a WhatsAppTemplate listing with specified pagination options.
1885+
*
1886+
* @param offset Number of objects to skip.
1887+
* @param limit Number of objects to take.
1888+
* @return List of templates.
1889+
* @throws UnauthorizedException if client is unauthorized
1890+
* @throws GeneralException general exception
1891+
*/
1892+
public WhatsAppTemplateList listWhatsAppTemplates(final int offset, final int limit)
1893+
throws UnauthorizedException, GeneralException {
1894+
String url = String.format(
1895+
"%s%s%s",
1896+
INTEGRATIONS_BASE_URL_V3,
1897+
INTEGRATIONS_WHATSAPP_PATH,
1898+
TEMPLATES_PATH
1899+
);
1900+
return messageBirdService.requestList(url, offset, limit, WhatsAppTemplateList.class);
1901+
}
1902+
1903+
/**
1904+
* Gets a template listing with default pagination options.
1905+
*
1906+
* @return List of whatsapp templates.
1907+
* @throws UnauthorizedException if client is unauthorized
1908+
* @throws GeneralException general exception
1909+
*/
1910+
public WhatsAppTemplateList listWhatsAppTemplates() throws UnauthorizedException, GeneralException {
1911+
final int offset = 0;
1912+
final int limit = 10;
1913+
1914+
return listWhatsAppTemplates(offset, limit);
1915+
}
1916+
1917+
/**
1918+
* Retrieves the template of an existing template name.
1919+
*
1920+
* @param templateName A name as returned by getWhatsAppTemplateBy in the name variable
1921+
* @return {@code List<WhatsAppTemplateResponse>} template list
1922+
* @throws UnauthorizedException if client is unauthorized
1923+
* @throws GeneralException general exception
1924+
* @throws NotFoundException if template name is not found
1925+
*/
1926+
public List<WhatsAppTemplateResponse> getWhatsAppTemplatesBy(final String templateName)
1927+
throws GeneralException, UnauthorizedException, NotFoundException {
1928+
if (templateName == null) {
1929+
throw new IllegalArgumentException("Template name must be specified.");
1930+
}
1931+
1932+
String url = String.format(
1933+
"%s%s%s",
1934+
INTEGRATIONS_BASE_URL_V2,
1935+
INTEGRATIONS_WHATSAPP_PATH,
1936+
TEMPLATES_PATH
1937+
);
1938+
1939+
final WhatsAppTemplateResponse[] templateResponses = messageBirdService.requestByID(url, templateName, WhatsAppTemplateResponse[].class);
1940+
return Arrays.asList(templateResponses);
1941+
}
1942+
1943+
/**
1944+
* Retrieves the template of an existing template name and language.
1945+
*
1946+
* @param templateName A name as returned by getWhatsAppTemplateBy in the name variable
1947+
* @param language A language code as returned by getWhatsAppTemplateBy in the language variable
1948+
*
1949+
* @return {@code WhatsAppTemplateResponse} template list
1950+
* @throws UnauthorizedException if client is unauthorized
1951+
* @throws GeneralException general exception
1952+
* @throws NotFoundException if template name and language are not found
1953+
*/
1954+
public WhatsAppTemplateResponse fetchWhatsAppTemplateBy(final String templateName, final String language)
1955+
throws GeneralException, UnauthorizedException, NotFoundException {
1956+
if (templateName == null || language == null) {
1957+
throw new IllegalArgumentException("Template name and language must be specified.");
1958+
}
1959+
1960+
String url = String.format(
1961+
"%s%s%s/%s/%s",
1962+
INTEGRATIONS_BASE_URL_V2,
1963+
INTEGRATIONS_WHATSAPP_PATH,
1964+
TEMPLATES_PATH,
1965+
templateName,
1966+
language
1967+
);
1968+
return messageBirdService.request(url, WhatsAppTemplateResponse.class);
1969+
}
1970+
1971+
1972+
/**
1973+
* Delete templates of an existing template name.
1974+
*
1975+
* @param templateName A template name which is created on the MessageBird platform
1976+
* @throws UnauthorizedException if client is unauthorized
1977+
* @throws GeneralException general exception
1978+
* @throws NotFoundException if template name is not found
1979+
*/
1980+
public void deleteTemplatesBy(final String templateName)
1981+
throws UnauthorizedException, GeneralException, NotFoundException {
1982+
if (templateName == null) {
1983+
throw new IllegalArgumentException("Template name must be specified.");
1984+
}
1985+
1986+
String url = String.format(
1987+
"%s%s%s/%s",
1988+
INTEGRATIONS_BASE_URL_V2,
1989+
INTEGRATIONS_WHATSAPP_PATH,
1990+
TEMPLATES_PATH,
1991+
templateName
1992+
);
1993+
messageBirdService.delete(url, null);
1994+
}
1995+
1996+
/**
1997+
* Delete template of an existing template name and language.
1998+
*
1999+
* @param templateName A template name which is created on the MessageBird platform
2000+
* @param language A language which is created on the MessageBird platform
2001+
* @throws UnauthorizedException if client is unauthorized
2002+
* @throws GeneralException general exception
2003+
* @throws NotFoundException if template name or language are not found
2004+
*/
2005+
public void deleteTemplatesBy(final String templateName, final String language)
2006+
throws UnauthorizedException, GeneralException, NotFoundException {
2007+
if (templateName == null || language == null) {
2008+
throw new IllegalArgumentException("Template name and language must be specified.");
2009+
}
2010+
2011+
String url = String.format(
2012+
"%s%s%s/%s/%s",
2013+
INTEGRATIONS_BASE_URL_V2,
2014+
INTEGRATIONS_WHATSAPP_PATH,
2015+
TEMPLATES_PATH,
2016+
templateName,
2017+
language
2018+
);
2019+
messageBirdService.delete(url, null);
2020+
}
2021+
}

api/src/main/java/com/messagebird/MessageBirdService.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
package com.messagebird;
22

3-
import java.util.Map;
4-
53
import com.messagebird.exceptions.GeneralException;
64
import com.messagebird.exceptions.NotFoundException;
75
import com.messagebird.exceptions.UnauthorizedException;
86
import com.messagebird.objects.PagedPaging;
7+
import java.util.Map;
98

109
/**
1110
* Created by rvt on 1/7/15.
1211
*/
1312
public interface MessageBirdService {
13+
14+
/**
15+
* Send GET request . It will retrieve a json object R back.
16+
*
17+
* @author ssk910
18+
* @param request path to the request, for example "/messages/id/language"
19+
* @param clazz Class type to return
20+
* @return new class with returned dataset
21+
* @throws UnauthorizedException if client is unauthorized
22+
* @throws GeneralException general exception
23+
* @throws NotFoundException if id not found
24+
*/
25+
<R> R request(String request, Class<R> clazz) throws UnauthorizedException, GeneralException, NotFoundException;
26+
1427
/**
1528
* Execute a object by ID request. It will add the id to the request parameter and retreive a json object R back.
1629
*
@@ -36,6 +49,19 @@ public interface MessageBirdService {
3649
*/
3750
void deleteByID(String request, String id) throws UnauthorizedException, GeneralException, NotFoundException;
3851

52+
/**
53+
* Send DELETE request. It will retrieve a json object R back.
54+
*
55+
* @author ssk910
56+
* @param request path to the request, for example "/messages/id/language"
57+
* @param clazz Class type to return
58+
* @return class with returned dataset
59+
* @throws UnauthorizedException if client is unauthorized
60+
* @throws GeneralException general exception
61+
* @throws NotFoundException if id not found
62+
*/
63+
<R> R delete(String request, Class<R> clazz) throws UnauthorizedException, GeneralException, NotFoundException;
64+
3965
/**
4066
* Request a List 'of' object.
4167
* Allow to request a listMessage or listViewMessages objects.

api/src/main/java/com/messagebird/MessageBirdServiceImpl.java

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
package com.messagebird;
22

3-
import java.io.*;
4-
import java.lang.reflect.Field;
5-
import java.lang.reflect.Modifier;
6-
import java.net.HttpURLConnection;
7-
import java.net.Proxy;
8-
import java.net.URL;
9-
import java.net.URLEncoder;
10-
import java.nio.charset.StandardCharsets;
11-
import java.text.DateFormat;
12-
import java.text.SimpleDateFormat;
13-
import java.util.*;
14-
153
import com.fasterxml.jackson.annotation.JsonInclude.Include;
164
import com.fasterxml.jackson.databind.DeserializationFeature;
175
import com.fasterxml.jackson.databind.JsonNode;
@@ -22,6 +10,29 @@
2210
import com.messagebird.exceptions.UnauthorizedException;
2311
import com.messagebird.objects.ErrorReport;
2412
import com.messagebird.objects.PagedPaging;
13+
import java.io.File;
14+
import java.io.FileOutputStream;
15+
import java.io.IOException;
16+
import java.io.InputStream;
17+
import java.io.UnsupportedEncodingException;
18+
import java.lang.reflect.Field;
19+
import java.lang.reflect.Modifier;
20+
import java.net.HttpURLConnection;
21+
import java.net.Proxy;
22+
import java.net.URL;
23+
import java.net.URLEncoder;
24+
import java.nio.charset.StandardCharsets;
25+
import java.text.DateFormat;
26+
import java.text.SimpleDateFormat;
27+
import java.util.ArrayList;
28+
import java.util.Arrays;
29+
import java.util.Collection;
30+
import java.util.HashMap;
31+
import java.util.Iterator;
32+
import java.util.LinkedHashMap;
33+
import java.util.List;
34+
import java.util.Map;
35+
import java.util.Scanner;
2536

2637
/**
2738
* Implementation of MessageBirdService
@@ -98,6 +109,12 @@ public MessageBirdServiceImpl(final String accessKey) {
98109
this(accessKey, "https://rest.messagebird.com");
99110
}
100111

112+
@Override
113+
public <R> R request(String request, Class<R> clazz)
114+
throws UnauthorizedException, GeneralException, NotFoundException {
115+
return getJsonData(request, null, "GET", clazz);
116+
}
117+
101118
@Override
102119
public <R> R requestByID(String request, String id, Class<R> clazz) throws UnauthorizedException, GeneralException, NotFoundException {
103120
String path = "";
@@ -126,6 +143,11 @@ public void deleteByID(String request, String id) throws UnauthorizedException,
126143
getJsonData(request + "/" + id, null, "DELETE", null);
127144
}
128145

146+
@Override
147+
public <R> R delete(String request, Class<R> clazz) throws UnauthorizedException, GeneralException, NotFoundException {
148+
return getJsonData(request, null, "DELETE", clazz);
149+
}
150+
129151
@Override
130152
public <R> R requestList(String request, Integer offset, Integer limit, Class<R> clazz) throws UnauthorizedException, GeneralException {
131153
Map<String, Object> map = new LinkedHashMap<>();
@@ -235,7 +257,10 @@ public <T, P> T getJsonData(final String request, final P payload, final String
235257
mapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);
236258
mapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS);
237259

238-
return mapper.readValue(body, clazz);
260+
// Prevents mismatched exception when clazz is null
261+
return clazz == null
262+
? null
263+
: mapper.readValue(body, clazz);
239264
} catch (IOException ioe) {
240265
throw new GeneralException(ioe);
241266
}

0 commit comments

Comments
 (0)