Skip to content

Commit 218feb8

Browse files
Improvements to Clients: (#524)
* Added extension methods for Kotlin to create handles * Javadocs/KTDocs improvements
1 parent 6d89738 commit 218feb8

File tree

3 files changed

+320
-9
lines changed

3 files changed

+320
-9
lines changed

client-kotlin/src/main/kotlin/dev/restate/client/kotlin/ingress.kt

Lines changed: 130 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import dev.restate.client.ResponseHead
1515
import dev.restate.client.SendResponse
1616
import dev.restate.common.Output
1717
import dev.restate.common.Request
18+
import dev.restate.common.Target
1819
import dev.restate.common.WorkflowRequest
1920
import dev.restate.serde.TypeTag
2021
import dev.restate.serde.kotlinx.typeTag
@@ -24,54 +25,82 @@ import kotlinx.coroutines.future.await
2425

2526
// Extension methods for the Client
2627

28+
/** Request options builder function */
2729
fun requestOptions(init: RequestOptions.Builder.() -> Unit): RequestOptions {
2830
val builder = RequestOptions.builder()
2931
builder.init()
3032
return builder.build()
3133
}
3234

33-
/** Shorthand for [callSuspend] */
35+
/**
36+
* Shorthand for [callSuspend]
37+
*
38+
* @param client the client to use for the call
39+
* @return the response
40+
*/
3441
suspend fun <Req, Res> Request<Req, Res>.call(client: Client): Response<Res> {
3542
return client.callSuspend(this)
3643
}
3744

38-
/** Suspend version of [Client.callAsync] */
45+
/** Call a service and wait for the response. */
3946
suspend fun <Req, Res> Client.callSuspend(request: Request<Req, Res>): Response<Res> {
4047
return this.callAsync(request).await()
4148
}
4249

43-
/** Shorthand for [sendSuspend] */
50+
/**
51+
* Shorthand for [sendSuspend]
52+
*
53+
* @param client the client to use for sending
54+
* @param delay optional execution delay
55+
* @return the send response
56+
*/
4457
suspend fun <Req, Res> Request<Req, Res>.send(
4558
client: Client,
4659
delay: Duration? = null
4760
): SendResponse<Res> {
4861
return client.sendSuspend(this, delay)
4962
}
5063

51-
/** Suspend version of [Client.sendAsync] */
64+
/**
65+
* Send a request to a service without waiting for the response, optionally providing an execution
66+
* delay to wait for.
67+
*/
5268
suspend fun <Req, Res> Client.sendSuspend(
5369
request: Request<Req, Res>,
5470
delay: Duration? = null
5571
): SendResponse<Res> {
5672
return this.sendAsync(request, delay?.toJavaDuration()).await()
5773
}
5874

59-
/** Shorthand for [submitSuspend] */
75+
/**
76+
* Shorthand for [submitSuspend]
77+
*
78+
* @param client the client to use for submission
79+
* @param delay optional execution delay
80+
* @return the send response
81+
*/
6082
suspend fun <Req, Res> WorkflowRequest<Req, Res>.submit(
6183
client: Client,
6284
delay: Duration? = null
6385
): SendResponse<Res> {
6486
return client.submitSuspend(this, delay)
6587
}
6688

67-
/** Suspend version of [Client.submitAsync] */
89+
/** Submit a workflow, optionally providing an execution delay to wait for. */
6890
suspend fun <Req, Res> Client.submitSuspend(
6991
request: WorkflowRequest<Req, Res>,
7092
delay: Duration? = null
7193
): SendResponse<Res> {
7294
return this.submitAsync(request, delay?.toJavaDuration()).await()
7395
}
7496

97+
/**
98+
* Complete with success the Awakeable.
99+
*
100+
* @param typeTag the type tag for serialization
101+
* @param payload the payload
102+
* @param options request options
103+
*/
75104
suspend fun <T : Any> Client.AwakeableHandle.resolveSuspend(
76105
typeTag: TypeTag<T>,
77106
payload: T,
@@ -80,61 +109,156 @@ suspend fun <T : Any> Client.AwakeableHandle.resolveSuspend(
80109
return this.resolveAsync(typeTag, payload, options).await()
81110
}
82111

112+
/**
113+
* Complete with success the Awakeable.
114+
*
115+
* @param payload the payload
116+
* @param options request options
117+
*/
83118
suspend inline fun <reified T : Any> Client.AwakeableHandle.resolveSuspend(
84119
payload: T,
85120
options: RequestOptions = RequestOptions.DEFAULT
86121
): Response<Void> {
87122
return this.resolveSuspend(typeTag<T>(), payload, options)
88123
}
89124

125+
/**
126+
* Complete with failure the Awakeable.
127+
*
128+
* @param reason the rejection reason
129+
* @param options request options
130+
*/
90131
suspend fun Client.AwakeableHandle.rejectSuspend(
91132
reason: String,
92133
options: RequestOptions = RequestOptions.DEFAULT
93134
): Response<Void> {
94135
return this.rejectAsync(reason, options).await()
95136
}
96137

138+
/**
139+
* Create a new [Client.InvocationHandle] for the provided invocation identifier.
140+
*
141+
* @param invocationId the invocation identifier
142+
* @return the invocation handle
143+
*/
144+
inline fun <reified Res> Client.invocationHandle(
145+
invocationId: String
146+
): Client.InvocationHandle<Res> {
147+
return this.invocationHandle(invocationId, typeTag<Res>())
148+
}
149+
150+
/**
151+
* Suspend version of [Client.InvocationHandle.attach].
152+
*
153+
* @param options request options
154+
* @return the response
155+
*/
97156
suspend fun <T> Client.InvocationHandle<T>.attachSuspend(
98157
options: RequestOptions = RequestOptions.DEFAULT
99158
): Response<T> {
100159
return this.attachAsync(options).await()
101160
}
102161

162+
/**
163+
* Suspend version of [Client.InvocationHandle.getOutput].
164+
*
165+
* @param options request options
166+
* @return the output response
167+
*/
103168
suspend fun <T : Any?> Client.InvocationHandle<T>.getOutputSuspend(
104169
options: RequestOptions = RequestOptions.DEFAULT
105170
): Response<Output<T>> {
106171
return this.getOutputAsync(options).await()
107172
}
108173

174+
/**
175+
* Create a new [Client.IdempotentInvocationHandle] for the provided target and idempotency key.
176+
*
177+
* @param target the target service/method
178+
* @param idempotencyKey the idempotency key
179+
* @return the idempotent invocation handle
180+
*/
181+
inline fun <reified Res> Client.idempotentInvocationHandle(
182+
target: Target,
183+
idempotencyKey: String
184+
): Client.IdempotentInvocationHandle<Res> {
185+
return this.idempotentInvocationHandle(target, idempotencyKey, typeTag<Res>())
186+
}
187+
188+
/**
189+
* Suspend version of [Client.IdempotentInvocationHandle.attach].
190+
*
191+
* @param options request options
192+
* @return the response
193+
*/
109194
suspend fun <T> Client.IdempotentInvocationHandle<T>.attachSuspend(
110195
options: RequestOptions = RequestOptions.DEFAULT
111196
): Response<T> {
112197
return this.attachAsync(options).await()
113198
}
114199

200+
/**
201+
* Suspend version of [Client.IdempotentInvocationHandle.getOutput].
202+
*
203+
* @param options request options
204+
* @return the output response
205+
*/
115206
suspend fun <T> Client.IdempotentInvocationHandle<T>.getOutputSuspend(
116207
options: RequestOptions = RequestOptions.DEFAULT
117208
): Response<Output<T>> {
118209
return this.getOutputAsync(options).await()
119210
}
120211

212+
/**
213+
* Create a new [Client.WorkflowHandle] for the provided workflow name and identifier.
214+
*
215+
* @param workflowName the workflow name
216+
* @param workflowId the workflow identifier
217+
* @return the workflow handle
218+
*/
219+
inline fun <reified Res> Client.workflowHandle(
220+
workflowName: String,
221+
workflowId: String
222+
): Client.WorkflowHandle<Res> {
223+
return this.workflowHandle(workflowName, workflowId, typeTag<Res>())
224+
}
225+
226+
/**
227+
* Suspend version of [Client.WorkflowHandle.attach].
228+
*
229+
* @param options request options
230+
* @return the response
231+
*/
121232
suspend fun <T> Client.WorkflowHandle<T>.attachSuspend(
122233
options: RequestOptions = RequestOptions.DEFAULT
123234
): Response<T> {
124235
return this.attachAsync(options).await()
125236
}
126237

238+
/**
239+
* Suspend version of [Client.WorkflowHandle.getOutput].
240+
*
241+
* @param options request options
242+
* @return the output response
243+
*/
127244
suspend fun <T> Client.WorkflowHandle<T>.getOutputSuspend(
128245
options: RequestOptions = RequestOptions.DEFAULT
129246
): Response<Output<T>> {
130247
return this.getOutputAsync(options).await()
131248
}
132249

250+
/** @see ResponseHead.statusCode */
133251
val ResponseHead.status: Int
134252
get() = this.statusCode()
253+
254+
/** @see ResponseHead.headers */
135255
val ResponseHead.headers: ResponseHead.Headers
136256
get() = this.headers()
257+
258+
/** @see Response.response */
137259
val <Res> Response<Res>.response: Res
138260
get() = this.response()
261+
262+
/** @see SendResponse.sendStatus */
139263
val <Res> SendResponse<Res>.sendStatus: SendResponse.SendStatus
140264
get() = this.sendStatus()

0 commit comments

Comments
 (0)