Skip to content

Commit 2f01895

Browse files
committed
docs: callback
- Rewrites callback.md to be equally useful to TypeScript, Python, and Java developers - Removes Table of Contents, back-links, Terminology, Key Features, Best Practices, and FAQ sections - Adds createCallback and waitForCallback walkthrough examples for all three languages - Documents method signatures, CallbackConfig, WaitForCallbackConfig, and the Callback handle type per language - Adds Send callback results section covering the AWS CLI and SAM CLI (sam local and sam remote) with success and failure examples - Adds new example files under examples/{typescript,python,java}/ operations/callbacks/ closes #16, closes #10, closes #70, closes #52
1 parent 816162b commit 2f01895

16 files changed

Lines changed: 519 additions & 502 deletions

docs/sdk-reference/operations/callback.md

Lines changed: 324 additions & 502 deletions
Large diffs are not rendered by default.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import software.amazon.lambda.durable.DurableCallbackFuture;
2+
import software.amazon.lambda.durable.DurableContext;
3+
import software.amazon.lambda.durable.DurableHandler;
4+
5+
public class BasicCallbackExample extends DurableHandler<Object, String> {
6+
@Override
7+
public String handleRequest(Object input, DurableContext context) {
8+
DurableCallbackFuture<String> callback =
9+
context.createCallback("wait-for-approval", String.class);
10+
11+
// Send callback.callbackId() to the external system that will resume this function.
12+
sendApprovalRequest(callback.callbackId());
13+
14+
// Execution suspends here until the external system calls back.
15+
return callback.get();
16+
}
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.time.Duration;
2+
import software.amazon.lambda.durable.DurableCallbackFuture;
3+
import software.amazon.lambda.durable.DurableContext;
4+
import software.amazon.lambda.durable.DurableHandler;
5+
import software.amazon.lambda.durable.config.CallbackConfig;
6+
7+
public class CallbackConfigExample extends DurableHandler<Object, String> {
8+
@Override
9+
public String handleRequest(Object input, DurableContext context) {
10+
CallbackConfig config = CallbackConfig.builder()
11+
.timeout(Duration.ofHours(24))
12+
.heartbeatTimeout(Duration.ofMinutes(30))
13+
.build();
14+
15+
DurableCallbackFuture<String> callback =
16+
context.createCallback("wait-for-payment", String.class, config);
17+
18+
submitPaymentRequest(callback.callbackId());
19+
return callback.get();
20+
}
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// With Class<T>
2+
<T> DurableCallbackFuture<T> createCallback(String name, Class<T> resultType)
3+
<T> DurableCallbackFuture<T> createCallback(String name, Class<T> resultType, CallbackConfig config)
4+
5+
// With TypeToken<T> for generic result types
6+
<T> DurableCallbackFuture<T> createCallback(String name, TypeToken<T> resultType)
7+
<T> DurableCallbackFuture<T> createCallback(String name, TypeToken<T> resultType, CallbackConfig config)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import software.amazon.lambda.durable.DurableContext;
2+
import software.amazon.lambda.durable.DurableHandler;
3+
4+
public class WaitForCallbackExample extends DurableHandler<Object, String> {
5+
@Override
6+
public String handleRequest(Object input, DurableContext context) {
7+
return context.waitForCallback(
8+
"wait-for-approval",
9+
String.class,
10+
(callbackId, stepCtx) -> sendApprovalRequest(callbackId));
11+
}
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// sync
2+
<T> T waitForCallback(String name, Class<T> resultType, BiConsumer<String, StepContext> func)
3+
<T> T waitForCallback(String name, Class<T> resultType, BiConsumer<String, StepContext> func, WaitForCallbackConfig config)
4+
5+
// async
6+
<T> DurableFuture<T> waitForCallbackAsync(String name, Class<T> resultType, BiConsumer<String, StepContext> func)
7+
<T> DurableFuture<T> waitForCallbackAsync(String name, Class<T> resultType, BiConsumer<String, StepContext> func, WaitForCallbackConfig config)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import Any
2+
3+
from aws_durable_execution_sdk_python import DurableContext, durable_execution
4+
5+
6+
@durable_execution
7+
def handler(event: Any, context: DurableContext) -> dict:
8+
callback = context.create_callback(name="wait-for-approval")
9+
10+
# Send callback.callback_id to the external system that will resume this function.
11+
send_approval_request(callback.callback_id, event["request_id"])
12+
13+
# Execution suspends here until the external system calls back.
14+
result = callback.result()
15+
return {"approved": True, "result": result}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import Any
2+
3+
from aws_durable_execution_sdk_python import DurableContext, durable_execution
4+
from aws_durable_execution_sdk_python.config import CallbackConfig, Duration
5+
6+
7+
@durable_execution
8+
def handler(event: Any, context: DurableContext) -> dict:
9+
config = CallbackConfig(
10+
timeout=Duration.from_hours(24),
11+
heartbeat_timeout=Duration.from_minutes(30),
12+
)
13+
callback = context.create_callback(name="wait-for-payment", config=config)
14+
15+
submit_payment_request(callback.callback_id, event["amount"])
16+
return callback.result()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def create_callback(
2+
self,
3+
name: str | None = None,
4+
config: CallbackConfig | None = None,
5+
) -> Callback: ...
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import Any
2+
3+
from aws_durable_execution_sdk_python import DurableContext, durable_execution
4+
from aws_durable_execution_sdk_python.types import WaitForCallbackContext
5+
6+
7+
@durable_execution
8+
def handler(event: Any, context: DurableContext) -> dict:
9+
def submit(callback_id: str, ctx: WaitForCallbackContext) -> None:
10+
send_approval_request(callback_id, event["request_id"])
11+
12+
result = context.wait_for_callback(submitter=submit, name="wait-for-approval")
13+
return {"approved": True, "result": result}

0 commit comments

Comments
 (0)