Skip to content

Commit

Permalink
Use case pages python (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
gvdongen authored Nov 28, 2024
1 parent 344fefd commit f581721
Show file tree
Hide file tree
Showing 63 changed files with 1,462 additions and 512 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/pre-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ jobs:
cd code_snippets/python
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
pip install mypy
python3 -m mypy .
python3 -m pip install -r requirements.txt
python3 -m pip install mypy
python3 -m mypy ./src --install-types
deactivate
- name: Install dasel
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ yarn-error.log*
/code_snippets/ts/node_modules/
/code_snippets/ts/dist/
/code_snippets/python/venv/
/code_snippets/python/.venv/
*__pycache__/
1 change: 1 addition & 0 deletions code_snippets/go/develop/serving.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func serving() {
// <start_identity>
if err := server.NewRestate().
Bind(restate.Reflect(MyService{})).
// !mark
WithIdentityV1("publickeyv1_w7YHemBctH5Ck2nQRQ47iBBqhNHy4FV7t2Usbye2A6f").
Start(context.Background(), ":9080"); err != nil {
log.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion code_snippets/go/getstarted/tour.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type CheckoutRequest struct {
/*
// <start_uuid>
func (CheckoutService) Handle(ctx restate.Context, request CheckoutRequest) (bool, error) {
// withClass(1:3) highlight-line
// !mark(1:3)
idempotencyKey := restate.Rand(ctx).UUID().String()
ctx.Log().Info("Generated idempotency key", "idempotencyKey", idempotencyKey)
return false, fmt.Errorf("Something happened!")
Expand Down
38 changes: 0 additions & 38 deletions code_snippets/java/src/main/java/develop/JournalingResults.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import dev.restate.sdk.Awakeable;
import dev.restate.sdk.Context;
import dev.restate.sdk.JsonSerdes;
import dev.restate.sdk.common.TerminalException;
import java.util.UUID;

class JournalingResults {
Expand All @@ -15,43 +14,6 @@ void sideEffect(Context ctx) {
String output = ctx.run(JsonSerdes.STRING, () -> doDbRequest());
// <end_side_effect>

var paymentClient = new PaymentClient();
String txId = "";
int amount = 1;

// <start_retry_settings>
ctx.run(
JsonSerdes.BOOLEAN,
() -> {
boolean result = paymentClient.call(txId, amount);
if (result) {
// withClass highlight-line
throw new IllegalStateException("Payment failed");
} else {
return result;
}
});
// <end_retry_settings>

// <start_terminal>
try {
ctx.run(
JsonSerdes.BOOLEAN,
() -> {
boolean result = paymentClient.call(txId, amount);
if (result) {
// withClass highlight-line
throw new TerminalException(
TerminalException.INTERNAL_SERVER_ERROR_CODE, "Payment failed");
} else {
return result;
}
});
} catch (TerminalException e) {
// handle terminal error
}
// <end_terminal>

Awakeable<Boolean> a1 = ctx.awakeable(JsonSerdes.BOOLEAN);
Awakeable<Boolean> a2 = ctx.awakeable(JsonSerdes.BOOLEAN);
Awakeable<Boolean> a3 = ctx.awakeable(JsonSerdes.BOOLEAN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class MySecureApp {
public static void main(String[] args) {
RestateHttpEndpointBuilder.builder()
.bind(new MyService())
// withClass(1:3) highlight-line
// !mark
.withRequestIdentityVerifier(
RestateRequestIdentityVerifier.fromKeys(
"publickeyv1_w7YHemBctH5Ck2nQRQ47iBBqhNHy4FV7t2Usbye2A6f"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void idempotentInvoke() {
Client rs = Client.connect("http://localhost:8080");
GreetCounterObjectClient.fromClient(rs, "Mary")
.send()
// withClass highlight-line
// !mark
.greet("Hi", CallRequestOptions.DEFAULT.withIdempotency("abcde"));
// <end_service_idempotent>
}
Expand Down
41 changes: 0 additions & 41 deletions code_snippets/java/src/main/java/develop/signals/MyWorkflow.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import dev.restate.sdk.annotation.Shared;
import dev.restate.sdk.annotation.Workflow;
import dev.restate.sdk.common.DurablePromiseKey;
import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder;
import dev.restate.sdk.serde.jackson.JacksonSerdes;
import develop.workflows.Email;
import usecases.utils.URL;
Expand All @@ -20,22 +19,24 @@ public class DataPreparationService {
DurablePromiseKey.of("url", JacksonSerdes.of(URL.class));

@Workflow
public URL run(WorkflowContext ctx, String userId) {
public URL run(WorkflowContext ctx) {
// <mark_1>
URL url = ctx.run(JacksonSerdes.of(URL.class), () -> createS3Bucket());
ctx.run(() -> uploadData(url));

// <mark_2>
ctx.promiseHandle(URL_PROMISE).resolve(url);
// </mark_2>
return url;
// </mark_1>
}

@Shared
public void resultAsEmail(SharedWorkflowContext ctx, Email email) {
// <mark_2>
URL url = ctx.promise(URL_PROMISE).awaitable().await();
// </mark_2>
ctx.run(() -> sendEmail(url, email));
}

public static void main(String[] args) {
RestateHttpEndpointBuilder.builder().bind(new DataPreparationService()).buildAndListen();
}
}
// <end_here>
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@
import java.util.concurrent.TimeUnit;
import usecases.asynctasks.synctoasync.DataPreparationServiceClient.IngressClient;

// <start_here>
public class MyClient {

private static final Client rs = Client.connect("http://localhost:8080");

// <start_here>
public void downloadData(String userId, Email email) {
// <mark_1>
Client rs = Client.connect("http://localhost:8080");
IngressClient client = DataPreparationServiceClient.fromClient(rs, userId);
// </mark_1>

// <mark_2>
client.submit(userId);
client.submit();
// </mark_2>

try {
Expand All @@ -34,5 +31,5 @@ public void downloadData(String userId, Email email) {
// </mark_4>
// ... process directly ...
}
// <end_here>
}
// <end_here>
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ public String getStage(SharedWorkflowContext ctx) {
public void approveEmail(SharedWorkflowContext ctx, String secret) {
ctx.promiseHandle(EMAIL_LINK).resolve(secret);
}

@Handler
public void rejectEmail(SharedWorkflowContext ctx) {
ctx.promiseHandle(EMAIL_LINK).reject("Abort verification");
}
// </mark_6>
}
// <end_here>
3 changes: 0 additions & 3 deletions code_snippets/kotlin/src/main/kotlin/develop/Greeter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder
import dev.restate.sdk.kotlin.KtStateKey
import dev.restate.sdk.kotlin.ObjectContext

// withClass tooltip java-overview-virtual-object
@VirtualObject
class Greeter {

// withClass(1:3) tooltip java-overview-state-key
companion object {
private val COUNT = KtStateKey.json<Int>("count")
}

// withClass tooltip java-overview-virtual-object-handler
@Handler
suspend fun greet(ctx: ObjectContext, greeting: String): String {
// Get the count and increment it
Expand Down
29 changes: 0 additions & 29 deletions code_snippets/kotlin/src/main/kotlin/develop/JournalingResults.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package develop

import dev.restate.sdk.common.TerminalException
import dev.restate.sdk.kotlin.*
import java.util.UUID

Expand All @@ -14,34 +13,6 @@ internal class SideEffects {
val txId = ""
val amount = 1

// <start_retry_settings>
ctx.runBlock {
val result = paymentClient.call(txId, amount)
if (result) {
// withClass highlight-line
throw IllegalStateException("Payment failed")
} else {
result
}
}
// <end_retry_settings>

// <start_terminal>
try {
ctx.runBlock {
val result = paymentClient.call(txId, amount)
if (result) {
// withClass highlight-line
throw TerminalException(TerminalException.INTERNAL_SERVER_ERROR_CODE, "Payment failed")
} else {
result
}
}
} catch (e: TerminalException) {
// handle terminal error
}
// <end_terminal>

val a1 = ctx.awakeable<Boolean>()
val a2 = ctx.awakeable<Boolean>()
val a3 = ctx.awakeable<Boolean>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ServiceCommunication {

// <start_one_way>
MyServiceClient.fromContext(ctx)
// withClass highlight-line
// !mark
.send()
.myHandler(request)
// <end_one_way>
Expand All @@ -51,7 +51,7 @@ class ServiceCommunication {

// <start_delayed>
MyServiceClient.fromContext(ctx)
// withClass highlight-line
// !mark
.send(1.seconds)
.myHandler(request)
// <end_delayed>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder
fun main() {
RestateHttpEndpointBuilder.builder()
.bind(MyService())
// withClass(1:5) highlight-line
// !mark(1:5)
.withRequestIdentityVerifier(
RestateRequestIdentityVerifier.fromKeys(
"publickeyv1_w7YHemBctH5Ck2nQRQ47iBBqhNHy4FV7t2Usbye2A6f",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ class Ingress {
// <start_delayed_call_kotlin>
val rs = Client.connect("http://localhost:8080")
GreeterServiceClient.fromClient(rs)
// withClass highlight-line
// !mark
.send(1.seconds)
.greet("Hi")

GreetCounterObjectClient.fromClient(rs, "Mary")
// withClass highlight-line
// !mark
.send(1000.milliseconds)
.greet("Hi")
// <end_delayed_call_kotlin>
Expand All @@ -55,7 +55,7 @@ class Ingress {
val rs = Client.connect("http://localhost:8080")
GreetCounterObjectClient.fromClient(rs, "Mary")
.send()
// withClass highlight-line
// !mark
.greet("Hi", CallRequestOptions.DEFAULT.withIdempotency("abcde"))
// <end_service_idempotent>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,23 @@ class DataPreparationService {
}

@Workflow
suspend fun run(ctx: WorkflowContext, userId: String): URL {
suspend fun run(ctx: WorkflowContext): URL {
// <mark_1>
val url: URL = ctx.runBlock { createS3Bucket() }
ctx.runBlock { uploadData(url) }

// <mark_2>
ctx.promiseHandle(URL_PROMISE).resolve(url)
// </mark_2>
return url
// </mark_1>
}

@Shared
suspend fun resultAsEmail(ctx: SharedWorkflowContext, email: Email) {
// <mark_2>
val url: URL = ctx.promise(URL_PROMISE).awaitable().await()
// </mark_2>
ctx.runBlock { sendEmail(url, email) }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@ import dev.restate.sdk.client.Client
import develop.workflows.Email
import java.util.concurrent.TimeUnit

// <start_here>
class MyClient {

companion object {
private val rs: Client = Client.connect("http://localhost:8080")
}

// <start_here>
suspend fun downloadData(userId: String, email: Email) {
// <mark_1>
val rs: Client = Client.connect("http://localhost:8080")
val client = DataPreparationServiceClient.fromClient(rs, userId)
// </mark_1>

// <mark_2>
client.submit(userId)
client.submit()
// </mark_2>

try {
Expand All @@ -32,5 +29,5 @@ class MyClient {
// </mark_4>
// ... process directly ...
}
// <end_here>
}
// <end_here>
Loading

0 comments on commit f581721

Please sign in to comment.