Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to send requests synchronously? #12

Closed
yvasyliev opened this issue Oct 27, 2020 · 4 comments
Closed

How to send requests synchronously? #12

yvasyliev opened this issue Oct 27, 2020 · 4 comments

Comments

@yvasyliev
Copy link
Contributor

Hi!

Is there any chance to send requests to Telegram API synchronously? In my application I need some things to do one by one.

@cavallium
Copy link
Member

cavallium commented Oct 27, 2020

You can wrap the send request into a CompletableFuture.

public static <T extends TdApi.Object> T sendSynchronously(TelegramClient client, TdApi.Function request, int timeoutSeconds)
		throws ExecutionException, InterruptedException {
	var response = new CompletableFuture<>();

	client.send(request, response::complete, response::completeExceptionally);
	return (T) response.completeOnTimeout(new TdApi.Error(408, "Request Timeout"), timeoutSeconds, TimeUnit.SECONDS).get();
}

Example:

TdApi.User me = sendSynchronously(tc, new TdApi.GetMe(), 10);

@lexanderav
Copy link

the work of this function gives me a Request Timeout, but I'm doing everything right. what could be the problem ?

@yvasyliev
Copy link
Contributor Author

@lexanderav, it may take some time for Telegram functions to return a result, so you have two options:

  • increase value of timeoutSeconds
  • use join instead of completeOnTimeout:
    @SuppressWarnings("unchecked")
    public <T extends TdApi.Object> T send(TelegramClient client, TdApi.Function<T> request) {
        var response = new CompletableFuture<>();
        client.send(request, response::complete, response::completeExceptionally);
        return (T) response.join();
    }

@lexanderav
Copy link

Thanks for your reply. I increase the value of timeoutSeconds but no response comes.
If to use ResultHandler the answer comes.
Using join also doesn't work. Waited a long time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants