Skip to content

Commit d9708b3

Browse files
authored
Tunnel fixes (#147)
# 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.
1 parent 2ab635f commit d9708b3

3 files changed

Lines changed: 23 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ This section contains changes that have been committed but not yet released.
88

99
### Changed
1010

11+
* Provide default implementations for `WebhookHandler` methods `onOpen`, `onClose`, and `onError`
12+
1113
### Deprecated
1214

1315
### Fixed
1416

17+
* Fix `Tunnel.onMessage` not emitting individual deltas
18+
* Change `java-websocket` dependency to `api` configuration
19+
1520
### Removed
1621

1722
### Security

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ dependencies {
4141
implementation('org.slf4j:slf4j-api:1.7.30')
4242

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

4646
///////////////////////////////////
4747
// Test dependencies

src/main/java/com/nylas/services/Tunnel.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,12 @@ public void connect() {
5858
*/
5959
@Override
6060
public void onOpen(ServerHandshake handshakedata) {
61-
log.trace("Opening websocket connection");
6261
webhookHandler.onOpen(handshakedata.getHttpStatus());
6362
}
6463

6564
/**
6665
* {@inheritDoc}
67-
* Calls {@link WebhookHandler#onMessage(Notification)}
66+
* Calls {@link WebhookHandler#onMessage(Notification.Delta)}
6867
*/
6968
@Override
7069
public void onMessage(String message) {
@@ -78,7 +77,9 @@ public void onMessage(String message) {
7877

7978
// Parse notification from JSON body and call onMessage callback
8079
Notification notification = Notification.parseNotification(jsonBody);
81-
webhookHandler.onMessage(notification);
80+
for(Notification.Delta delta : notification.getDeltas()) {
81+
webhookHandler.onMessage(delta);
82+
}
8283
}
8384

8485
/**
@@ -87,7 +88,6 @@ public void onMessage(String message) {
8788
*/
8889
@Override
8990
public void onClose(int code, String reason, boolean remote) {
90-
log.trace("Closing websocket connection");
9191
webhookHandler.onClose(code, reason, remote);
9292
}
9393

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

@@ -184,9 +183,18 @@ private static List<String> convertTriggersToString(Webhook.Trigger[] triggers)
184183
* An interface for implementing classes to handle events from the {@link Tunnel}
185184
*/
186185
public interface WebhookHandler {
187-
void onOpen(short httpStatusCode);
188-
void onClose(int code, String reason, boolean remote);
189-
void onMessage(Notification notification);
190-
void onError(Exception ex);
186+
void onMessage(Notification.Delta delta);
187+
188+
default void onOpen(short httpStatusCode) {
189+
log.trace("Opening websocket connection. Code: {}", httpStatusCode);
190+
}
191+
192+
default void onClose(int code, String reason, boolean remote) {
193+
log.trace("Closing websocket connection. Code: {}, Reason: {}, Remote: {}", code, reason, remote);
194+
}
195+
196+
default void onError(Exception ex) {
197+
log.error("Error encountered during websocket connection. Exception: {}", ex.getMessage());
198+
}
191199
}
192200
}

0 commit comments

Comments
 (0)