Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
2 changes: 1 addition & 1 deletion commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.polyapi</groupId>
<artifactId>parent-pom</artifactId>
<version>0.12.1-SNAPSHOT</version>
<version>0.13.0-SNAPSHOT</version>
<relativePath>../parent-pom</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ public interface WebSocketClient extends AutoCloseable {
*/
<T> Handle registerTrigger(String event, String handleId, Type eventType, PolyEventConsumer<T> 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 <T> The type of object that is received from the server.
*/
<T> void registerTriggerAndWait(String event, String handleId, Type eventType, PolyEventConsumer<T> trigger);

/**
* Registers a listener for error messages.
*
Expand All @@ -29,5 +41,14 @@ public interface WebSocketClient extends AutoCloseable {
*/
Handle registerErrorHandler(String path, Consumer<PolyErrorEvent> 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<PolyErrorEvent> listener);

<T> Handle registerAuthFunctionEventHandler(String id, PolyEventConsumer<T> trigger);
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,27 @@ public <T> 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 <T> void registerTriggerAndWait(String event, String handleId, Type eventType, PolyEventConsumer<T> 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<PolyErrorEvent> listener) {
try {
Expand All @@ -102,6 +113,18 @@ public Handle registerErrorHandler(String path, Consumer<PolyErrorEvent> listene
}
}


@Override
public void registerErrorHandlerAndWait(String path, Consumer<PolyErrorEvent> 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 <T> Handle registerAuthFunctionEventHandler(String id, PolyEventConsumer<T> trigger) {
return registerTrigger("", id, Object[].class, trigger);
Expand Down
2 changes: 1 addition & 1 deletion library/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.polyapi</groupId>
<artifactId>parent-pom</artifactId>
<version>0.12.1-SNAPSHOT</version>
<version>0.13.0-SNAPSHOT</version>
<relativePath>../parent-pom</relativePath>
</parent>
<artifactId>library</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
Expand Down
2 changes: 1 addition & 1 deletion parent-pom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.polyapi</groupId>
<artifactId>parent-pom</artifactId>
<version>0.12.1-SNAPSHOT</version>
<version>0.13.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Poly API Java parent POM</name>
<url>https://polyapi.io</url>
Expand Down
2 changes: 1 addition & 1 deletion polyapi-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.polyapi</groupId>
<artifactId>parent-pom</artifactId>
<version>0.12.1-SNAPSHOT</version>
<version>0.13.0-SNAPSHOT</version>
<relativePath>../parent-pom</relativePath>
</parent>
<artifactId>polyapi-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@Slf4j
@Setter
@Mojo(name = "create-server-variable", requiresProject = false)
public class createServerVariableMojo extends PolyApiMojo {
public class CreateServerVariableMojo extends PolyApiMojo {
@Parameter(property = "name", required = true)
private String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.polyapi</groupId>
<artifactId>polyapi-java</artifactId>
<version>0.12.1-SNAPSHOT</version>
<version>0.13.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>parent-pom</module>
Expand Down