Search in sources :

Example 1 with WebSocketSubprotocolHandler

use of io.smallrye.graphql.client.vertx.websocket.WebSocketSubprotocolHandler in project smallrye-graphql by smallrye.

the class VertxDynamicGraphQLClient method webSocketHandler.

private Uni<WebSocketSubprotocolHandler> webSocketHandler() {
    return webSocketHandler.updateAndGet(currentValue -> {
        if (currentValue == null) {
            // if we don't have a handler, create a new one
            return Uni.createFrom().<WebSocketSubprotocolHandler>emitter(handlerEmitter -> {
                List<String> subprotocolIds = subprotocols.stream().map(i -> i.getProtocolId()).collect(toList());
                httpClient.webSocketAbs(websocketUrl, headers, WebsocketVersion.V13, subprotocolIds, result -> {
                    if (result.succeeded()) {
                        WebSocket webSocket = result.result();
                        WebSocketSubprotocolHandler handler = BuiltinWebsocketSubprotocolHandlers.createHandlerFor(webSocket.subProtocol(), webSocket, subscriptionInitializationTimeout, () -> {
                            // if the websocket disconnects, remove the handler so we can try
                            // connecting again with a new websocket and handler
                            webSocketHandler.set(null);
                        });
                        handlerEmitter.complete(handler);
                        log.debug("Using websocket subprotocol handler: " + handler);
                    } else {
                        handlerEmitter.fail(result.cause());
                    }
                });
            }).memoize().indefinitely();
        } else {
            return currentValue;
        }
    });
}
Also used : WebClientOptions(io.vertx.ext.web.client.WebClientOptions) WebSocketSubprotocolHandler(io.smallrye.graphql.client.vertx.websocket.WebSocketSubprotocolHandler) WebClient(io.vertx.ext.web.client.WebClient) Logger(org.jboss.logging.Logger) MultiMap(io.vertx.core.MultiMap) Response(io.smallrye.graphql.client.Response) WebsocketVersion(io.vertx.core.http.WebsocketVersion) AtomicReference(java.util.concurrent.atomic.AtomicReference) Multi(io.smallrye.mutiny.Multi) Uni(io.smallrye.mutiny.Uni) ResponseReader(io.smallrye.graphql.client.impl.ResponseReader) Document(io.smallrye.graphql.client.core.Document) Map(java.util.Map) WebsocketSubprotocol(io.smallrye.graphql.client.websocket.WebsocketSubprotocol) JsonObject(javax.json.JsonObject) RequestImpl(io.smallrye.graphql.client.impl.RequestImpl) BuiltinWebsocketSubprotocolHandlers(io.smallrye.graphql.client.vertx.websocket.BuiltinWebsocketSubprotocolHandlers) DynamicGraphQLClient(io.smallrye.graphql.client.dynamic.api.DynamicGraphQLClient) Vertx(io.vertx.core.Vertx) WebSocket(io.vertx.core.http.WebSocket) Collectors(java.util.stream.Collectors) Request(io.smallrye.graphql.client.Request) ExecutionException(java.util.concurrent.ExecutionException) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) Collections(java.util.Collections) HttpClient(io.vertx.core.http.HttpClient) WebSocketSubprotocolHandler(io.smallrye.graphql.client.vertx.websocket.WebSocketSubprotocolHandler) WebSocket(io.vertx.core.http.WebSocket)

Example 2 with WebSocketSubprotocolHandler

use of io.smallrye.graphql.client.vertx.websocket.WebSocketSubprotocolHandler in project smallrye-graphql by smallrye.

the class VertxTypesafeGraphQLClientProxy method webSocketHandler.

