Testing Method structure
This commit is contained in:
@@ -7,6 +7,7 @@ import dev.selimaj.session.types.Message;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.WebSocket;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public final class Session {
|
||||
|
||||
@@ -45,6 +46,13 @@ public final class Session {
|
||||
listener.notify(ws, method, data);
|
||||
}
|
||||
|
||||
public <Req, Res> CompletableFuture<Res> request(
|
||||
String method,
|
||||
Req data,
|
||||
Class<Res> resType) throws Exception {
|
||||
return listener.request(ws, method, data, resType);
|
||||
}
|
||||
|
||||
public void close() throws Exception {
|
||||
ws.sendClose(0, "Session closed");
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.net.http.WebSocket;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@@ -16,6 +17,7 @@ public class SessionListener implements WebSocket.Listener {
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private final ConcurrentHashMap<Integer, CompletableFuture<JsonNode>> pending = new ConcurrentHashMap<>();
|
||||
private final AtomicInteger id = new AtomicInteger();
|
||||
|
||||
@Override
|
||||
public CompletionStage<?> onText(WebSocket ws, CharSequence data, boolean last) {
|
||||
@@ -73,4 +75,29 @@ public class SessionListener implements WebSocket.Listener {
|
||||
void notify(WebSocket ws, String method, Object data) throws Exception {
|
||||
send(ws, new Message.Notification(method, mapper.valueToTree(data)));
|
||||
}
|
||||
|
||||
<Req, Res> CompletableFuture<Res> request(
|
||||
WebSocket ws,
|
||||
String method,
|
||||
Req data,
|
||||
Class<Res> resType) throws Exception {
|
||||
|
||||
int id = this.id.incrementAndGet();
|
||||
|
||||
CompletableFuture<JsonNode> fut = new CompletableFuture<>();
|
||||
pending.put(id, fut);
|
||||
|
||||
send(ws, new Message.Request(
|
||||
id,
|
||||
method,
|
||||
mapper.valueToTree(data)));
|
||||
|
||||
return fut.thenApply(json -> {
|
||||
try {
|
||||
return mapper.treeToValue(json, resType);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,5 +3,5 @@ package dev.selimaj.session.types;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
public interface Method<Req extends JsonNode, Res extends JsonNode, Err extends JsonNode> {
|
||||
String NAME = "";
|
||||
String getName();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user