From 7f9f332cd4985cd4a27c254f37e83717f8ef26a9 Mon Sep 17 00:00:00 2001 From: Ali-BenZarrouk-MB <52418122+Ali-BenZarrouk-MB@users.noreply.github.com> Date: Thu, 4 Jul 2019 15:35:27 +0200 Subject: [PATCH] Add support for updating Conversations webhooks * Added update conversation webhook call includes unit tests Refactoring ConversationWebhookTest to use mocking instead of Spying service Refactored DTOs * Added an example for updating conversation webhook * Fixed comments on PR and upgraded to Java 11 Added status to ConversationWebhook Reverted Conversation web hook request for backwards compatibility Extracted creation of objects used in tests to TestUtil Added support for default values in example of testing update of conversation web hook * Updated travis ci to support Oracle JDK 11 * Updated major version * Renaming of DTO and testing against 11th of oracle and open jdks * Updated README.md * Updated travis ci job to remove matrix that was causing an extra build job to run --- .travis.yml | 10 +- README.md | 22 +-- api/pom.xml | 12 +- .../com/messagebird/MessageBirdClient.java | 17 ++- .../messagebird/MessageBirdServiceImpl.java | 2 +- .../conversations/ConversationWebhook.java | 15 ++- .../ConversationWebhookBaseRequest.java | 41 ++++++ .../ConversationWebhookCreateRequest.java | 31 +++++ .../ConversationWebhookRequest.java | 60 --------- .../ConversationWebhookStatus.java | 43 ++++++ .../ConversationWebhookUpdateRequest.java | 38 ++++++ .../messagebird/ConversationWebhooksTest.java | 126 +++++++++++------- .../test/java/com/messagebird/TestUtil.java | 38 +++++- examples/pom.xml | 9 +- .../ExampleUpdateConversationWebhook.java | 83 ++++++++++++ 15 files changed, 396 insertions(+), 151 deletions(-) create mode 100644 api/src/main/java/com/messagebird/objects/conversations/ConversationWebhookBaseRequest.java create mode 100644 api/src/main/java/com/messagebird/objects/conversations/ConversationWebhookCreateRequest.java delete mode 100644 api/src/main/java/com/messagebird/objects/conversations/ConversationWebhookRequest.java create mode 100644 api/src/main/java/com/messagebird/objects/conversations/ConversationWebhookStatus.java create mode 100644 api/src/main/java/com/messagebird/objects/conversations/ConversationWebhookUpdateRequest.java create mode 100644 examples/src/main/java/ExampleUpdateConversationWebhook.java diff --git a/.travis.yml b/.travis.yml index 2a8614b0..c71a5afe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,11 +3,5 @@ dist: trusty before_script: cd api/ script: mvn test -Ptest -DskipTests=false -Dhttps.protocols=TLSv1.2 -DmessageBirdAccessKey=test_iQpAp0KCs5GCsMpDhIx2leuNB -DmessageBirdMSISDN=31612345678 jdk: - - oraclejdk8 - - oraclejdk9 - - openjdk7 - - openjdk8 -matrix: - include: - - jdk: oraclejdk7 - dist: precise + - oraclejdk11 + - openjdk11 diff --git a/README.md b/README.md index 247b1e45..fbacf4fe 100644 --- a/README.md +++ b/README.md @@ -23,27 +23,7 @@ cd java-rest-api/api mvn install ``` -If you are using maven simply add the messagebird API to your dependencies like this: - -``` - - com.messagebird - messagebird-api - 2.1.1 - -``` - -In case you are building without maven you still need maven to build the libraries but -then simply copy the following jar's over to your project - -``` -messagebird-api-2.1.1.jar -jackson-core-2.9.8.jar -jackson-databind-2.9.8.jar -jackson-dataformat-csv-2.9.8.jar -jackson-annotations-2.9.8.jar -``` - +If you are using maven, please refer to the [mvn repository](https://mvnrepository.com/artifact/com.messagebird/messagebird-api) and choose your desired package version. Examples -------- diff --git a/api/pom.xml b/api/pom.xml index 6715612f..65bdd1ba 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -4,7 +4,7 @@ com.messagebird messagebird-api - 2.1.2 @@ -43,7 +43,7 @@ scm:git:git@github.com:messagebird/java-rest-api.git git@github.com:messagebird/java-rest-api.git HEAD - + @@ -67,7 +67,7 @@ disable-doclint - [1.8,) + [11,) none @@ -140,7 +140,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 3.0.0 + 3.1.0 package @@ -177,8 +177,8 @@ maven-compiler-plugin 3.7.0 - 1.7 - 1.7 + 11 + 11 diff --git a/api/src/main/java/com/messagebird/MessageBirdClient.java b/api/src/main/java/com/messagebird/MessageBirdClient.java index 6f2132cd..57c42f29 100644 --- a/api/src/main/java/com/messagebird/MessageBirdClient.java +++ b/api/src/main/java/com/messagebird/MessageBirdClient.java @@ -855,12 +855,27 @@ public void deleteConversationWebhook(final String webhookId) * @param request Webhook to create. * @return Newly created webhook. */ - public ConversationWebhook sendConversationWebhook(final ConversationWebhookRequest request) + public ConversationWebhook sendConversationWebhook(final ConversationWebhookCreateRequest request) throws UnauthorizedException, GeneralException { String url = CONVERSATIONS_BASE_URL + CONVERSATION_WEBHOOK_PATH; return messageBirdService.sendPayLoad(url, request, ConversationWebhook.class); } + /** + * Update an existing webhook. + * + * @param request update request. + * @return Updated webhook. + */ + public ConversationWebhook updateConversationWebhook(final String id, final ConversationWebhookUpdateRequest request) throws UnauthorizedException, GeneralException { + if (id == null || id.isEmpty()) { + throw new IllegalArgumentException("Conversation webhook ID must be specified."); + } + + String url = CONVERSATIONS_BASE_URL + CONVERSATION_WEBHOOK_PATH + "/" + id; + return messageBirdService.sendPayLoad("PATCH", url, request, ConversationWebhook.class); + } + /** * Gets a single webhook. * diff --git a/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java b/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java index abe70e7d..09a4f758 100644 --- a/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java +++ b/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java @@ -57,7 +57,7 @@ public class MessageBirdServiceImpl implements MessageBirdService { private final String accessKey; private final String serviceUrl; - private final String clientVersion = "2.1.1"; + private final String clientVersion = "3.0.0"; private final String userAgentString; private Proxy proxy = null; diff --git a/api/src/main/java/com/messagebird/objects/conversations/ConversationWebhook.java b/api/src/main/java/com/messagebird/objects/conversations/ConversationWebhook.java index 32365eab..91689f97 100644 --- a/api/src/main/java/com/messagebird/objects/conversations/ConversationWebhook.java +++ b/api/src/main/java/com/messagebird/objects/conversations/ConversationWebhook.java @@ -2,6 +2,7 @@ import java.util.Date; import java.util.List; +import java.util.stream.Collectors; /** * Webhooks enable real-time notifications of conversation events to be @@ -15,6 +16,7 @@ public class ConversationWebhook { private String id; private String channelId; private String url; + private ConversationWebhookStatus status; private List events; private Date createdDatetime; private Date updatedDatetime; @@ -43,6 +45,14 @@ public void setUrl(String url) { this.url = url; } + public ConversationWebhookStatus getStatus() { + return status; + } + + public void setStatus(ConversationWebhookStatus status) { + this.status = status; + } + public List getEvents() { return events; } @@ -73,9 +83,12 @@ public String toString() { "id='" + id + '\'' + ", channelId='" + channelId + '\'' + ", url='" + url + '\'' + - ", events=" + events + + ", status='" + status + '\'' + + ", events=" + events.stream().map(ConversationWebhookEvent::toString).collect(Collectors.joining(",")) + ", createdDatetime=" + createdDatetime + ", updatedDatetime=" + updatedDatetime + '}'; } + + } diff --git a/api/src/main/java/com/messagebird/objects/conversations/ConversationWebhookBaseRequest.java b/api/src/main/java/com/messagebird/objects/conversations/ConversationWebhookBaseRequest.java new file mode 100644 index 00000000..47b1d6b1 --- /dev/null +++ b/api/src/main/java/com/messagebird/objects/conversations/ConversationWebhookBaseRequest.java @@ -0,0 +1,41 @@ +package com.messagebird.objects.conversations; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * Contains common fields for webhook requests. + */ +public abstract class ConversationWebhookBaseRequest { + protected String url; + protected List events; + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public List getEvents() { + return events; + } + + public void setEvents(List events) { + this.events = events; + } + + protected abstract String getRequestName(); + + protected abstract String getStringRepresentationOfExtraParameters(); + + @Override + public String toString() { + return getRequestName() + "{" + + getStringRepresentationOfExtraParameters() + '\'' + + ", url='" + url + '\'' + + ", events=" + events.stream().map(ConversationWebhookEvent::toString).collect(Collectors.joining(",")) + + '}'; + } +} diff --git a/api/src/main/java/com/messagebird/objects/conversations/ConversationWebhookCreateRequest.java b/api/src/main/java/com/messagebird/objects/conversations/ConversationWebhookCreateRequest.java new file mode 100644 index 00000000..1b7c3864 --- /dev/null +++ b/api/src/main/java/com/messagebird/objects/conversations/ConversationWebhookCreateRequest.java @@ -0,0 +1,31 @@ +package com.messagebird.objects.conversations; + +import java.util.List; + +/** + * Request object used to create webhooks. + */ +public class ConversationWebhookCreateRequest extends ConversationWebhookBaseRequest{ + + private String channelId; + + public ConversationWebhookCreateRequest( + final String channelId, + final String url, + final List events + ) { + this.channelId = channelId; + this.url = url; + this.events = events; + } + + @Override + protected String getRequestName() { + return "ConversationWebhookCreateRequest"; + } + + @Override + protected String getStringRepresentationOfExtraParameters() { + return "channelId='" + channelId; + } +} diff --git a/api/src/main/java/com/messagebird/objects/conversations/ConversationWebhookRequest.java b/api/src/main/java/com/messagebird/objects/conversations/ConversationWebhookRequest.java deleted file mode 100644 index c12737e7..00000000 --- a/api/src/main/java/com/messagebird/objects/conversations/ConversationWebhookRequest.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.messagebird.objects.conversations; - -import java.util.List; - -/** - * Request object used to create webhooks. - */ -public class ConversationWebhookRequest { - - private String channelId; - private String url; - private List events; - - public ConversationWebhookRequest( - final String channelId, - final String url, - final List events - ) { - this.channelId = channelId; - this.url = url; - this.events = events; - } - - public ConversationWebhookRequest() { - // - } - - public String getChannelId() { - return channelId; - } - - public void setChannelId(String channelId) { - this.channelId = channelId; - } - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - - public List getEvents() { - return events; - } - - public void setEvents(List events) { - this.events = events; - } - - @Override - public String toString() { - return "ConversationWebhookRequest{" + - "channelId='" + channelId + '\'' + - ", url='" + url + '\'' + - ", events=" + events + - '}'; - } -} diff --git a/api/src/main/java/com/messagebird/objects/conversations/ConversationWebhookStatus.java b/api/src/main/java/com/messagebird/objects/conversations/ConversationWebhookStatus.java new file mode 100644 index 00000000..0e5dd186 --- /dev/null +++ b/api/src/main/java/com/messagebird/objects/conversations/ConversationWebhookStatus.java @@ -0,0 +1,43 @@ +package com.messagebird.objects.conversations; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; + +/** + * Indicates whether a conversation webhook is enabled or disabled. + */ +public enum ConversationWebhookStatus { + ENABLED("enabled"), + DISABLED("disabled"); + + private final String status; + + ConversationWebhookStatus(final String status) { + this.status = status; + } + + @JsonCreator + public static ConversationWebhookStatus forValue(final String value) { + for (ConversationWebhookStatus conversationWebhookStatus : ConversationWebhookStatus.values()) { + if (conversationWebhookStatus.getStatus().equals(value)) { + return conversationWebhookStatus; + } + } + + return null; + } + + @JsonValue + public String toJson() { + return getStatus(); + } + + public String getStatus() { + return status; + } + + @Override + public String toString() { + return getStatus(); + } +} diff --git a/api/src/main/java/com/messagebird/objects/conversations/ConversationWebhookUpdateRequest.java b/api/src/main/java/com/messagebird/objects/conversations/ConversationWebhookUpdateRequest.java new file mode 100644 index 00000000..c882ef41 --- /dev/null +++ b/api/src/main/java/com/messagebird/objects/conversations/ConversationWebhookUpdateRequest.java @@ -0,0 +1,38 @@ +package com.messagebird.objects.conversations; + +import java.util.List; + +/** + * Request object used to update webhook. + */ +public class ConversationWebhookUpdateRequest extends ConversationWebhookBaseRequest { + private ConversationWebhookStatus status; + + public ConversationWebhookUpdateRequest( + final ConversationWebhookStatus status, + final String url, + final List events + ) { + this.status = status; + this.url = url; + this.events = events; + } + + public ConversationWebhookStatus getStatus() { + return status; + } + + public void setStatus(ConversationWebhookStatus status) { + this.status = status; + } + + @Override + protected String getRequestName() { + return "ConversationWebhookUpdateRequest"; + } + + @Override + protected String getStringRepresentationOfExtraParameters() { + return "status='" + status; + } +} diff --git a/api/src/test/java/com/messagebird/ConversationWebhooksTest.java b/api/src/test/java/com/messagebird/ConversationWebhooksTest.java index 1e64eba8..ccfd627d 100644 --- a/api/src/test/java/com/messagebird/ConversationWebhooksTest.java +++ b/api/src/test/java/com/messagebird/ConversationWebhooksTest.java @@ -3,43 +3,53 @@ import com.messagebird.exceptions.GeneralException; import com.messagebird.exceptions.NotFoundException; import com.messagebird.exceptions.UnauthorizedException; -import com.messagebird.objects.conversations.ConversationWebhook; -import com.messagebird.objects.conversations.ConversationWebhookEvent; -import com.messagebird.objects.conversations.ConversationWebhookList; -import com.messagebird.objects.conversations.ConversationWebhookRequest; +import com.messagebird.objects.conversations.*; +import org.junit.Before; import org.junit.Test; -import java.util.Arrays; +import java.util.Collections; +import java.util.Date; +import static com.messagebird.TestUtil.*; import static org.junit.Assert.*; +import static org.mockito.Mockito.*; public class ConversationWebhooksTest { + private static final String WHID = "whid"; + private static final String CONVERSATIONS_WEBHOOK_URL = "https://conversations.messagebird.com/v1/webhooks"; + private static final String CHANID = "chanid"; + private static final String HTTPS_EXAMPLE_COM_WEBHOOKS = "https://example.com/webhooks"; - private static final String EMPTY_RESPONSE_BODY = ""; - private static final String JSON_WEBHOOK = "{\"id\": \"whid\",\"url\": \"https://example.com/webhooks\",\"channelId\": \"chanid\",\"events\": [\"message.created\"],\"createdDatetime\": \"2018-08-30T09:34:36Z\",\"updatedDatetime\": null}"; - private static final String JSON_WEBHOOK_LIST = "{\"offset\": 10,\"limit\": 5,\"count\": 1,\"totalCount\": 11,\"items\": [{\"id\": \"whid\",\"url\": \"https://example.com/webhooks\",\"channelId\": \"chanid\",\"events\": [\"message.created\"],\"createdDatetime\": \"2018-08-24T14:46:39Z\",\"updatedDatetime\": null}]}"; - private static final int STATUS_NO_CONTENT = 204; + private MessageBirdService mockMessageBirdService; + private MessageBirdClient messageBirdClient; + + @Before + public void setUp() { + mockMessageBirdService = mock(MessageBirdService.class); + messageBirdClient = new MessageBirdClient(mockMessageBirdService); + } @Test public void testDeleteConversationWebhook() throws GeneralException, NotFoundException, UnauthorizedException { - MessageBirdService messageBirdService = SpyService - .expects("DELETE", "webhooks/whid") - .withConversationsAPIBaseURL() - .andReturns(new APIResponse(EMPTY_RESPONSE_BODY, STATUS_NO_CONTENT)); - MessageBirdClient messageBirdClient = new MessageBirdClient(messageBirdService); + doNothing().when(mockMessageBirdService).deleteByID(HTTPS_EXAMPLE_COM_WEBHOOKS, WHID); + messageBirdClient.deleteConversationWebhook(WHID); - messageBirdClient.deleteConversationWebhook("whid"); + verify(mockMessageBirdService, times(1)).deleteByID(CONVERSATIONS_WEBHOOK_URL, WHID); } @Test public void testListConversationWebhooks() throws GeneralException, UnauthorizedException { - MessageBirdService messageBirdService = SpyService - .expects("GET", "webhooks?offset=10&limit=5") - .withConversationsAPIBaseURL() - .andReturns(new APIResponse(JSON_WEBHOOK_LIST)); - MessageBirdClient messageBirdClient = new MessageBirdClient(messageBirdService); + ConversationWebhookList conversationWebhookListResponse = mock(ConversationWebhookList.class); + ConversationWebhook conversationWebhook = new ConversationWebhook(); + conversationWebhook.setId(WHID); + when(conversationWebhookListResponse.getItems()).thenReturn(Collections.singletonList(conversationWebhook)); + when(conversationWebhookListResponse.getLimit()).thenReturn(5); + when(conversationWebhookListResponse.getOffset()).thenReturn(10); + when(conversationWebhookListResponse.getTotalCount()).thenReturn(11); + when(mockMessageBirdService.requestList(CONVERSATIONS_WEBHOOK_URL, 10, 5, ConversationWebhookList.class)) + .thenReturn(conversationWebhookListResponse); ConversationWebhookList conversationWebhookList = messageBirdClient.listConversationWebhooks(10, 5); @@ -47,44 +57,70 @@ public void testListConversationWebhooks() throws GeneralException, Unauthorized assertEquals(Integer.valueOf(10), conversationWebhookList.getOffset()); assertEquals(Integer.valueOf(5), conversationWebhookList.getLimit()); assertEquals(Integer.valueOf(11), conversationWebhookList.getTotalCount()); - assertEquals("whid", conversationWebhookList.getItems().get(0).getId()); + assertEquals(WHID, conversationWebhookList.getItems().get(0).getId()); } @Test public void testSendConversationWebhook() throws GeneralException, UnauthorizedException { - ConversationWebhookRequest request = new ConversationWebhookRequest( - "chanid", - "https://example.com/webhooks", - Arrays.asList( - ConversationWebhookEvent.CONVERSATION_CREATED, - ConversationWebhookEvent.MESSAGE_CREATED - ) - ); - - MessageBirdService messageBirdService = SpyService - .expects("POST", "webhooks", request) - .withConversationsAPIBaseURL() - .andReturns(new APIResponse(JSON_WEBHOOK)); - MessageBirdClient messageBirdClient = new MessageBirdClient(messageBirdService); + ConversationWebhookCreateRequest request = createConversationWebhookRequest(); + + ConversationWebhook conversationWebhookResponse = new ConversationWebhook(); + conversationWebhookResponse.setId(WHID); + + when(mockMessageBirdService.sendPayLoad(CONVERSATIONS_WEBHOOK_URL, request, ConversationWebhook.class)) + .thenReturn(conversationWebhookResponse); ConversationWebhook conversationWebhook = messageBirdClient.sendConversationWebhook(request); - assertEquals("whid", conversationWebhook.getId()); + assertEquals(WHID, conversationWebhook.getId()); + } + + @Test + public void testUpdateConversationWebhook() throws GeneralException, UnauthorizedException { + ConversationWebhookUpdateRequest request = createConversationWebhookUpdateRequest(); + + ConversationWebhook conversationWebhookResponse = new ConversationWebhook(); + conversationWebhookResponse.setId(WHID); + conversationWebhookResponse.setUpdatedDatetime(new Date()); + + when(mockMessageBirdService.sendPayLoad("PATCH",CONVERSATIONS_WEBHOOK_URL + "/chanid", request, ConversationWebhook.class)) + .thenReturn(conversationWebhookResponse); + + ConversationWebhook conversationWebhook = messageBirdClient.updateConversationWebhook(CHANID, request); + + assertEquals(WHID, conversationWebhook.getId()); + assertNotNull(conversationWebhook.getUpdatedDatetime()); + } + + @Test(expected = IllegalArgumentException.class) + public void testUpdateConversationWebhookWithNullWebhookId() throws GeneralException, UnauthorizedException { + ConversationWebhookUpdateRequest request = createConversationWebhookUpdateRequest(); + + messageBirdClient = new MessageBirdClient(null); + + messageBirdClient.updateConversationWebhook(null, request); + } + + @Test(expected = IllegalArgumentException.class) + public void testUpdateConversationWebhookWithEmptyWebhookId() throws GeneralException, UnauthorizedException { + ConversationWebhookUpdateRequest request = createConversationWebhookUpdateRequest(); + + messageBirdClient = new MessageBirdClient(null); + + messageBirdClient.updateConversationWebhook("", request); } @Test public void testViewConversationWebhook() throws GeneralException, NotFoundException, UnauthorizedException { - MessageBirdService messageBirdService = SpyService - .expects("GET", "webhooks/whid") - .withConversationsAPIBaseURL() - .andReturns(new APIResponse(JSON_WEBHOOK)); - MessageBirdClient messageBirdClient = new MessageBirdClient(messageBirdService); + ConversationWebhook conversationWebhookResponse = createConversationWebhook(); + when(mockMessageBirdService.requestByID(CONVERSATIONS_WEBHOOK_URL, WHID, ConversationWebhook.class)) + .thenReturn(conversationWebhookResponse); - ConversationWebhook conversationWebhook = messageBirdClient.viewConversationWebhook("whid"); + ConversationWebhook conversationWebhook = messageBirdClient.viewConversationWebhook(WHID); - assertEquals("whid", conversationWebhook.getId()); - assertEquals("https://example.com/webhooks", conversationWebhook.getUrl()); - assertEquals("chanid", conversationWebhook.getChannelId()); + assertEquals(WHID, conversationWebhook.getId()); + assertEquals(HTTPS_EXAMPLE_COM_WEBHOOKS, conversationWebhook.getUrl()); + assertEquals(CHANID, conversationWebhook.getChannelId()); assertEquals(1, conversationWebhook.getEvents().size()); assertEquals(ConversationWebhookEvent.MESSAGE_CREATED, conversationWebhook.getEvents().get(0)); } diff --git a/api/src/test/java/com/messagebird/TestUtil.java b/api/src/test/java/com/messagebird/TestUtil.java index c49642e8..584b6834 100644 --- a/api/src/test/java/com/messagebird/TestUtil.java +++ b/api/src/test/java/com/messagebird/TestUtil.java @@ -1,12 +1,10 @@ package com.messagebird; import com.messagebird.objects.*; +import com.messagebird.objects.conversations.*; import com.messagebird.objects.voicecalls.*; -import java.util.Collections; -import java.util.Date; -import java.util.LinkedHashMap; -import java.util.Map; +import java.util.*; class TestUtil { @@ -148,4 +146,36 @@ static ContactList createContactList() { return contactList; } + static ConversationWebhook createConversationWebhook() { + ConversationWebhook conversationWebhookResponse = new ConversationWebhook(); + conversationWebhookResponse.setId("whid"); + conversationWebhookResponse.setUrl("https://example.com/webhooks"); + conversationWebhookResponse.setChannelId("chanid"); + conversationWebhookResponse.setEvents(Collections.singletonList( + ConversationWebhookEvent.MESSAGE_CREATED + )); + return conversationWebhookResponse; + } + + static ConversationWebhookUpdateRequest createConversationWebhookUpdateRequest() { + return new ConversationWebhookUpdateRequest( + ConversationWebhookStatus.ENABLED, + "https://example.com/webhooks", + Arrays.asList( + ConversationWebhookEvent.CONVERSATION_UPDATED, + ConversationWebhookEvent.MESSAGE_UPDATED + ) + ); + } + + static ConversationWebhookCreateRequest createConversationWebhookRequest() { + return new ConversationWebhookCreateRequest( + "chanid", + "https://example.com/webhooks", + Arrays.asList( + ConversationWebhookEvent.CONVERSATION_CREATED, + ConversationWebhookEvent.MESSAGE_CREATED + ) + ); + } } diff --git a/examples/pom.xml b/examples/pom.xml index 00936a20..88d378fd 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -6,7 +6,7 @@ com.messagebird examples - 2.1.1 + 3.0.0 @@ -20,7 +20,8 @@ com.messagebird messagebird-api - 2.1.1 + 3.0.0 + compile @@ -49,8 +50,8 @@ maven-compiler-plugin 3.1 - 1.7 - 1.7 + 11 + 11 diff --git a/examples/src/main/java/ExampleUpdateConversationWebhook.java b/examples/src/main/java/ExampleUpdateConversationWebhook.java new file mode 100644 index 00000000..37b46955 --- /dev/null +++ b/examples/src/main/java/ExampleUpdateConversationWebhook.java @@ -0,0 +1,83 @@ +import com.messagebird.MessageBirdClient; +import com.messagebird.MessageBirdService; +import com.messagebird.MessageBirdServiceImpl; +import com.messagebird.exceptions.GeneralException; +import com.messagebird.exceptions.UnauthorizedException; +import com.messagebird.objects.conversations.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ExampleUpdateConversationWebhook { + + public static void main(String[] args) { + if (args.length < 1 || args.length > 8) { + System.out.println("Please at least specify your access key and a max of 8 arguments.\n" + + "You can also specify a webhook id, enabled or not, url of webhook and events. Default value would be used if you don't\n" + + "Events can be a maximum of 4 between conversation.created, conversation.updated, message.created, message.updated\n" + + "If third argument is enabled, then status of the webhook will be updated to enabled, otherwise will be disabled\n\n" + + "Usage : java -jar test_accesskey(Required) webhook_title(Optional) enabled(Optional) webhook-url(Optional) event1(Optional) event2(Optional) event3(Optional) event4(Optional)"); + return; + } + + //First create your service object + final MessageBirdService wsr = new MessageBirdServiceImpl(args[0]); + + //Add the service to the client + final MessageBirdClient messageBirdClient = new MessageBirdClient(wsr); + + try { + //Creating webHook object to send client + System.out.println("Updating conversation webhook.."); + + ConversationWebhookUpdateRequest request = new ConversationWebhookUpdateRequest( + getStatus(args) , + getWebhookUrl(args), + getConversationWebHookEvents(args) + ); + + //Sending ConversationWebHook update request + final ConversationWebhook conversationWebhookResponse = messageBirdClient.updateConversationWebhook(args[1], request); + //Display conversationWebhook response + System.out.println(conversationWebhookResponse); + } catch (GeneralException | UnauthorizedException exception) { + exception.printStackTrace(); + } + } + + private static ConversationWebhookStatus getStatus(String[] args) { + return (args.length <= 3 || args[2].equals("enabled")) ? ConversationWebhookStatus.ENABLED : ConversationWebhookStatus.DISABLED; + } + + private static String getWebhookUrl(String[] args) { + return args.length > 4 ? args[3] : "https://example-web-hook-url"; + } + + private static List parseConversationWebHookEvents(String[] args) { + List conversationWebhookEventList = new ArrayList<>(); + + for (String arg : args ) { + ConversationWebhookEvent event = ConversationWebhookEvent.forValue(arg); + + if (event == null) { + System.out.println("Skipping unrecognized event " + arg); + continue; + } + + conversationWebhookEventList.add(event); + } + + return conversationWebhookEventList; + } + + private static List getConversationWebHookEvents(String[] args) { + if (args.length < 5) + return Arrays.asList(ConversationWebhookEvent.CONVERSATION_CREATED, ConversationWebhookEvent.MESSAGE_CREATED); + + String[] arrayOfEvents = new String[args.length - 4]; + System.arraycopy(args, 4, arrayOfEvents, 0, args.length - 4); + + return parseConversationWebHookEvents(arrayOfEvents); + } +}