Skip to content
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
added option to java client to send query with CompletableFuture resp…
…onse
  • Loading branch information
p-vorobyev committed Feb 17, 2024
commit 5c981e0cd90ee7ba3b2faf7551c8f043bf4ac3ab
22 changes: 22 additions & 0 deletions example/java/org/drinkless/tdlib/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
package org.drinkless.tdlib;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

Expand Down Expand Up @@ -111,6 +112,27 @@ public void send(TdApi.Function query, ResultHandler resultHandler) {
send(query, resultHandler, null);
}

/**
* Sends a request to the TDLib.
*
* @param query Object representing a query to the TDLib.
* @param <T> Automatically deduced return type of the query.
* @return {@link CompletableFuture} response with result type
* If this stage completes exceptionally it throws {@link ExecutionException}
*/
@SuppressWarnings("unchecked")
public <T extends TdApi.Object> CompletableFuture<T> send(TdApi.Function<T> query) {
CompletableFuture<T> future = new CompletableFuture<>();
send(query, object -> {
if (object instanceof TdApi.Error) {
future.completeExceptionally(new ExecutionException((TdApi.Error) object));
} else {
future.complete((T) object);
}
});
return future;
}

/**
* Synchronously executes a TDLib request. Only a few marked accordingly requests can be executed synchronously.
*
Expand Down