Skip to content

Commit

Permalink
Tunnel fixes (#147)
Browse files Browse the repository at this point in the history
# Description
This PR contains multiple small fixes for the Websocket implementation
provided in #145:
* Provide default implementations for `WebhookHandler` methods `onOpen`,
`onClose`, and `onError`
* Fix `Tunnel.onMessage` not emitting individual deltas
* Change `java-websocket` dependency to `api` configuration

# License
<!-- Your PR comment must contain the following line for us to merge the
PR. -->
I confirm that this contribution is made under the terms of the MIT
license and that I have the authority necessary to make this
contribution on behalf of its copyright owner.
  • Loading branch information
mrashed-dev committed Feb 9, 2023
1 parent 2ab635f commit d9708b3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ This section contains changes that have been committed but not yet released.

### Changed

* Provide default implementations for `WebhookHandler` methods `onOpen`, `onClose`, and `onError`

### Deprecated

### Fixed

* Fix `Tunnel.onMessage` not emitting individual deltas
* Change `java-websocket` dependency to `api` configuration

### Removed

### Security
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ dependencies {
implementation('org.slf4j:slf4j-api:1.7.30')

// Websocket dependency
implementation('org.java-websocket:Java-WebSocket:1.5.3')
api('org.java-websocket:Java-WebSocket:1.5.3')

///////////////////////////////////
// Test dependencies
Expand Down
26 changes: 17 additions & 9 deletions src/main/java/com/nylas/services/Tunnel.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,12 @@ public void connect() {
*/
@Override
public void onOpen(ServerHandshake handshakedata) {
log.trace("Opening websocket connection");
webhookHandler.onOpen(handshakedata.getHttpStatus());
}

/**
* {@inheritDoc}
* Calls {@link WebhookHandler#onMessage(Notification)}
* Calls {@link WebhookHandler#onMessage(Notification.Delta)}
*/
@Override
public void onMessage(String message) {
Expand All @@ -78,7 +77,9 @@ public void onMessage(String message) {

// Parse notification from JSON body and call onMessage callback
Notification notification = Notification.parseNotification(jsonBody);
webhookHandler.onMessage(notification);
for(Notification.Delta delta : notification.getDeltas()) {
webhookHandler.onMessage(delta);
}
}

/**
Expand All @@ -87,7 +88,6 @@ public void onMessage(String message) {
*/
@Override
public void onClose(int code, String reason, boolean remote) {
log.trace("Closing websocket connection");
webhookHandler.onClose(code, reason, remote);
}

Expand All @@ -97,7 +97,6 @@ public void onClose(int code, String reason, boolean remote) {
*/
@Override
public void onError(Exception ex) {
log.trace("Error encountered during websocket connection");
webhookHandler.onError(ex);
}

Expand Down Expand Up @@ -184,9 +183,18 @@ private static List<String> convertTriggersToString(Webhook.Trigger[] triggers)
* An interface for implementing classes to handle events from the {@link Tunnel}
*/
public interface WebhookHandler {
void onOpen(short httpStatusCode);
void onClose(int code, String reason, boolean remote);
void onMessage(Notification notification);
void onError(Exception ex);
void onMessage(Notification.Delta delta);

default void onOpen(short httpStatusCode) {
log.trace("Opening websocket connection. Code: {}", httpStatusCode);
}

default void onClose(int code, String reason, boolean remote) {
log.trace("Closing websocket connection. Code: {}, Reason: {}, Remote: {}", code, reason, remote);
}

default void onError(Exception ex) {
log.error("Error encountered during websocket connection. Exception: {}", ex.getMessage());
}
}
}

0 comments on commit d9708b3

Please sign in to comment.