Skip to content

Commit e500d13

Browse files
Change a bit the request api
1 parent c755b4d commit e500d13

9 files changed

Lines changed: 50 additions & 29 deletions

File tree

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -429,10 +429,10 @@ internal constructor(
429429
* @return a [KClientRequest] with the correct response type
430430
*/
431431
@Suppress("UNCHECKED_CAST")
432-
fun <Res> request(block: suspend (SVC) -> Res): KClientRequest<Any?, Res> {
432+
fun <Res> request(block: suspend SVC.() -> Res): KClientRequest<Any?, Res> {
433433
return KClientRequestImpl(
434434
client,
435-
RequestCaptureProxy(clazz, key).capture(block as suspend (SVC) -> Any?).toRequest(),
435+
RequestCaptureProxy(clazz, key).capture(block as suspend SVC.() -> Any?).toRequest(),
436436
)
437437
as KClientRequest<Any?, Res>
438438
}
@@ -444,7 +444,7 @@ internal constructor(
444444
* Example usage:
445445
* ```kotlin
446446
* client.toService<CounterKt>()
447-
* .request { it.add(1) }
447+
* .request { add(1) }
448448
* .withOptions { idempotencyKey = "123" }
449449
* .call()
450450
* ```
@@ -485,7 +485,7 @@ interface KClientRequest<Req, Res> : Request<Req, Res> {
485485
* Example usage:
486486
* ```kotlin
487487
* val response = client.toService<Greeter>()
488-
* .request { it.greet("Alice") }
488+
* .request { greet("Alice") }
489489
* .call()
490490
* ```
491491
*
@@ -507,7 +507,7 @@ inline fun <reified SVC : Any> Client.toService(): KClientRequestBuilder<SVC> {
507507
* Example usage:
508508
* ```kotlin
509509
* val response = client.toVirtualObject<Counter>("my-counter")
510-
* .request { it.add(1) }
510+
* .request { add(1) }
511511
* .call()
512512
* ```
513513
*
@@ -530,7 +530,7 @@ inline fun <reified SVC : Any> Client.toVirtualObject(key: String): KClientReque
530530
* Example usage:
531531
* ```kotlin
532532
* val response = client.toWorkflow<MyWorkflow>("workflow-123")
533-
* .request { it.run("input") }
533+
* .request { run("input") }
534534
* .call()
535535
* ```
536536
*

common-kotlin/src/main/kotlin/dev/restate/common/reflection/kotlin/RequestCaptureProxy.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class RequestCaptureProxy<SVC : Any>(private val clazz: Class<SVC>, private val
3737
* @param block the suspend lambda that invokes a method on the service proxy
3838
* @return the captured invocation information
3939
*/
40-
fun capture(block: suspend (SVC) -> Any?): CapturedInvocation {
40+
fun capture(block: suspend SVC.() -> Any?): CapturedInvocation {
4141
var capturedInvocation: CapturedInvocation? = null
4242

4343
val proxy =
@@ -60,7 +60,7 @@ class RequestCaptureProxy<SVC : Any>(private val clazz: Class<SVC>, private val
6060
}
6161
}
6262

63-
val suspendBlock: suspend () -> Any? = { block(proxy) }
63+
val suspendBlock: suspend () -> Any? = { proxy.block() }
6464
suspendBlock.startCoroutine(capturingContinuation)
6565

6666
return capturedInvocation

sdk-api-kotlin/src/main/kotlin/dev/restate/sdk/kotlin/api.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ private class KotlinStateImpl(
11421142
* Example usage:
11431143
* ```kotlin
11441144
* toService<CounterKt>()
1145-
* .request { it.add(1) }
1145+
* .request { add(1) }
11461146
* .withOptions { idempotencyKey = "123" }
11471147
* .call()
11481148
* ```
@@ -1203,9 +1203,9 @@ internal constructor(
12031203
* @return a [KRequest] with the correct response type
12041204
*/
12051205
@Suppress("UNCHECKED_CAST")
1206-
fun <Res> request(block: suspend (SVC) -> Res): KRequest<Any?, Res> {
1206+
fun <Res> request(block: suspend SVC.() -> Res): KRequest<Any?, Res> {
12071207
return KRequestImpl(
1208-
RequestCaptureProxy(clazz, key).capture(block as suspend (SVC) -> Any?).toRequest()
1208+
RequestCaptureProxy(clazz, key).capture(block as suspend SVC.() -> Any?).toRequest()
12091209
)
12101210
as KRequest<Any?, Res>
12111211
}
@@ -1219,7 +1219,7 @@ internal constructor(
12191219
* @Handler
12201220
* suspend fun myHandler(): String {
12211221
* val result = toService<Greeter>()
1222-
* .request { it.greet("Alice") }
1222+
* .request { greet("Alice") }
12231223
* .call()
12241224
* .await()
12251225
* return result
@@ -1246,7 +1246,7 @@ inline fun <reified SVC : Any> toService(): KRequestBuilder<SVC> {
12461246
* @Handler
12471247
* suspend fun myHandler(): Long {
12481248
* val result = toVirtualObject<Counter>("my-counter")
1249-
* .request { it.add(1) }
1249+
* .request { add(1) }
12501250
* .call()
12511251
* .await()
12521252
* return result
@@ -1274,7 +1274,7 @@ inline fun <reified SVC : Any> toVirtualObject(key: String): KRequestBuilder<SVC
12741274
* @Handler
12751275
* suspend fun myHandler(): String {
12761276
* val result = toWorkflow<MyWorkflow>("workflow-123")
1277-
* .request { it.run("input") }
1277+
* .request { run("input") }
12781278
* .call()
12791279
* .await()
12801280
* return result

sdk-core/src/test/kotlin/dev/restate/sdk/core/kotlinapi/reflections/ReflectionDiscoveryTest.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import dev.restate.sdk.core.generated.manifest.Input
1414
import dev.restate.sdk.core.generated.manifest.Output
1515
import dev.restate.sdk.core.generated.manifest.Service
1616
import dev.restate.sdk.kotlin.endpoint.*
17+
import dev.restate.serde.Serde
1718
import org.assertj.core.api.InstanceOfAssertFactories.type
1819
import org.junit.jupiter.api.Test
1920

@@ -49,6 +50,26 @@ class ReflectionDiscoveryTest {
4950
.isEqualTo("application/vnd.my.custom")
5051
}
5152

53+
@Test
54+
fun checkRawInputContentType() {
55+
assertThatDiscovery(RawInputOutput())
56+
.extractingService("RawInputOutput")
57+
.extractingHandler("rawInput")
58+
.extracting({ it.input }, type(Input::class.java))
59+
.extracting { it.contentType }
60+
.isEqualTo(Serde.RAW.contentType())
61+
}
62+
63+
@Test
64+
fun checkRawOutputContentType() {
65+
assertThatDiscovery(RawInputOutput())
66+
.extractingService("RawInputOutput")
67+
.extractingHandler("rawOutput")
68+
.extracting({ it.output }, type(Output::class.java))
69+
.extracting { it.contentType }
70+
.isEqualTo(Serde.RAW.contentType())
71+
}
72+
5273
@Test
5374
fun explicitNames() {
5475
assertThatDiscovery(

sdk-core/src/test/kotlin/dev/restate/sdk/core/kotlinapi/reflections/testClasses.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ open class CornerCases {
110110

111111
@Exclusive
112112
open suspend fun badReturnTypeInferred(): Unit {
113-
toVirtualObject<CornerCases>(key()).request { it.badReturnTypeInferred() }.send()
113+
toVirtualObject<CornerCases>(key()).request { badReturnTypeInferred() }.send()
114114
}
115115
}
116116

@@ -149,7 +149,7 @@ open class RawInputOutput {
149149
open class MyWorkflow {
150150
@Workflow
151151
open suspend fun run(myInput: String) {
152-
toWorkflow<MyWorkflow>(key()).request { it.sharedHandler(myInput) }.send()
152+
toWorkflow<MyWorkflow>(key()).request { sharedHandler(myInput) }.send()
153153
}
154154

155155
@Handler

sdk-spring-boot-kotlin-starter/src/test/kotlin/dev/restate/sdk/springboot/kotlin/SdkTestingIntegrationTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class SdkTestingIntegrationTest {
4949
@Timeout(value = 10)
5050
fun greetNewApiWithRequestTo(@RestateClient ingressClient: Client) = runTest {
5151
val response: String =
52-
ingressClient.toService<GreeterNewApi>().request { it.greet("Francesco") }.call().response()
52+
ingressClient.toService<GreeterNewApi>().request { greet("Francesco") }.call().response()
5353

5454
assertThat(response).isEqualTo("Something something Francesco")
5555
}

test-services/src/main/kotlin/dev/restate/sdk/testservices/KillTestImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class KillTestImpl {
2828
class SingletonImpl : KillTest.Singleton {
2929
override suspend fun recursiveCall() {
3030
val awakeable = awakeable(Serde.RAW)
31-
toVirtualObject<AwakeableHolder>(key()).request { it.hold(awakeable.id) }.send()
31+
toVirtualObject<AwakeableHolder>(key()).request { hold(awakeable.id) }.send()
3232
awakeable.await()
3333

3434
virtualObject<KillTest.Singleton>(key()).recursiveCall()

test-services/src/main/kotlin/dev/restate/sdk/testservices/NonDeterministicImpl.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ class NonDeterministicImpl : NonDeterministic {
4444

4545
override suspend fun backgroundInvokeWithDifferentTargets() {
4646
if (doLeftAction()) {
47-
toVirtualObject<Counter>("abc").request { it.get() }.send()
47+
toVirtualObject<Counter>("abc").request { get() }.send()
4848
} else {
49-
toVirtualObject<Counter>("abc").request { it.reset() }.send()
49+
toVirtualObject<Counter>("abc").request { reset() }.send()
5050
}
5151
// This is required to cause a suspension after the non-deterministic operation
5252
sleep(100.milliseconds)
@@ -65,7 +65,7 @@ class NonDeterministicImpl : NonDeterministic {
6565
}
6666

6767
private suspend fun incrementCounter() {
68-
toVirtualObject<Counter>("abc").request { it.add(1) }.send()
68+
toVirtualObject<Counter>("abc").request { add(1) }.send()
6969
}
7070

7171
private suspend fun doLeftAction(): Boolean {

test-services/src/main/kotlin/dev/restate/sdk/testservices/interpreter.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,14 @@ class ObjectInterpreterImpl(private val layer: Int) : ObjectInterpreter {
116116
}
117117
is CallService -> {
118118
val expected = "hello-$i"
119-
val awaitable = toService<ServiceInterpreterHelper>().request { it.echo(expected) }.call()
119+
val awaitable = toService<ServiceInterpreterHelper>().request { echo(expected) }.call()
120120
promises[i] = { checkAwaitable(awaitable, expected, i, cmd) }
121121
}
122122
is CallSlowService -> {
123123
val expected = "hello-$i"
124124
val awaitable =
125125
toService<ServiceInterpreterHelper>()
126-
.request { it.echoLater(EchoLaterRequest(cmd.sleep, expected)) }
126+
.request { echoLater(EchoLaterRequest(cmd.sleep, expected)) }
127127
.call()
128128
promises[i] = { checkAwaitable(awaitable, expected, i, cmd) }
129129
}
@@ -138,15 +138,15 @@ class ObjectInterpreterImpl(private val layer: Int) : ObjectInterpreter {
138138
}
139139
is IncrementStateCounterIndirectly -> {
140140
toService<ServiceInterpreterHelper>()
141-
.request { it.incrementIndirectly(interpreterId()) }
141+
.request { incrementIndirectly(interpreterId()) }
142142
.send()
143143
}
144144
is IncrementStateCounterViaAwakeable -> {
145145
// Dancing in the mooonlight!
146146
val awakeable = awakeable<String>()
147147
toService<ServiceInterpreterHelper>()
148148
.request {
149-
it.incrementViaAwakeableDance(
149+
incrementViaAwakeableDance(
150150
IncrementViaAwakeableDanceRequest(interpreterId(), awakeable.id)
151151
)
152152
}
@@ -156,7 +156,7 @@ class ObjectInterpreterImpl(private val layer: Int) : ObjectInterpreter {
156156
}
157157
is IncrementViaDelayedCall -> {
158158
toService<ServiceInterpreterHelper>()
159-
.request { it.incrementIndirectly(interpreterId()) }
159+
.request { incrementIndirectly(interpreterId()) }
160160
.send(delay = cmd.duration.milliseconds)
161161
}
162162
is RecoverTerminalCall -> {
@@ -174,18 +174,18 @@ class ObjectInterpreterImpl(private val layer: Int) : ObjectInterpreter {
174174
}
175175
is RecoverTerminalCallMaybeUnAwaited -> {
176176
val awaitable =
177-
toService<ServiceInterpreterHelper>().request { it.terminalFailure() }.call()
177+
toService<ServiceInterpreterHelper>().request { terminalFailure() }.call()
178178
promises[i] = { checkAwaitableFails(awaitable, i, cmd) }
179179
}
180180
is RejectAwakeable -> {
181181
val awakeable = awakeable<String>()
182182
promises[i] = { checkAwaitableFails(awakeable, i, cmd) }
183-
toService<ServiceInterpreterHelper>().request { it.rejectAwakeable(awakeable.id) }.send()
183+
toService<ServiceInterpreterHelper>().request { rejectAwakeable(awakeable.id) }.send()
184184
}
185185
is ResolveAwakeable -> {
186186
val awakeable = awakeable<String>()
187187
promises[i] = { checkAwaitable(awakeable, "ok", i, cmd) }
188-
toService<ServiceInterpreterHelper>().request { it.resolveAwakeable(awakeable.id) }.send()
188+
toService<ServiceInterpreterHelper>().request { resolveAwakeable(awakeable.id) }.send()
189189
}
190190
is SetState -> {
191191
state().set(cmdStateKey(cmd.key), "value-${cmd.key}")

0 commit comments

Comments
 (0)