Skip to content

Commit

Permalink
Rename the ingress client and the connect method (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
slinkydeveloper authored May 31, 2024
1 parent ff15028 commit 0e1434a
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public static void main(String[] args) {

// To invoke the workflow:
LoanWorkflowClient.IngressClient client =
LoanWorkflowClient.fromIngress("http://127.0.0.1:8080", "my-loan");
LoanWorkflowClient.connect("http://127.0.0.1:8080", "my-loan");

var state =
client.submit(
Expand Down
30 changes: 15 additions & 15 deletions sdk-api-gen/src/main/resources/templates/Client.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public class {{generatedClassSimpleName}} {
return new ContextClient(ctx{{#isKeyed}}, key{{/isKeyed}});
}

public static IngressClient fromIngress(dev.restate.sdk.client.IngressClient ingressClient{{#isKeyed}}, String key{{/isKeyed}}) {
return new IngressClient(ingressClient{{#isKeyed}}, key{{/isKeyed}});
public static IngressClient fromClient(dev.restate.sdk.client.Client client{{#isKeyed}}, String key{{/isKeyed}}) {
return new IngressClient(client{{#isKeyed}}, key{{/isKeyed}});
}

public static IngressClient fromIngress(String baseUri{{#isKeyed}}, String key{{/isKeyed}}) {
return new IngressClient(dev.restate.sdk.client.IngressClient.defaultClient(baseUri){{#isKeyed}}, key{{/isKeyed}});
public static IngressClient connect(String baseUri{{#isKeyed}}, String key{{/isKeyed}}) {
return new IngressClient(dev.restate.sdk.client.Client.connect(baseUri){{#isKeyed}}, key{{/isKeyed}});
}

public static class ContextClient {
Expand Down Expand Up @@ -70,17 +70,17 @@ public class {{generatedClassSimpleName}} {

public static class IngressClient {

private final dev.restate.sdk.client.IngressClient ingressClient;
private final dev.restate.sdk.client.Client client;
{{#isKeyed}}private final String key;{{/isKeyed}}

public IngressClient(dev.restate.sdk.client.IngressClient ingressClient{{#isKeyed}}, String key{{/isKeyed}}) {
this.ingressClient = ingressClient;
public IngressClient(dev.restate.sdk.client.Client client{{#isKeyed}}, String key{{/isKeyed}}) {
this.client = client;
{{#isKeyed}}this.key = key;{{/isKeyed}}
}

{{#handlers}}{{#if isWorkflow}}
public dev.restate.sdk.client.IngressClient.WorkflowHandle<{{{boxedOutputFqcn}}}> workflowHandle() {
return IngressClient.this.ingressClient.workflowHandle(
public dev.restate.sdk.client.Client.WorkflowHandle<{{{boxedOutputFqcn}}}> workflowHandle() {
return IngressClient.this.client.workflowHandle(
{{generatedClassSimpleNamePrefix}}Definitions.SERVICE_NAME,
this.key,
{{outputSerdeRef}});
Expand All @@ -93,7 +93,7 @@ public class {{generatedClassSimpleName}} {
}

public dev.restate.sdk.client.SendResponse submit({{^inputEmpty}}{{{inputFqcn}}} req, {{/inputEmpty}}dev.restate.sdk.client.RequestOptions requestOptions) {
return IngressClient.this.ingressClient.send(
return IngressClient.this.client.send(
{{{targetExpr this "this.key"}}},
{{inputSerdeRef}},
{{#if inputEmpty}}null{{else}}req{{/if}},
Expand All @@ -108,7 +108,7 @@ public class {{generatedClassSimpleName}} {
}

public java.util.concurrent.CompletableFuture<dev.restate.sdk.client.SendResponse> submitAsync({{^inputEmpty}}{{{inputFqcn}}} req, {{/inputEmpty}}dev.restate.sdk.client.RequestOptions requestOptions) {
return IngressClient.this.ingressClient.sendAsync(
return IngressClient.this.client.sendAsync(
{{{targetExpr this "this.key"}}},
{{inputSerdeRef}},
{{#if inputEmpty}}null{{else}}req{{/if}},
Expand All @@ -123,7 +123,7 @@ public class {{generatedClassSimpleName}} {
}

public {{#if outputEmpty}}void{{else}}{{{outputFqcn}}}{{/if}} {{methodName}}({{^inputEmpty}}{{{inputFqcn}}} req, {{/inputEmpty}}dev.restate.sdk.client.CallRequestOptions requestOptions) {
{{^outputEmpty}}return {{/outputEmpty}}this.ingressClient.call(
{{^outputEmpty}}return {{/outputEmpty}}this.client.call(
{{{targetExpr this "this.key"}}},
{{inputSerdeRef}},
{{outputSerdeRef}},
Expand All @@ -138,7 +138,7 @@ public class {{generatedClassSimpleName}} {
}

public {{#if outputEmpty}}java.util.concurrent.CompletableFuture<Void>{{else}}java.util.concurrent.CompletableFuture<{{{boxedOutputFqcn}}}>{{/if}} {{methodName}}Async({{^inputEmpty}}{{{inputFqcn}}} req, {{/inputEmpty}}dev.restate.sdk.client.CallRequestOptions requestOptions) {
return this.ingressClient.callAsync(
return this.client.callAsync(
{{{targetExpr this "this.key"}}},
{{inputSerdeRef}},
{{outputSerdeRef}},
Expand Down Expand Up @@ -170,7 +170,7 @@ public class {{generatedClassSimpleName}} {
}

public dev.restate.sdk.client.SendResponse {{methodName}}({{^inputEmpty}}{{{inputFqcn}}} req, {{/inputEmpty}}dev.restate.sdk.client.CallRequestOptions requestOptions) {
return IngressClient.this.ingressClient.send(
return IngressClient.this.client.send(
{{{targetExpr this "IngressClient.this.key"}}},
{{inputSerdeRef}},
{{#if inputEmpty}}null{{else}}req{{/if}},
Expand All @@ -185,7 +185,7 @@ public class {{generatedClassSimpleName}} {
}

public java.util.concurrent.CompletableFuture<dev.restate.sdk.client.SendResponse> {{methodName}}Async({{^inputEmpty}}{{{inputFqcn}}} req, {{/inputEmpty}}dev.restate.sdk.client.CallRequestOptions requestOptions) {
return IngressClient.this.ingressClient.sendAsync(
return IngressClient.this.client.sendAsync(
{{{targetExpr this "IngressClient.this.key"}}},
{{inputSerdeRef}},
{{#if inputEmpty}}null{{else}}req{{/if}},
Expand Down
6 changes: 3 additions & 3 deletions sdk-api-gen/src/test/java/dev/restate/sdk/CodegenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ public String run(WorkflowContext context, String request) {
public String submit(SharedWorkflowContext context, String request) {
// Just needs to compile
String ignored =
CodegenTestWorkflowCornerCasesClient.fromIngress("invalid", request)._submit("my_send");
CodegenTestWorkflowCornerCasesClient.fromIngress("invalid", request).submit("my_send");
return CodegenTestWorkflowCornerCasesClient.fromIngress("invalid", request)
CodegenTestWorkflowCornerCasesClient.connect("invalid", request)._submit("my_send");
CodegenTestWorkflowCornerCasesClient.connect("invalid", request).submit("my_send");
return CodegenTestWorkflowCornerCasesClient.connect("invalid", request)
.workflowHandle()
.getOutput();
}
Expand Down
20 changes: 10 additions & 10 deletions sdk-api-kotlin-gen/src/main/resources/templates/Client.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ object {{generatedClassSimpleName}} {
return ContextClient(ctx{{#isKeyed}}, key{{/isKeyed}})
}

fun fromIngress(ingressClient: dev.restate.sdk.client.IngressClient{{#isKeyed}}, key: String{{/isKeyed}}): IngressClient {
return IngressClient(ingressClient{{#isKeyed}}, key{{/isKeyed}});
fun fromClient(client: dev.restate.sdk.client.Client{{#isKeyed}}, key: String{{/isKeyed}}): IngressClient {
return IngressClient(client{{#isKeyed}}, key{{/isKeyed}});
}

fun fromIngress(baseUri: String{{#isKeyed}}, key: String{{/isKeyed}}): IngressClient {
return IngressClient(dev.restate.sdk.client.IngressClient.defaultClient(baseUri){{#isKeyed}}, key{{/isKeyed}});
fun connect(baseUri: String{{#isKeyed}}, key: String{{/isKeyed}}): IngressClient {
return IngressClient(dev.restate.sdk.client.Client.connect(baseUri){{#isKeyed}}, key{{/isKeyed}});
}

class ContextClient(private val ctx: Context{{#isKeyed}}, private val key: String{{/isKeyed}}){
Expand Down Expand Up @@ -49,18 +49,18 @@ object {{generatedClassSimpleName}} {
}
}

class IngressClient(private val ingressClient: dev.restate.sdk.client.IngressClient{{#isKeyed}}, private val key: String{{/isKeyed}}) {
class IngressClient(private val client: dev.restate.sdk.client.Client{{#isKeyed}}, private val key: String{{/isKeyed}}) {

{{#handlers}}{{#if isWorkflow}}
fun workflowHandle(): dev.restate.sdk.client.IngressClient.WorkflowHandle<{{{boxedOutputFqcn}}}> {
return this@IngressClient.ingressClient.workflowHandle(
fun workflowHandle(): dev.restate.sdk.client.Client.WorkflowHandle<{{{boxedOutputFqcn}}}> {
return this@IngressClient.client.workflowHandle(
{{generatedClassSimpleNamePrefix}}Definitions.SERVICE_NAME,
this.key,
{{outputSerdeRef}});
}

suspend fun submit({{^inputEmpty}}req: {{{inputFqcn}}}, {{/inputEmpty}}requestOptions: dev.restate.sdk.client.RequestOptions = dev.restate.sdk.client.RequestOptions.DEFAULT): dev.restate.sdk.client.SendResponse {
return this@IngressClient.ingressClient.sendSuspend(
return this@IngressClient.client.sendSuspend(
{{{targetExpr this "this.key"}}},
{{inputSerdeRef}},
{{#if inputEmpty}}Unit{{else}}req{{/if}},
Expand All @@ -69,7 +69,7 @@ object {{generatedClassSimpleName}} {
}
{{else}}
suspend fun {{methodName}}({{^inputEmpty}}req: {{{inputFqcn}}}, {{/inputEmpty}}requestOptions: dev.restate.sdk.client.CallRequestOptions = dev.restate.sdk.client.CallRequestOptions.DEFAULT): {{{boxedOutputFqcn}}} {
return this@IngressClient.ingressClient.callSuspend(
return this@IngressClient.client.callSuspend(
{{{targetExpr this "this.key"}}},
{{inputSerdeRef}},
{{outputSerdeRef}},
Expand All @@ -85,7 +85,7 @@ object {{generatedClassSimpleName}} {
inner class Send(private val delay: Duration) {
{{#handlers}}{{^isWorkflow}}
suspend fun {{methodName}}({{^inputEmpty}}req: {{{inputFqcn}}}, {{/inputEmpty}}requestOptions: dev.restate.sdk.client.CallRequestOptions = dev.restate.sdk.client.CallRequestOptions.DEFAULT): dev.restate.sdk.client.SendResponse {
return this@IngressClient.ingressClient.sendSuspend(
return this@IngressClient.client.sendSuspend(
{{{targetExpr this "[email protected]"}}},
{{inputSerdeRef}},
{{#if inputEmpty}}Unit{{else}}req{{/if}},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ class CodegenTest : TestDefinitions.TestSuite {
suspend fun submit(context: SharedWorkflowContext, request: String): String {
// Just needs to compile
val ignored: String =
CodegenTestWorkflowCornerCasesClient.fromIngress("invalid", request)._submit("my_send")
CodegenTestWorkflowCornerCasesClient.fromIngress("invalid", request).submit("my_send")
return CodegenTestWorkflowCornerCasesClient.fromIngress("invalid", request)
CodegenTestWorkflowCornerCasesClient.connect("invalid", request)._submit("my_send")
CodegenTestWorkflowCornerCasesClient.connect("invalid", request).submit("my_send")
return CodegenTestWorkflowCornerCasesClient.connect("invalid", request)
.workflowHandle()
.output
}
Expand Down
18 changes: 9 additions & 9 deletions sdk-api-kotlin/src/main/kotlin/dev/restate/sdk/kotlin/ingress.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
package dev.restate.sdk.kotlin

import dev.restate.sdk.client.IngressClient
import dev.restate.sdk.client.Client
import dev.restate.sdk.client.RequestOptions
import dev.restate.sdk.client.SendResponse
import dev.restate.sdk.common.Serde
Expand All @@ -19,7 +19,7 @@ import kotlinx.coroutines.future.await

// Extension methods for the IngressClient

suspend fun <Req, Res> IngressClient.callSuspend(
suspend fun <Req, Res> Client.callSuspend(
target: Target,
reqSerde: Serde<Req>,
resSerde: Serde<Res>,
Expand All @@ -29,7 +29,7 @@ suspend fun <Req, Res> IngressClient.callSuspend(
return this.callAsync(target, reqSerde, resSerde, req, options).await()
}

suspend fun <Req> IngressClient.sendSuspend(
suspend fun <Req> Client.sendSuspend(
target: Target,
reqSerde: Serde<Req>,
req: Req,
Expand All @@ -39,40 +39,40 @@ suspend fun <Req> IngressClient.sendSuspend(
return this.sendAsync(target, reqSerde, req, delay.toJavaDuration(), options).await()
}

suspend fun <T> IngressClient.AwakeableHandle.resolveSuspend(
suspend fun <T> Client.AwakeableHandle.resolveSuspend(
serde: Serde<T>,
payload: T,
options: RequestOptions = RequestOptions.DEFAULT
) {
this.resolveAsync(serde, payload, options).await()
}

suspend fun IngressClient.AwakeableHandle.rejectSuspend(
suspend fun Client.AwakeableHandle.rejectSuspend(
reason: String,
options: RequestOptions = RequestOptions.DEFAULT
) {
this.rejectAsync(reason, options).await()
}

suspend fun <T> IngressClient.InvocationHandle<T>.attachSuspend(
suspend fun <T> Client.InvocationHandle<T>.attachSuspend(
options: RequestOptions = RequestOptions.DEFAULT
) {
this.attachAsync(options).await()
}

suspend fun <T> IngressClient.InvocationHandle<T>.getOutputSuspend(
suspend fun <T> Client.InvocationHandle<T>.getOutputSuspend(
options: RequestOptions = RequestOptions.DEFAULT
) {
this.getOutputAsync(options).await()
}

suspend fun <T> IngressClient.WorkflowHandle<T>.attachSuspend(
suspend fun <T> Client.WorkflowHandle<T>.attachSuspend(
options: RequestOptions = RequestOptions.DEFAULT
) {
this.attachAsync(options).await()
}

suspend fun <T> IngressClient.WorkflowHandle<T>.getOutputSuspend(
suspend fun <T> Client.WorkflowHandle<T>.getOutputSuspend(
options: RequestOptions = RequestOptions.DEFAULT
) {
this.getOutputAsync(options).await()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;

public interface IngressClient {
public interface Client {

<Req, Res> CompletableFuture<Res> callAsync(
Target target, Serde<Req> reqSerde, Serde<Res> resSerde, Req req, RequestOptions options);
Expand Down Expand Up @@ -257,11 +257,11 @@ default Res getOutput() throws IngressException {
}
}

static IngressClient defaultClient(String baseUri) {
return defaultClient(baseUri, Collections.emptyMap());
static Client connect(String baseUri) {
return connect(baseUri, Collections.emptyMap());
}

static IngressClient defaultClient(String baseUri, Map<String, String> headers) {
return new DefaultIngressClient(HttpClient.newHttpClient(), baseUri, headers);
static Client connect(String baseUri, Map<String, String> headers) {
return new DefaultClient(HttpClient.newHttpClient(), baseUri, headers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@
import java.util.concurrent.CompletableFuture;
import org.jspecify.annotations.NonNull;

public class DefaultIngressClient implements IngressClient {
public class DefaultClient implements Client {

private static final JsonFactory JSON_FACTORY = new JsonFactory();

private final HttpClient httpClient;
private final URI baseUri;
private final Map<String, String> headers;

public DefaultIngressClient(HttpClient httpClient, String baseUri, Map<String, String> headers) {
DefaultClient(HttpClient httpClient, String baseUri, Map<String, String> headers) {
this.httpClient = httpClient;
this.baseUri = URI.create(baseUri);
this.headers = headers;
Expand Down Expand Up @@ -475,4 +475,9 @@ private static Map<String, String> findStringFieldsInJsonObject(

return resultMap;
}

public static DefaultClient of(
HttpClient httpClient, String baseUri, Map<String, String> headers) {
return new DefaultClient(httpClient, baseUri, headers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
package dev.restate.sdk.testing;

import dev.restate.admin.client.ApiClient;
import dev.restate.sdk.client.IngressClient;
import dev.restate.sdk.client.Client;
import java.net.URL;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
Expand All @@ -28,8 +28,8 @@ public boolean supportsParameter(
throws ParameterResolutionException {
return (parameterContext.isAnnotated(RestateAdminClient.class)
&& ApiClient.class.isAssignableFrom(parameterContext.getParameter().getType()))
|| (parameterContext.isAnnotated(RestateIngressClient.class)
&& IngressClient.class.isAssignableFrom(parameterContext.getParameter().getType()))
|| (parameterContext.isAnnotated(RestateClient.class)
&& Client.class.isAssignableFrom(parameterContext.getParameter().getType()))
|| (parameterContext.isAnnotated(RestateURL.class)
&& (String.class.isAssignableFrom(parameterContext.getParameter().getType())
|| URL.class.isAssignableFrom(parameterContext.getParameter().getType())));
Expand All @@ -41,8 +41,8 @@ public Object resolveParameter(
throws ParameterResolutionException {
if (parameterContext.isAnnotated(RestateAdminClient.class)) {
return getDeployer(extensionContext).getAdminClient();
} else if (parameterContext.isAnnotated(RestateIngressClient.class)) {
return resolveIngressClient(extensionContext);
} else if (parameterContext.isAnnotated(RestateClient.class)) {
return resolveClient(extensionContext);
} else if (parameterContext.isAnnotated(RestateURL.class)) {
URL url = getDeployer(extensionContext).getIngressUrl();
if (parameterContext.getParameter().getType().equals(String.class)) {
Expand All @@ -53,9 +53,9 @@ public Object resolveParameter(
throw new ParameterResolutionException("The parameter is not supported");
}

private IngressClient resolveIngressClient(ExtensionContext extensionContext) {
private Client resolveClient(ExtensionContext extensionContext) {
URL url = getDeployer(extensionContext).getIngressUrl();
return IngressClient.defaultClient(url.toString());
return Client.connect(url.toString());
}

private ManualRestateRunner getDeployer(ExtensionContext extensionContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
package dev.restate.sdk.testing;

import dev.restate.sdk.client.Client;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/** Inject a {@link dev.restate.sdk.client.IngressClient} to interact with the deployed runtime. */
/** Inject a {@link Client} to interact with the deployed runtime. */
@Target(value = ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface RestateIngressClient {}
public @interface RestateClient {}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
* <p>This extension is scoped per test class, meaning that the restate runner will be shared among
* test methods.
*
* <p>Use the annotations {@link RestateIngressClient}, {@link RestateURL} and {@link
* RestateAdminClient} to interact with the deployed runtime:
* <p>Use the annotations {@link RestateClient}, {@link RestateURL} and {@link RestateAdminClient}
* to interact with the deployed runtime:
*
* <pre>{@code
* {@literal @}Test
Expand Down
Loading

0 comments on commit 0e1434a

Please sign in to comment.