Skip to content

Commit

Permalink
Add support for updating Conversations webhooks
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Ali-BenZarrouk-MB authored and epels committed Jul 4, 2019
1 parent defc2c0 commit 7f9f332
Show file tree
Hide file tree
Showing 15 changed files with 396 additions and 151 deletions.
10 changes: 2 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 1 addition & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

```
<dependency>
<groupId>com.messagebird</groupId>
<artifactId>messagebird-api</artifactId>
<version>2.1.1</version>
</dependency>
```

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
--------
Expand Down
12 changes: 6 additions & 6 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.messagebird</groupId>
<artifactId>messagebird-api</artifactId>
<version>2.1.2</version> <!--
<version>3.0.0</version> <!--
are you going to bump a major number?
then you are pleased to replace com.messagebird.Base64 with some library, for example net.iharder.base64
-->
Expand Down Expand Up @@ -43,7 +43,7 @@
<developerConnection>scm:git:[email protected]:messagebird/java-rest-api.git</developerConnection>
<url>[email protected]:messagebird/java-rest-api.git</url>
<tag>HEAD</tag>
</scm>
</scm>

<profiles>
<profile>
Expand All @@ -67,7 +67,7 @@
<profile>
<id>disable-doclint</id>
<activation>
<jdk>[1.8,)</jdk>
<jdk>[11,)</jdk>
</activation>
<properties>
<doclint>none</doclint>
Expand Down Expand Up @@ -140,7 +140,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.0</version>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
Expand Down Expand Up @@ -177,8 +177,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
Expand Down
17 changes: 16 additions & 1 deletion api/src/main/java/com/messagebird/MessageBirdClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,6 +16,7 @@ public class ConversationWebhook {
private String id;
private String channelId;
private String url;
private ConversationWebhookStatus status;
private List<ConversationWebhookEvent> events;
private Date createdDatetime;
private Date updatedDatetime;
Expand Down Expand Up @@ -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<ConversationWebhookEvent> getEvents() {
return events;
}
Expand Down Expand Up @@ -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 +
'}';
}


}
Original file line number Diff line number Diff line change
@@ -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<ConversationWebhookEvent> events;

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public List<ConversationWebhookEvent> getEvents() {
return events;
}

public void setEvents(List<ConversationWebhookEvent> 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(",")) +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -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<ConversationWebhookEvent> events
) {
this.channelId = channelId;
this.url = url;
this.events = events;
}

@Override
protected String getRequestName() {
return "ConversationWebhookCreateRequest";
}

@Override
protected String getStringRepresentationOfExtraParameters() {
return "channelId='" + channelId;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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 <strong>enabled</strong> or <strong>disabled</strong>.
*/
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();
}
}
Original file line number Diff line number Diff line change
@@ -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<ConversationWebhookEvent> 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;
}
}
Loading

0 comments on commit 7f9f332

Please sign in to comment.