Skip to content

Commit a1f6939

Browse files
authored
Merge pull request #68 from xevgeny/voice-webhooks
update list delete endpoints for voice webhooks
2 parents 5ba7ea0 + 8e740ae commit a1f6939

File tree

9 files changed

+198
-66
lines changed

9 files changed

+198
-66
lines changed

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

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ ConversationWebhookList listConversationWebhooks(final int offset, final int lim
10091009
*
10101010
* @return List of webhooks.
10111011
*/
1012-
public ConversationWebhookList listConversationWebHooks() throws UnauthorizedException, GeneralException {
1012+
public ConversationWebhookList listConversationWebhooks() throws UnauthorizedException, GeneralException {
10131013
final int offset = 0;
10141014
final int limit = 10;
10151015

@@ -1273,18 +1273,14 @@ public TranscriptionResponse viewTranscription(String callID, String legId, Stri
12731273
}
12741274

12751275
/**
1276-
* Function to create web hook
1276+
* Function to create a webhook
12771277
*
1278-
* @param webhook title, url and token of webHook
1279-
* @return WebHookResponseData
1278+
* @param webhook webhook to create
1279+
* @return WebhookResponseData created webhook
12801280
* @throws UnauthorizedException if client is unauthorized
12811281
* @throws GeneralException general exception
12821282
*/
1283-
public WebhookResponseData createWebHook(Webhook webhook) throws UnauthorizedException, GeneralException {
1284-
if (webhook.getTitle() == null) {
1285-
throw new IllegalArgumentException("Title of webhook must be specified.");
1286-
}
1287-
1283+
public WebhookResponseData createWebhook(Webhook webhook) throws UnauthorizedException, GeneralException {
12881284
if (webhook.getUrl() == null) {
12891285
throw new IllegalArgumentException("URL of webhook must be specified.");
12901286
}
@@ -1294,19 +1290,74 @@ public WebhookResponseData createWebHook(Webhook webhook) throws UnauthorizedExc
12941290
}
12951291

12961292
/**
1297-
* Function to view webhook
1293+
* Function to update a webhook
1294+
*
1295+
* @param webhook webhook fields to update
1296+
* @return WebhookResponseData updated webhook
1297+
* @throws UnauthorizedException if client is unauthorized
1298+
* @throws GeneralException general exception
1299+
*/
1300+
public WebhookResponseData updateWebhook(String id, Webhook webhook) throws UnauthorizedException, GeneralException {
1301+
if (id == null) {
1302+
throw new IllegalArgumentException("Id of webhook must be specified.");
1303+
}
1304+
1305+
String url = String.format("%s%s/%s", VOICE_CALLS_BASE_URL, WEBHOOKS, id);
1306+
return messageBirdService.sendPayLoad("PUT", url, webhook, WebhookResponseData.class);
1307+
}
1308+
1309+
/**
1310+
* Function to view a webhook
12981311
*
1299-
* @param id webHook id
1300-
* @return WebHookResponseData
1312+
* @param id id of a webhook
1313+
* @return WebhookResponseData
13011314
* @throws UnauthorizedException if client is unauthorized
13021315
* @throws GeneralException general exception
13031316
*/
1304-
public WebhookResponseData viewWebHook(String id) throws NotFoundException, GeneralException, UnauthorizedException {
1317+
public WebhookResponseData viewWebhook(String id) throws NotFoundException, GeneralException, UnauthorizedException {
13051318
if (id == null) {
1306-
throw new IllegalArgumentException("Id of webHook must be specified.");
1319+
throw new IllegalArgumentException("Id of webhook must be specified.");
13071320
}
13081321

13091322
String url = String.format("%s%s", VOICE_CALLS_BASE_URL, WEBHOOKS);
13101323
return messageBirdService.requestByID(url, id, WebhookResponseData.class);
13111324
}
1325+
1326+
/**
1327+
* Function to list webhooks
1328+
*
1329+
* @param offset offset for result list
1330+
* @param limit limit for result list
1331+
* @return WebhookList
1332+
* @throws UnauthorizedException if client is unauthorized
1333+
* @throws GeneralException general exception
1334+
*/
1335+
public WebhookList listWebhooks(final Integer offset, final Integer limit) throws UnauthorizedException, GeneralException {
1336+
if (offset != null && offset < 0) {
1337+
throw new IllegalArgumentException("Offset must be > 0");
1338+
}
1339+
if (limit != null && limit < 0) {
1340+
throw new IllegalArgumentException("Limit must be > 0");
1341+
}
1342+
1343+
String url = String.format("%s%s", VOICE_CALLS_BASE_URL, WEBHOOKS);
1344+
return messageBirdService.requestList(url, offset, limit, WebhookList.class);
1345+
}
1346+
1347+
/**
1348+
* Function to delete a webhook
1349+
*
1350+
* @param id A unique random ID which is created on the MessageBird platform
1351+
* @throws NotFoundException if id is not found
1352+
* @throws GeneralException general exception
1353+
* @throws UnauthorizedException if client is unauthorized
1354+
*/
1355+
public void deleteWebhook(String id) throws NotFoundException, GeneralException, UnauthorizedException {
1356+
if (id == null) {
1357+
throw new IllegalArgumentException("Webhook ID must be specified.");
1358+
}
1359+
1360+
String url = String.format("%s%s", VOICE_CALLS_BASE_URL, WEBHOOKS);
1361+
messageBirdService.deleteByID(url, id);
1362+
}
13121363
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,18 +341,18 @@ private boolean isURLAbsolute(String url) {
341341
* Create a HttpURLConnection connection object
342342
*
343343
* @param serviceUrl URL that needs to be requested
344-
* @param postData PostDATA, must be not null for requestType is POST
344+
* @param body body could not be empty for POST or PUT requests
345345
* @param requestType Request type POST requests without a payload will generate a exception
346346
* @return base class
347347
* @throws IOException io exception
348348
*/
349-
public <P> HttpURLConnection getConnection(final String serviceUrl, final P postData, final String requestType) throws IOException {
349+
public <P> HttpURLConnection getConnection(final String serviceUrl, final P body, final String requestType) throws IOException {
350350
if (requestType == null || !REQUEST_METHODS.contains(requestType)) {
351351
throw new IllegalArgumentException(String.format(REQUEST_METHOD_NOT_ALLOWED, requestType));
352352
}
353353

354-
if (postData == null && "POST".equals(requestType)) {
355-
throw new IllegalArgumentException("POST detected without a payload, please supply a payload with a POST request");
354+
if (body == null && ("POST".equals(requestType) || "PUT".equals(requestType))) {
355+
throw new IllegalArgumentException("Empty body is not allowed for POST or PUT requests");
356356
}
357357

358358
final URL restService = new URL(serviceUrl);
@@ -370,7 +370,7 @@ public <P> HttpURLConnection getConnection(final String serviceUrl, final P post
370370
connection.setRequestProperty("Authorization", "AccessKey " + accessKey);
371371
connection.setRequestProperty("User-agent", userAgentString);
372372

373-
if ("POST".equals(requestType) || "PATCH".equals(requestType)) {
373+
if ("POST".equals(requestType) || "PUT".equals(requestType) || "PATCH".equals(requestType)) {
374374
connection.setRequestMethod(requestType);
375375
connection.setDoOutput(true);
376376
connection.setRequestProperty("Content-Type", "application/json");
@@ -383,7 +383,7 @@ public <P> HttpURLConnection getConnection(final String serviceUrl, final P post
383383
DateFormat df = getDateFormat();
384384
mapper.setDateFormat(df);
385385

386-
final String json = mapper.writeValueAsString(postData);
386+
final String json = mapper.writeValueAsString(body);
387387
connection.getOutputStream().write(json.getBytes(String.valueOf(StandardCharsets.UTF_8)));
388388
} else if ("DELETE".equals(requestType)) {
389389
// could have just used rquestType as it is

api/src/main/java/com/messagebird/objects/voicecalls/Webhook.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,9 @@ public class Webhook implements Serializable {
66

77
private static final long serialVersionUID = 727746356185518354L;
88

9-
private String title;
109
private String url;
1110
private String token;
1211

13-
public String getTitle() {
14-
return title;
15-
}
16-
17-
public void setTitle(String title) {
18-
this.title = title;
19-
}
20-
2112
public String getUrl() {
2213
return url;
2314
}
@@ -37,8 +28,7 @@ public void setToken(String token) {
3728
@Override
3829
public String toString() {
3930
return "Webhook{" +
40-
"title='" + title + '\'' +
41-
", url='" + url + '\'' +
31+
"url='" + url + '\'' +
4232
", token='" + token + '\'' +
4333
'}';
4434
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.messagebird.objects.voicecalls;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
import java.io.Serializable;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
public class WebhookList implements Serializable {
10+
11+
private static final long serialVersionUID = -5524142916135114801L;
12+
13+
private List<WebhookResponse> data;
14+
@JsonProperty("_list")
15+
private Map<String, String> links;
16+
private Pagination pagination;
17+
18+
public List<WebhookResponse> getData() {
19+
return data;
20+
}
21+
22+
public void setData(List<WebhookResponse> data) {
23+
this.data = data;
24+
}
25+
26+
public Map<String, String> getLinks() {
27+
return links;
28+
}
29+
30+
public void setLinks(Map<String, String> links) {
31+
this.links = links;
32+
}
33+
34+
public Pagination getPagination() {
35+
return pagination;
36+
}
37+
38+
public void setPagination(Pagination pagination) {
39+
this.pagination = pagination;
40+
}
41+
42+
@Override
43+
public String toString() {
44+
return "WebhookList{" +
45+
"data=" + data +
46+
", links=" + links +
47+
", pagination=" + pagination +
48+
'}';
49+
}
50+
}

api/src/test/java/com/messagebird/MessageBirdClientTest.java

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -603,34 +603,24 @@ public void testViewTranscription() throws UnauthorizedException, GeneralExcepti
603603

604604
@Test
605605
public void testCreateWebhook() throws UnauthorizedException, GeneralException {
606-
final Webhook webhook = TestUtil.createWebHook();
606+
final Webhook webhook = TestUtil.createWebhook();
607607
final WebhookResponseData webhookResponseData = TestUtil.createWebhookResponseData();
608608

609609
MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class);
610610
MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock);
611611

612612
when(messageBirdServiceMock.sendPayLoad(VOICE_CALLS_BASE_URL + WEBHOOKS, webhook, WebhookResponseData.class))
613613
.thenReturn(webhookResponseData);
614-
final WebhookResponseData response = messageBirdClientInjectMock.createWebHook(webhook);
614+
final WebhookResponseData response = messageBirdClientInjectMock.createWebhook(webhook);
615615
verify(messageBirdServiceMock, times(1)).sendPayLoad(VOICE_CALLS_BASE_URL + WEBHOOKS, webhook, WebhookResponseData.class);
616616
assertNotNull(response);
617617
assertEquals(response.getData().get(0).getId(), webhookResponseData.getData().get(0).getId());
618618
}
619619

620-
@Test(expected = IllegalArgumentException.class)
621-
public void shouldThrowIllegalArgumentExceptionWhenCreateWebhookWithMissingTitle() throws UnauthorizedException, GeneralException {
622-
final Webhook webhook = new Webhook();
623-
webhook.setUrl("ANY_URL");
624-
messageBirdClient.createWebHook(webhook);
625-
626-
}
627-
628620
@Test(expected = IllegalArgumentException.class)
629621
public void shouldThrowIllegalArgumentExceptionWhenCreateWebhookWithMissingUrl() throws UnauthorizedException, GeneralException {
630622
final Webhook webhook = new Webhook();
631-
webhook.setTitle("ANY_TITLE");
632-
messageBirdClient.createWebHook(webhook);
633-
623+
messageBirdClient.createWebhook(webhook);
634624
}
635625

636626
@Test
@@ -642,11 +632,56 @@ public void testViewWebhook() throws UnauthorizedException, GeneralException, No
642632

643633
when(messageBirdServiceMock.requestByID(VOICE_CALLS_BASE_URL + WEBHOOKS, "ANY_ID", WebhookResponseData.class))
644634
.thenReturn(webhookResponseData);
645-
final WebhookResponseData response = messageBirdClientInjectMock.viewWebHook("ANY_ID");
635+
final WebhookResponseData response = messageBirdClientInjectMock.viewWebhook("ANY_ID");
646636
verify(messageBirdServiceMock, times(1)).requestByID(VOICE_CALLS_BASE_URL + WEBHOOKS,
647637
"ANY_ID", WebhookResponseData.class);
648638
assertNotNull(response);
649639
assertEquals(response.getData().get(0).getId(), webhookResponseData.getData().get(0).getId());
650640
assertEquals(response.getData().get(0).getUrl(), webhookResponseData.getData().get(0).getUrl());
651641
}
642+
643+
@Test
644+
public void testListWebhooks() throws UnauthorizedException, GeneralException {
645+
final WebhookList webhookList = TestUtil.createWebhookList();
646+
647+
MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class);
648+
MessageBirdClient messageBirdClientMock = new MessageBirdClient(messageBirdServiceMock);
649+
650+
when(messageBirdServiceMock.requestList(anyString(), anyInt(), anyInt(), eq(WebhookList.class)))
651+
.thenReturn(webhookList);
652+
final WebhookList response = messageBirdClientMock.listWebhooks(0, 0);
653+
verify(messageBirdServiceMock, times(1))
654+
.requestList(VOICE_CALLS_BASE_URL + WEBHOOKS, 0, 0, WebhookList.class);
655+
assertNotNull(response);
656+
assertEquals(response.getData().get(0).getId(), webhookList.getData().get(0).getId());
657+
assertEquals(response.getData().get(0).getUrl(), webhookList.getData().get(0).getUrl());
658+
}
659+
660+
@Test
661+
public void testUpdateWebhook() throws UnauthorizedException, GeneralException {
662+
final Webhook webhook = TestUtil.createWebhook();
663+
final WebhookResponseData webhookResponseData = TestUtil.createWebhookResponseData();
664+
final String id = webhookResponseData.getData().get(0).getId();
665+
666+
MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class);
667+
MessageBirdClient messageBirdClientMock = new MessageBirdClient(messageBirdServiceMock);
668+
669+
String url = String.format("%s%s/%s", VOICE_CALLS_BASE_URL, WEBHOOKS, id);
670+
when(messageBirdServiceMock.sendPayLoad(anyString(), anyString(), eq(webhook), eq(WebhookResponseData.class)))
671+
.thenReturn(webhookResponseData);
672+
final WebhookResponseData response = messageBirdClientMock.updateWebhook(id, webhook);
673+
verify(messageBirdServiceMock, times(1))
674+
.sendPayLoad("PUT", url, webhook, WebhookResponseData.class);
675+
assertNotNull(response);
676+
assertEquals(response.getData().get(0).getUrl(), webhookResponseData.getData().get(0).getUrl());
677+
}
678+
679+
@Test
680+
public void testDeleteWebhook() throws NotFoundException, GeneralException, UnauthorizedException {
681+
MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class);
682+
MessageBirdClient messageBirdClientMock = new MessageBirdClient(messageBirdServiceMock);
683+
684+
messageBirdClientMock.deleteWebhook("id");
685+
verify(messageBirdServiceMock, times(1)).deleteByID(VOICE_CALLS_BASE_URL + WEBHOOKS, "id");
686+
}
652687
}

api/src/test/java/com/messagebird/TestUtil.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,8 @@ static TranscriptionResponse createTranscriptionResponse() {
9393
return transcriptionResponse;
9494
}
9595

96-
static Webhook createWebHook() {
96+
static Webhook createWebhook() {
9797
final Webhook webhook = new Webhook();
98-
webhook.setTitle("ANY_TITLE");
9998
webhook.setUrl("ANY_URL");
10099
return webhook;
101100
}
@@ -114,6 +113,13 @@ static WebhookResponseData createWebhookResponseData() {
114113
return webhookResponseData;
115114
}
116115

116+
static WebhookList createWebhookList() {
117+
final WebhookList webhookList = new WebhookList();
118+
webhookList.setData(Collections.singletonList(createWebhookResponse()));
119+
webhookList.setLinks(Collections.singletonMap("self", "ANY_ID"));
120+
return webhookList;
121+
}
122+
117123
private static Contact createContact(){
118124
final CustomDetails customDetails = new CustomDetails();
119125
customDetails.setCustom1("ANY_DETAIL");

examples/src/main/java/ExampleSendWebhook.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public class ExampleSendWebhook {
1010

1111
public static void main(String[] args) {
1212
if (args.length < 3) {
13-
System.out.println("Please specify your access key, title of webhook and url of webhook :" +
14-
" java -jar <this jar file> test_accesskey webhook_title webhook-url");
13+
System.out.println("Please specify your access key, url and token of webhook :" +
14+
" java -jar <this jar file> test_accesskey webhook-url webhook-token");
1515
return;
1616
}
1717

@@ -22,14 +22,14 @@ public static void main(String[] args) {
2222
final MessageBirdClient messageBirdClient = new MessageBirdClient(wsr);
2323

2424
try {
25-
//Creating webHook object to send client
25+
//Creating webhook object to send client
2626
System.out.println("Creating new webhook..");
2727
final Webhook webhook = new Webhook();
28-
webhook.setTitle(args[1]);
29-
webhook.setUrl(args[2]);
30-
//Sending webHook object to client
31-
final WebhookResponseData webhookResponseDataList = messageBirdClient.createWebHook(webhook);
32-
//Display webHook response
28+
webhook.setUrl(args[1]);
29+
webhook.setToken(args[2]);
30+
//Sending webhook object to client
31+
final WebhookResponseData webhookResponseDataList = messageBirdClient.createWebhook(webhook);
32+
//Display webhook response
3333
System.out.println(webhookResponseDataList.toString());
3434
} catch (GeneralException | UnauthorizedException exception) {
3535
exception.printStackTrace();

0 commit comments

Comments
 (0)