tdlib-session-container/src/main/java/it/tdlight/tdlibsession/td/WrappedReactorTelegramClient.java

55 lines
1.7 KiB
Java
Raw Normal View History

2021-02-13 17:42:22 +01:00
package it.tdlight.tdlibsession.td;
import it.tdlight.common.ReactiveTelegramClient;
import it.tdlight.jni.TdApi;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
public class WrappedReactorTelegramClient implements ReactorTelegramClient {
private final ReactiveTelegramClient reactiveTelegramClient;
public WrappedReactorTelegramClient(ReactiveTelegramClient reactiveTelegramClient) {
this.reactiveTelegramClient = reactiveTelegramClient;
}
@Override
public Flux<TdApi.Object> initialize() {
return Flux.from(reactiveTelegramClient).concatMap(item -> {
if (item.isUpdate()) {
return Mono.just(item.getUpdate());
} else if (item.isHandleException()) {
return Mono.error(item.getHandleException());
} else if (item.isUpdateException()) {
return Mono.error(item.getUpdateException());
} else {
return Mono.error(new IllegalStateException("This shouldn't happen. Received unknown ReactiveItem type"));
}
});
}
/**
* Sends a request to the TDLib.
*
* @param query Object representing a query to the TDLib.
* @throws NullPointerException if query is null.
* @return a publisher that will emit exactly one item, or an error
*/
@Override
public Mono<TdApi.Object> send(TdApi.Function query) {
return Mono.from(reactiveTelegramClient.send(query));
}
/**
* Synchronously executes a TDLib request. Only a few marked accordingly requests can be executed synchronously.
*
* @param query Object representing a query to the TDLib.
* @return request result or {@link TdApi.Error}.
* @throws NullPointerException if query is null.
*/
@Override
public TdApi.Object execute(TdApi.Function query) {
return reactiveTelegramClient.execute(query);
}
}