dev.restate.sdk.client.Client Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sdk-common Show documentation
Show all versions of sdk-common Show documentation
Common interfaces of the Restate SDK
The newest version!
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
//
// This file is part of the Restate Java SDK,
// which is released under the MIT license.
//
// You can find a copy of the license in file LICENSE in the root
// directory of this repository or package, or at
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
package dev.restate.sdk.client;
import dev.restate.sdk.common.Output;
import dev.restate.sdk.common.Serde;
import dev.restate.sdk.common.Target;
import java.net.http.HttpClient;
import java.time.Duration;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
public interface Client {
CompletableFuture callAsync(
Target target, Serde reqSerde, Serde resSerde, Req req, RequestOptions options);
default CompletableFuture callAsync(
Target target, Serde reqSerde, Serde resSerde, Req req) {
return callAsync(target, reqSerde, resSerde, req, RequestOptions.DEFAULT);
}
default Res call(
Target target, Serde reqSerde, Serde resSerde, Req req, RequestOptions options)
throws IngressException {
try {
return callAsync(target, reqSerde, resSerde, req, options).join();
} catch (CompletionException e) {
if (e.getCause() instanceof RuntimeException) {
throw (RuntimeException) e.getCause();
}
throw new RuntimeException(e.getCause());
}
}
default Res call(Target target, Serde reqSerde, Serde resSerde, Req req)
throws IngressException {
return call(target, reqSerde, resSerde, req, RequestOptions.DEFAULT);
}
CompletableFuture sendAsync(
Target target,
Serde reqSerde,
Req req,
@Nullable Duration delay,
RequestOptions options);
default CompletableFuture sendAsync(
Target target, Serde reqSerde, Req req, @Nullable Duration delay) {
return sendAsync(target, reqSerde, req, delay, RequestOptions.DEFAULT);
}
default CompletableFuture sendAsync(
Target target, Serde reqSerde, Req req) {
return sendAsync(target, reqSerde, req, null, RequestOptions.DEFAULT);
}
default SendResponse send(
Target target, Serde reqSerde, Req req, @Nullable Duration delay, RequestOptions options)
throws IngressException {
try {
return sendAsync(target, reqSerde, req, delay, options).join();
} catch (CompletionException e) {
if (e.getCause() instanceof RuntimeException) {
throw (RuntimeException) e.getCause();
}
throw new RuntimeException(e.getCause());
}
}
default SendResponse send(
Target target, Serde reqSerde, Req req, @Nullable Duration delay)
throws IngressException {
return send(target, reqSerde, req, delay, RequestOptions.DEFAULT);
}
default SendResponse send(Target target, Serde reqSerde, Req req)
throws IngressException {
return send(target, reqSerde, req, null, RequestOptions.DEFAULT);
}
/**
* Create a new {@link AwakeableHandle} for the provided identifier. You can use it to {@link
* AwakeableHandle#resolve(Serde, Object)} or {@link AwakeableHandle#reject(String)} an Awakeable
* from the ingress.
*/
AwakeableHandle awakeableHandle(String id);
/**
* This class represents a handle to an Awakeable. It can be used to complete awakeables from the
* ingress
*/
interface AwakeableHandle {
/** Same as {@link #resolve(Serde, Object)} but async with options. */
CompletableFuture resolveAsync(
Serde serde, @NonNull T payload, RequestOptions options);
/** Same as {@link #resolve(Serde, Object)} but async. */
default CompletableFuture resolveAsync(Serde serde, @NonNull T payload) {
return resolveAsync(serde, payload, RequestOptions.DEFAULT);
}
/** Same as {@link #resolve(Serde, Object)} with options. */
default void resolve(Serde serde, @NonNull T payload, RequestOptions options) {
try {
resolveAsync(serde, payload, options).join();
} catch (CompletionException e) {
if (e.getCause() instanceof RuntimeException) {
throw (RuntimeException) e.getCause();
}
throw new RuntimeException(e.getCause());
}
}
/**
* Complete with success the Awakeable.
*
* @param serde used to serialize the Awakeable result payload.
* @param payload the result payload. MUST NOT be null.
*/
default void resolve(Serde serde, @NonNull T payload) {
this.resolve(serde, payload, RequestOptions.DEFAULT);
}
/** Same as {@link #reject(String)} but async with options. */
CompletableFuture rejectAsync(String reason, RequestOptions options);
/** Same as {@link #reject(String)} but async. */
default CompletableFuture rejectAsync(String reason) {
return rejectAsync(reason, RequestOptions.DEFAULT);
}
/** Same as {@link #reject(String)} with options. */
default void reject(String reason, RequestOptions options) {
try {
rejectAsync(reason, options).join();
} catch (CompletionException e) {
if (e.getCause() instanceof RuntimeException) {
throw (RuntimeException) e.getCause();
}
throw new RuntimeException(e.getCause());
}
}
/**
* Complete with failure the Awakeable.
*
* @param reason the rejection reason. MUST NOT be null.
*/
default void reject(String reason) {
this.reject(reason, RequestOptions.DEFAULT);
}
}
InvocationHandle invocationHandle(String invocationId, Serde resSerde);
interface InvocationHandle {
String invocationId();
CompletableFuture attachAsync(RequestOptions options);
default CompletableFuture attachAsync() {
return attachAsync(RequestOptions.DEFAULT);
}
default Res attach(RequestOptions options) throws IngressException {
try {
return attachAsync(options).join();
} catch (CompletionException e) {
if (e.getCause() instanceof RuntimeException) {
throw (RuntimeException) e.getCause();
}
throw new RuntimeException(e.getCause());
}
}
default Res attach() throws IngressException {
return attach(RequestOptions.DEFAULT);
}
CompletableFuture