private Uni<WebSocketSubprotocolHandler> webSocketHandler() {
    return webSocketHandler.updateAndGet(currentValue -> {
        if (currentValue == null) {
            return Uni.createFrom().<WebSocketSubprotocolHandler>emitter(handlerEmitter -> {
                List<String> subprotocolIds = subprotocols.stream().map(i -> i.getProtocolId()).collect(toList());
                MultiMap headers = HeadersMultiMap.headers().addAll(new HeaderBuilder(api, null, additionalHeaders).build());
                httpClient.webSocketAbs(websocketUrl, headers, WebsocketVersion.V13, subprotocolIds, result -> {
                    if (result.succeeded()) {
                        WebSocket webSocket = result.result();
                        WebSocketSubprotocolHandler handler = BuiltinWebsocketSubprotocolHandlers.createHandlerFor(webSocket.subProtocol(), webSocket, subscriptionInitializationTimeout, () -> {
                            webSocketHandler.set(null);
                        });
                        handlerEmitter.complete(handler);
                        log.debug("Using websocket subprotocol handler: " + handler);
                    } else {
                        handlerEmitter.fail(result.cause());
                    }
                });
            }).memoize().indefinitely();
        } else {
            return currentValue;
        }
    });
}
Also used : WebSocketSubprotocolHandler(io.smallrye.graphql.client.vertx.websocket.WebSocketSubprotocolHandler) Arrays(java.util.Arrays) Array(java.lang.reflect.Array) JsonArrayBuilder(javax.json.JsonArrayBuilder) HttpResponse(io.vertx.ext.web.client.HttpResponse) WebClient(io.vertx.ext.web.client.WebClient) Date(java.util.Date) JsonBuilderFactory(javax.json.JsonBuilderFactory) Logger(org.jboss.logging.Logger) MultiMap(io.vertx.core.MultiMap) HashMap(java.util.HashMap) WebsocketVersion(io.vertx.core.http.WebsocketVersion) AtomicReference(java.util.concurrent.atomic.AtomicReference) Multi(io.smallrye.mutiny.Multi) ArrayList(java.util.ArrayList) Uni(io.smallrye.mutiny.Uni) HeadersMultiMap(io.vertx.core.http.impl.headers.HeadersMultiMap) JsonValue(javax.json.JsonValue) BigDecimal(java.math.BigDecimal) Map(java.util.Map) Json(javax.json.Json) MethodInvocation(io.smallrye.graphql.client.impl.typesafe.reflection.MethodInvocation) BigInteger(java.math.BigInteger) HeaderBuilder(io.smallrye.graphql.client.impl.typesafe.HeaderBuilder) URI(java.net.URI) InvalidResponseException(io.smallrye.graphql.client.InvalidResponseException) WebsocketSubprotocol(io.smallrye.graphql.client.websocket.WebsocketSubprotocol) JsonObject(javax.json.JsonObject) BuiltinWebsocketSubprotocolHandlers(io.smallrye.graphql.client.vertx.websocket.BuiltinWebsocketSubprotocolHandlers) FieldInfo(io.smallrye.graphql.client.impl.typesafe.reflection.FieldInfo) Collection(java.util.Collection) ResultBuilder(io.smallrye.graphql.client.impl.typesafe.ResultBuilder) JsonArray(javax.json.JsonArray) WebSocket(io.vertx.core.http.WebSocket) Future(io.vertx.core.Future) ExecutionException(java.util.concurrent.ExecutionException) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) Buffer(io.vertx.core.buffer.Buffer) QueryBuilder(io.smallrye.graphql.client.impl.typesafe.QueryBuilder) TypeInfo(io.smallrye.graphql.client.impl.typesafe.reflection.TypeInfo) JsonObjectBuilder(javax.json.JsonObjectBuilder) HttpClient(io.vertx.core.http.HttpClient) MultiMap(io.vertx.core.MultiMap) HeadersMultiMap(io.vertx.core.http.impl.headers.HeadersMultiMap) HeaderBuilder(io.smallrye.graphql.client.impl.typesafe.HeaderBuilder) WebSocketSubprotocolHandler(io.smallrye.graphql.client.vertx.websocket.WebSocketSubprotocolHandler) WebSocket(io.vertx.core.http.WebSocket)

Example 3 with WebSocketSubprotocolHandler

use of io.smallrye.graphql.client.vertx.websocket.WebSocketSubprotocolHandler in project smallrye-graphql by smallrye.

the class VertxTypesafeGraphQLClientProxy method executeSingleResultOperationOverWebsocket.

private Uni<Object> executeSingleResultOperationOverWebsocket(MethodInvocation method, JsonObject request) {
    AtomicReference<String> operationId = new AtomicReference<>();
    AtomicReference<WebSocketSubprotocolHandler> handlerRef = new AtomicReference<>();
    Uni<String> rawUni = Uni.createFrom().emitter(rawEmitter -> {
        webSocketHandler().subscribe().with((handler) -> {
            handlerRef.set(handler);
            operationId.set(handler.executeUni(request, rawEmitter));
        });
    });
    return rawUni.onCancellation().invoke(() -> {
        String id = operationId.get();
        log.trace("Received onCancellation on operation ID " + id);
        if (id != null) {
            handlerRef.get().cancelMulti(id);
        } else {
            log.trace("Received onCancellation on an operation that does not have an ID yet");
        }
    }).onItem().transform(data -> {
        Object object = new ResultBuilder(method, data).read();
        if (object != null) {
            return object;
        } else {
            throw new InvalidResponseException("Couldn't find neither data nor errors in the response: " + data);
        }
    });
}
Also used : ResultBuilder(io.smallrye.graphql.client.impl.typesafe.ResultBuilder) AtomicReference(java.util.concurrent.atomic.AtomicReference) JsonObject(javax.json.JsonObject) WebSocketSubprotocolHandler(io.smallrye.graphql.client.vertx.websocket.WebSocketSubprotocolHandler) InvalidResponseException(io.smallrye.graphql.client.InvalidResponseException)

Example 4 with WebSocketSubprotocolHandler

use of io.smallrye.graphql.client.vertx.websocket.WebSocketSubprotocolHandler in project smallrye-graphql by smallrye.

the class VertxTypesafeGraphQLClientProxy method executeSubscriptionOverWebsocket.

private Multi<Object> executeSubscriptionOverWebsocket(MethodInvocation method, JsonObject request) {
    AtomicReference<String> operationId = new AtomicReference<>();
    AtomicReference<WebSocketSubprotocolHandler> handlerRef = new AtomicReference<>();
    Multi<String> rawMulti = Multi.createFrom().emitter(rawEmitter -> {
        webSocketHandler().subscribe().with(handler -> {
            handlerRef.set(handler);
            operationId.set(handler.executeMulti(request, rawEmitter));
        });
    });
    return rawMulti.onCancellation().invoke(() -> {
        handlerRef.get().cancelMulti(operationId.get());
    }).onItem().transform(data -> {
        Object object = new ResultBuilder(method, data).read();
        if (object != null) {
            return object;
        } else {
            throw new InvalidResponseException("Couldn't find neither data nor errors in the response: " + data);
        }
    });
}
Also used : ResultBuilder(io.smallrye.graphql.client.impl.typesafe.ResultBuilder) AtomicReference(java.util.concurrent.atomic.AtomicReference) JsonObject(javax.json.JsonObject) WebSocketSubprotocolHandler(io.smallrye.graphql.client.vertx.websocket.WebSocketSubprotocolHandler) InvalidResponseException(io.smallrye.graphql.client.InvalidResponseException)

Aggregations

WebSocketSubprotocolHandler (io.smallrye.graphql.client.vertx.websocket.WebSocketSubprotocolHandler)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 JsonObject (javax.json.JsonObject)4 InvalidResponseException (io.smallrye.graphql.client.InvalidResponseException)3 ResultBuilder (io.smallrye.graphql.client.impl.typesafe.ResultBuilder)3 BuiltinWebsocketSubprotocolHandlers (io.smallrye.graphql.client.vertx.websocket.BuiltinWebsocketSubprotocolHandlers)2 WebsocketSubprotocol (io.smallrye.graphql.client.websocket.WebsocketSubprotocol)2 Multi (io.smallrye.mutiny.Multi)2 Uni (io.smallrye.mutiny.Uni)2 MultiMap (io.vertx.core.MultiMap)2 Buffer (io.vertx.core.buffer.Buffer)2 HttpClient (io.vertx.core.http.HttpClient)2 WebSocket (io.vertx.core.http.WebSocket)2 WebsocketVersion (io.vertx.core.http.WebsocketVersion)2 WebClient (io.vertx.ext.web.client.WebClient)2 List (java.util.List)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 Collectors.toList (java.util.stream.Collectors.toList)2 Logger (org.jboss.logging.Logger)2