td icon indicating copy to clipboard operation
td copied to clipboard

CompletableFuture support for java client

Open p-vorobyev opened this issue 1 year ago • 1 comments

Added option to java client to send query with CompletableFuture response. It allows work in a functional style with asynchronous requests, as well as chaining and combining.

// with Optional check
client.send(new TdApi.GetMe()).thenAccept(response ->
        response.object().ifPresentOrElse(
                me -> System.out.println("I'm " + me.firstName + " " + me.lastName),
                () -> System.out.println(response.error().orElseGet(TdApi.Error::new))
        )
);

// with callbacks
client.send(new TdApi.GetMe())
        .thenAccept(response -> response
                .onSuccess(me -> System.out.println("I'm " + me.firstName + " " + me.lastName))
                .onError(System.out::println)
        );

Because of CompletableFuture available since java 1.8, minimum supported version has been updated too.

p-vorobyev avatar Feb 17 '24 19:02 p-vorobyev

Thank you, @p-vorobyev!

Unfortunately, this can't be merged now, because CompletableFuture is available on Android only since Android 7.0 and Telegram apps still support Android 4.*, but it can be merged after support for the old Android versions is dropped, which should happen someday.

levlam avatar Feb 26 '24 14:02 levlam