Skip to content

Voice webhooks #68

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

Merged
merged 10 commits into from
Sep 25, 2019
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
79 changes: 65 additions & 14 deletions api/src/main/java/com/messagebird/MessageBirdClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ ConversationWebhookList listConversationWebhooks(final int offset, final int lim
*
* @return List of webhooks.
*/
public ConversationWebhookList listConversationWebHooks() throws UnauthorizedException, GeneralException {
public ConversationWebhookList listConversationWebhooks() throws UnauthorizedException, GeneralException {
final int offset = 0;
final int limit = 10;

Expand Down Expand Up @@ -1273,18 +1273,14 @@ public TranscriptionResponse viewTranscription(String callID, String legId, Stri
}

/**
* Function to create web hook
* Function to create a webhook
*
* @param webhook title, url and token of webHook
* @return WebHookResponseData
* @param webhook webhook to create
* @return WebhookResponseData created webhook
* @throws UnauthorizedException if client is unauthorized
* @throws GeneralException general exception
*/
public WebhookResponseData createWebHook(Webhook webhook) throws UnauthorizedException, GeneralException {
if (webhook.getTitle() == null) {
throw new IllegalArgumentException("Title of webhook must be specified.");
}

public WebhookResponseData createWebhook(Webhook webhook) throws UnauthorizedException, GeneralException {
if (webhook.getUrl() == null) {
throw new IllegalArgumentException("URL of webhook must be specified.");
}
Expand All @@ -1294,19 +1290,74 @@ public WebhookResponseData createWebHook(Webhook webhook) throws UnauthorizedExc
}

/**
* Function to view webhook
* Function to update a webhook
*
* @param webhook webhook fields to update
* @return WebhookResponseData updated webhook
* @throws UnauthorizedException if client is unauthorized
* @throws GeneralException general exception
*/
public WebhookResponseData updateWebhook(String id, Webhook webhook) throws UnauthorizedException, GeneralException {
if (id == null) {
throw new IllegalArgumentException("Id of webhook must be specified.");
}

String url = String.format("%s%s/%s", VOICE_CALLS_BASE_URL, WEBHOOKS, id);
return messageBirdService.sendPayLoad("PUT", url, webhook, WebhookResponseData.class);
}

/**
* Function to view a webhook
*
* @param id webHook id
* @return WebHookResponseData
* @param id id of a webhook
* @return WebhookResponseData
* @throws UnauthorizedException if client is unauthorized
* @throws GeneralException general exception
*/
public WebhookResponseData viewWebHook(String id) throws NotFoundException, GeneralException, UnauthorizedException {
public WebhookResponseData viewWebhook(String id) throws NotFoundException, GeneralException, UnauthorizedException {
if (id == null) {
throw new IllegalArgumentException("Id of webHook must be specified.");
throw new IllegalArgumentException("Id of webhook must be specified.");
}

String url = String.format("%s%s", VOICE_CALLS_BASE_URL, WEBHOOKS);
return messageBirdService.requestByID(url, id, WebhookResponseData.class);
}

/**
* Function to list webhooks
*
* @param offset offset for result list
* @param limit limit for result list
* @return WebhookList
* @throws UnauthorizedException if client is unauthorized
* @throws GeneralException general exception
*/
public WebhookList listWebhooks(final Integer offset, final Integer limit) throws UnauthorizedException, GeneralException {
if (offset != null && offset < 0) {
throw new IllegalArgumentException("Offset must be > 0");
}
if (limit != null && limit < 0) {
throw new IllegalArgumentException("Limit must be > 0");
}

String url = String.format("%s%s", VOICE_CALLS_BASE_URL, WEBHOOKS);
return messageBirdService.requestList(url, offset, limit, WebhookList.class);
}

/**
* Function to delete a webhook
*
* @param id A unique random ID which is created on the MessageBird platform
* @throws NotFoundException if id is not found
* @throws GeneralException general exception
* @throws UnauthorizedException if client is unauthorized
*/
public void deleteWebhook(String id) throws NotFoundException, GeneralException, UnauthorizedException {
if (id == null) {
throw new IllegalArgumentException("Webhook ID must be specified.");
}

String url = String.format("%s%s", VOICE_CALLS_BASE_URL, WEBHOOKS);
messageBirdService.deleteByID(url, id);
}
}
12 changes: 6 additions & 6 deletions api/src/main/java/com/messagebird/MessageBirdServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -341,18 +341,18 @@ private boolean isURLAbsolute(String url) {
* Create a HttpURLConnection connection object
*
* @param serviceUrl URL that needs to be requested
* @param postData PostDATA, must be not null for requestType is POST
* @param body body could not be empty for POST or PUT requests
* @param requestType Request type POST requests without a payload will generate a exception
* @return base class
* @throws IOException io exception
*/
public <P> HttpURLConnection getConnection(final String serviceUrl, final P postData, final String requestType) throws IOException {
public <P> HttpURLConnection getConnection(final String serviceUrl, final P body, final String requestType) throws IOException {
if (requestType == null || !REQUEST_METHODS.contains(requestType)) {
throw new IllegalArgumentException(String.format(REQUEST_METHOD_NOT_ALLOWED, requestType));
}

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

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

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

final String json = mapper.writeValueAsString(postData);
final String json = mapper.writeValueAsString(body);
connection.getOutputStream().write(json.getBytes(String.valueOf(StandardCharsets.UTF_8)));
} else if ("DELETE".equals(requestType)) {
// could have just used rquestType as it is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,9 @@ public class Webhook implements Serializable {

private static final long serialVersionUID = 727746356185518354L;

private String title;
private String url;
private String token;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getUrl() {
return url;
}
Expand All @@ -37,8 +28,7 @@ public void setToken(String token) {
@Override
public String toString() {
return "Webhook{" +
"title='" + title + '\'' +
", url='" + url + '\'' +
"url='" + url + '\'' +
", token='" + token + '\'' +
'}';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.messagebird.objects.voicecalls;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

public class WebhookList implements Serializable {

private static final long serialVersionUID = -5524142916135114801L;

private List<WebhookResponse> data;
@JsonProperty("_list")
private Map<String, String> links;
private Pagination pagination;

public List<WebhookResponse> getData() {
return data;
}

public void setData(List<WebhookResponse> data) {
this.data = data;
}

public Map<String, String> getLinks() {
return links;
}

public void setLinks(Map<String, String> links) {
this.links = links;
}

public Pagination getPagination() {
return pagination;
}

public void setPagination(Pagination pagination) {
this.pagination = pagination;
}

@Override
public String toString() {
return "WebhookList{" +
"data=" + data +
", links=" + links +
", pagination=" + pagination +
'}';
}
}
63 changes: 49 additions & 14 deletions api/src/test/java/com/messagebird/MessageBirdClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -603,34 +603,24 @@ public void testViewTranscription() throws UnauthorizedException, GeneralExcepti

@Test
public void testCreateWebhook() throws UnauthorizedException, GeneralException {
final Webhook webhook = TestUtil.createWebHook();
final Webhook webhook = TestUtil.createWebhook();
final WebhookResponseData webhookResponseData = TestUtil.createWebhookResponseData();

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

when(messageBirdServiceMock.sendPayLoad(VOICE_CALLS_BASE_URL + WEBHOOKS, webhook, WebhookResponseData.class))
.thenReturn(webhookResponseData);
final WebhookResponseData response = messageBirdClientInjectMock.createWebHook(webhook);
final WebhookResponseData response = messageBirdClientInjectMock.createWebhook(webhook);
verify(messageBirdServiceMock, times(1)).sendPayLoad(VOICE_CALLS_BASE_URL + WEBHOOKS, webhook, WebhookResponseData.class);
assertNotNull(response);
assertEquals(response.getData().get(0).getId(), webhookResponseData.getData().get(0).getId());
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowIllegalArgumentExceptionWhenCreateWebhookWithMissingTitle() throws UnauthorizedException, GeneralException {
final Webhook webhook = new Webhook();
webhook.setUrl("ANY_URL");
messageBirdClient.createWebHook(webhook);

}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowIllegalArgumentExceptionWhenCreateWebhookWithMissingUrl() throws UnauthorizedException, GeneralException {
final Webhook webhook = new Webhook();
webhook.setTitle("ANY_TITLE");
messageBirdClient.createWebHook(webhook);

messageBirdClient.createWebhook(webhook);
}

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

when(messageBirdServiceMock.requestByID(VOICE_CALLS_BASE_URL + WEBHOOKS, "ANY_ID", WebhookResponseData.class))
.thenReturn(webhookResponseData);
final WebhookResponseData response = messageBirdClientInjectMock.viewWebHook("ANY_ID");
final WebhookResponseData response = messageBirdClientInjectMock.viewWebhook("ANY_ID");
verify(messageBirdServiceMock, times(1)).requestByID(VOICE_CALLS_BASE_URL + WEBHOOKS,
"ANY_ID", WebhookResponseData.class);
assertNotNull(response);
assertEquals(response.getData().get(0).getId(), webhookResponseData.getData().get(0).getId());
assertEquals(response.getData().get(0).getUrl(), webhookResponseData.getData().get(0).getUrl());
}

@Test
public void testListWebhooks() throws UnauthorizedException, GeneralException {
final WebhookList webhookList = TestUtil.createWebhookList();

MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class);
MessageBirdClient messageBirdClientMock = new MessageBirdClient(messageBirdServiceMock);

when(messageBirdServiceMock.requestList(anyString(), anyInt(), anyInt(), eq(WebhookList.class)))
.thenReturn(webhookList);
final WebhookList response = messageBirdClientMock.listWebhooks(0, 0);
verify(messageBirdServiceMock, times(1))
.requestList(VOICE_CALLS_BASE_URL + WEBHOOKS, 0, 0, WebhookList.class);
assertNotNull(response);
assertEquals(response.getData().get(0).getId(), webhookList.getData().get(0).getId());
assertEquals(response.getData().get(0).getUrl(), webhookList.getData().get(0).getUrl());
}

@Test
public void testUpdateWebhook() throws UnauthorizedException, GeneralException {
final Webhook webhook = TestUtil.createWebhook();
final WebhookResponseData webhookResponseData = TestUtil.createWebhookResponseData();
final String id = webhookResponseData.getData().get(0).getId();

MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class);
MessageBirdClient messageBirdClientMock = new MessageBirdClient(messageBirdServiceMock);

String url = String.format("%s%s/%s", VOICE_CALLS_BASE_URL, WEBHOOKS, id);
when(messageBirdServiceMock.sendPayLoad(anyString(), anyString(), eq(webhook), eq(WebhookResponseData.class)))
.thenReturn(webhookResponseData);
final WebhookResponseData response = messageBirdClientMock.updateWebhook(id, webhook);
verify(messageBirdServiceMock, times(1))
.sendPayLoad("PUT", url, webhook, WebhookResponseData.class);
assertNotNull(response);
assertEquals(response.getData().get(0).getUrl(), webhookResponseData.getData().get(0).getUrl());
}

@Test
public void testDeleteWebhook() throws NotFoundException, GeneralException, UnauthorizedException {
MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class);
MessageBirdClient messageBirdClientMock = new MessageBirdClient(messageBirdServiceMock);

messageBirdClientMock.deleteWebhook("id");
verify(messageBirdServiceMock, times(1)).deleteByID(VOICE_CALLS_BASE_URL + WEBHOOKS, "id");
}
}
10 changes: 8 additions & 2 deletions api/src/test/java/com/messagebird/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ static TranscriptionResponse createTranscriptionResponse() {
return transcriptionResponse;
}

static Webhook createWebHook() {
static Webhook createWebhook() {
final Webhook webhook = new Webhook();
webhook.setTitle("ANY_TITLE");
webhook.setUrl("ANY_URL");
return webhook;
}
Expand All @@ -114,6 +113,13 @@ static WebhookResponseData createWebhookResponseData() {
return webhookResponseData;
}

static WebhookList createWebhookList() {
final WebhookList webhookList = new WebhookList();
webhookList.setData(Collections.singletonList(createWebhookResponse()));
webhookList.setLinks(Collections.singletonMap("self", "ANY_ID"));
return webhookList;
}

private static Contact createContact(){
final CustomDetails customDetails = new CustomDetails();
customDetails.setCustom1("ANY_DETAIL");
Expand Down
16 changes: 8 additions & 8 deletions examples/src/main/java/ExampleSendWebhook.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public class ExampleSendWebhook {

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

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

try {
//Creating webHook object to send client
//Creating webhook object to send client
System.out.println("Creating new webhook..");
final Webhook webhook = new Webhook();
webhook.setTitle(args[1]);
webhook.setUrl(args[2]);
//Sending webHook object to client
final WebhookResponseData webhookResponseDataList = messageBirdClient.createWebHook(webhook);
//Display webHook response
webhook.setUrl(args[1]);
webhook.setToken(args[2]);
//Sending webhook object to client
final WebhookResponseData webhookResponseDataList = messageBirdClient.createWebhook(webhook);
//Display webhook response
System.out.println(webhookResponseDataList.toString());
} catch (GeneralException | UnauthorizedException exception) {
exception.printStackTrace();
Expand Down
Loading