diff --git a/README.md b/README.md
index 4a025770..c12e8c68 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,7 @@
# Java Client Library (beta)
-* Latest released version 0.12.0
-* Latest snapshot version 0.12.1-SNAPSHOT
+* Latest released version 0.13.0
+* Latest snapshot version 0.13.1-SNAPSHOT
## Introduction
Welcome my friends! This is the Poly API Java client GitHub page. If you are here, then it means you're familiar with what we do at Poly. If you aren't, you can always check [here](https://github.com/polyapi/poly-alpha).
diff --git a/commons/pom.xml b/commons/pom.xml
index 3b5e4ccc..bfedcf3a 100644
--- a/commons/pom.xml
+++ b/commons/pom.xml
@@ -4,7 +4,7 @@
io.polyapi
parent-pom
- 0.12.1-SNAPSHOT
+ 0.13.0-SNAPSHOT
../parent-pom
diff --git a/commons/src/main/java/io/polyapi/commons/api/websocket/WebSocketClient.java b/commons/src/main/java/io/polyapi/commons/api/websocket/WebSocketClient.java
index 57cd757d..9c3bdb87 100644
--- a/commons/src/main/java/io/polyapi/commons/api/websocket/WebSocketClient.java
+++ b/commons/src/main/java/io/polyapi/commons/api/websocket/WebSocketClient.java
@@ -20,6 +20,18 @@ public interface WebSocketClient extends AutoCloseable {
*/
Handle registerTrigger(String event, String handleId, Type eventType, PolyEventConsumer trigger);
+
+ /**
+ * Registers an event on the server so that it triggers a consumer every time an event is dispatched and blocks the current thread.
+ *
+ * @param event The event to listen to.
+ * @param handleId The ID of the emitter of the event in the server.
+ * @param eventType The type of object to be handled by the consumer. This parameter is so that a proper casting can be done.
+ * @param trigger The {@link Consumer} that will be triggered every time a listener comes. This should be a stateless object.
+ * @param The type of object that is received from the server.
+ */
+ void registerTriggerAndWait(String event, String handleId, Type eventType, PolyEventConsumer trigger);
+
/**
* Registers a listener for error messages.
*
@@ -29,5 +41,14 @@ public interface WebSocketClient extends AutoCloseable {
*/
Handle registerErrorHandler(String path, Consumer listener);
+
+ /**
+ * Registers a listener for error messages and blocks the current thread.
+ *
+ * @param path The path that will filter the messages.
+ * @param listener The {@link Consumer} of {@link PolyErrorEvent}s that will work as a listener.
+ */
+ void registerErrorHandlerAndWait(String path, Consumer listener);
+
Handle registerAuthFunctionEventHandler(String id, PolyEventConsumer trigger);
}
diff --git a/commons/src/main/java/io/polyapi/commons/internal/websocket/SocketIOWebSocketClient.java b/commons/src/main/java/io/polyapi/commons/internal/websocket/SocketIOWebSocketClient.java
index 0b4fd172..293e4a61 100644
--- a/commons/src/main/java/io/polyapi/commons/internal/websocket/SocketIOWebSocketClient.java
+++ b/commons/src/main/java/io/polyapi/commons/internal/websocket/SocketIOWebSocketClient.java
@@ -69,16 +69,27 @@ public Handle registerTrigger(String event, String handleId, Type eventType,
}
String eventKey = format("%s:%s", event, handleId);
return new EmitterHandle(eventKey, getSocket().on(eventKey, new PolyEventListener<>(event, handleId, jsonParser, EventMessage.class, message -> {
- log.debug("Parsing payload to {}.", eventType);
- T parsedInput = jsonParser.parseString(message.getBody(), eventType);
- log.debug("Input parsed. Passing it to listener.");
- trigger.accept(parsedInput, message.getHeaders(), message.getParams());
+ log.debug("Parsing payload to {}.", eventType);
+ T parsedInput = jsonParser.parseString(message.getBody(), eventType);
+ log.debug("Input parsed. Passing it to listener.");
+ trigger.accept(parsedInput, message.getHeaders(), message.getParams());
})));
} catch (InterruptedException | ExecutionException e) {
throw new EventRegistrationException(event, handleId, e);
}
}
+ @Override
+ public void registerTriggerAndWait(String event, String handleId, Type eventType, PolyEventConsumer trigger) {
+ try (Handle handle = registerTrigger(event, handleId, eventType, trigger)) {
+ synchronized (this) {
+ this.wait();
+ }
+ } catch (InterruptedException e) {
+ log.warn("Event listener for '{}' with ID '{}' interrupted.", event, handleId, e);
+ }
+ }
+
@Override
public Handle registerErrorHandler(String path, Consumer listener) {
try {
@@ -102,6 +113,18 @@ public Handle registerErrorHandler(String path, Consumer listene
}
}
+
+ @Override
+ public void registerErrorHandlerAndWait(String path, Consumer listener) {
+ try (Handle handle = registerErrorHandler(path, listener)) {
+ synchronized (this) {
+ this.wait();
+ }
+ } catch (InterruptedException e) {
+ log.warn("Error listener for path '{}' interrupted.", path, e);
+ }
+ }
+
@Override
public Handle registerAuthFunctionEventHandler(String id, PolyEventConsumer trigger) {
return registerTrigger("", id, Object[].class, trigger);
diff --git a/library/pom.xml b/library/pom.xml
index a53f14ec..b4b762e2 100644
--- a/library/pom.xml
+++ b/library/pom.xml
@@ -4,7 +4,7 @@
io.polyapi
parent-pom
- 0.12.1-SNAPSHOT
+ 0.13.0-SNAPSHOT
../parent-pom
library
diff --git a/library/src/main/java/io/polyapi/client/internal/proxy/invocation/handler/PolyTriggerInvocationHandler.java b/library/src/main/java/io/polyapi/client/internal/proxy/invocation/handler/PolyTriggerInvocationHandler.java
index aa0aaa5a..efd51fed 100644
--- a/library/src/main/java/io/polyapi/client/internal/proxy/invocation/handler/PolyTriggerInvocationHandler.java
+++ b/library/src/main/java/io/polyapi/client/internal/proxy/invocation/handler/PolyTriggerInvocationHandler.java
@@ -31,7 +31,12 @@ public Object invoke(Object proxy, Method method, Object[] args) {
log.debug("Executing method {} in proxy class {}.", method, proxy.getClass().getSimpleName());
log.debug("Registering Poly trigger with ID '{}'.", polyData.value());
log.debug("Event type: {}.", polyMetadata.paramTypes()[0]);
- return webSocketClient.registerTrigger("handleWebhookEvent", polyData.value(), Class.forName(polyMetadata.paramTypes()[0]), consumer);
+ if (method.getName().equalsIgnoreCase(invokingClass.getSimpleName())) {
+ webSocketClient.registerTriggerAndWait("handleWebhookEvent", polyData.value(), Class.forName(polyMetadata.paramTypes()[0]), consumer);
+ return null;
+ } else {
+ return webSocketClient.registerTrigger("handleWebhookEvent", polyData.value(), Class.forName(polyMetadata.paramTypes()[0]), consumer);
+ }
} catch (ClassNotFoundException e) {
throw new PolyApiLibraryException(e); // FIXME: Throw the appropriate exception.
}
diff --git a/parent-pom/pom.xml b/parent-pom/pom.xml
index 4e2bead8..e7713511 100644
--- a/parent-pom/pom.xml
+++ b/parent-pom/pom.xml
@@ -3,7 +3,7 @@
4.0.0
io.polyapi
parent-pom
- 0.12.1-SNAPSHOT
+ 0.13.0-SNAPSHOT
pom
Poly API Java parent POM
https://polyapi.io
diff --git a/polyapi-maven-plugin/pom.xml b/polyapi-maven-plugin/pom.xml
index ae9b3b1b..a7e33f04 100644
--- a/polyapi-maven-plugin/pom.xml
+++ b/polyapi-maven-plugin/pom.xml
@@ -4,7 +4,7 @@
io.polyapi
parent-pom
- 0.12.1-SNAPSHOT
+ 0.13.0-SNAPSHOT
../parent-pom
polyapi-maven-plugin
diff --git a/polyapi-maven-plugin/src/main/resources/templates/ResolvedContext.hbs b/polyapi-maven-plugin/src/main/resources/templates/ResolvedContext.hbs
index cdeae680..9ea1550d 100644
--- a/polyapi-maven-plugin/src/main/resources/templates/ResolvedContext.hbs
+++ b/polyapi-maven-plugin/src/main/resources/templates/ResolvedContext.hbs
@@ -87,13 +87,22 @@ public class {{className}} extends PolyContext {
{{~/each}}
{{~#each webhookHandlerSpecifications}}
- public Handle {{this.name}}(Consumer<{{{this.eventType}}}> callback) {
- return this.{{this.name}}.{{this.name}}(callback);
+ public Handle handle{{this.className}}(Consumer<{{{this.eventType}}}> callback) {
+ return this.{{this.name}}.handle{{this.className}}(callback);
}
- public Handle {{this.name}}(PolyEventConsumer<{{{this.eventType}}}> callback) {
- return this.{{this.name}}.{{this.name}}(callback);
+ public Handle handle{{this.className}}(PolyEventConsumer<{{{this.eventType}}}> callback) {
+ return this.{{this.name}}.handle{{this.className}}(callback);
}
+
+ public void {{this.name}}(Consumer<{{{this.eventType}}}> callback) {
+ this.{{this.name}}.{{this.name}}(callback);
+ }
+
+ public void {{this.name}}(PolyEventConsumer<{{{this.eventType}}}> callback) {
+ this.{{this.name}}.{{this.name}}(callback);
+ }
+
{{~/each}}
{{~#each specifications}}
diff --git a/polyapi-maven-plugin/src/main/resources/templates/ResolvedWebhookHandleSpecification.hbs b/polyapi-maven-plugin/src/main/resources/templates/ResolvedWebhookHandleSpecification.hbs
index 41d326e6..bc9a40e5 100644
--- a/polyapi-maven-plugin/src/main/resources/templates/ResolvedWebhookHandleSpecification.hbs
+++ b/polyapi-maven-plugin/src/main/resources/templates/ResolvedWebhookHandleSpecification.hbs
@@ -14,7 +14,11 @@ import java.util.function.Consumer;
@PolyMetadata(paramTypes="{{{eventType}}}")
public interface {{className}} extends PolyTrigger {
- Handle {{name}}(PolyEventConsumer<{{{eventType}}}> trigger);
+ Handle handle{{className}}(PolyEventConsumer<{{{eventType}}}> trigger);
- Handle {{name}}(Consumer<{{{eventType}}}> trigger);
+ Handle handle{{className}}(Consumer<{{{eventType}}}> trigger);
+
+ void {{name}}(PolyEventConsumer<{{{eventType}}}> trigger);
+
+ void {{name}}(Consumer<{{{eventType}}}> trigger);
}
diff --git a/pom.xml b/pom.xml
index 23fecef6..9a5399de 100644
--- a/pom.xml
+++ b/pom.xml
@@ -3,7 +3,7 @@
4.0.0
io.polyapi
polyapi-java
- 0.12.1-SNAPSHOT
+ 0.13.0-SNAPSHOT
pom
parent-pom