Skip to content

Commit c9fe6d9

Browse files
committed
Reflect @acm19's feedback at #421
1 parent cb2b891 commit c9fe6d9

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed

bolt-aws-lambda/src/main/java/com/slack/api/bolt/aws_lambda/SlackApiLambdaHandler.java

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,46 @@ public abstract class SlackApiLambdaHandler implements RequestHandler<ApiGateway
2626
private final App app;
2727

2828
public SlackApiLambdaHandler(App app) {
29-
this.app = app;
3029
this.requestParser = new SlackRequestParser(app.config());
30+
this.app = app;
31+
app.start();
32+
}
33+
34+
/**
35+
* Returns true if the given incoming request is an internal warmup request.
36+
* You can use your own logic for distinguishing requests from Slack from your own internal warmup trigger.
37+
*/
38+
protected abstract boolean isWarmupRequest(ApiGatewayRequest awsReq);
39+
40+
@Override
41+
public ApiGatewayResponse handleRequest(ApiGatewayRequest input, Context context) {
42+
if (isWarmupRequest(input)) {
43+
// This one is always an internal request
44+
// (It's not possible to create this request body via API Gateway)
45+
log.info("Successfully responded to a warmup request ({})", input);
46+
return null;
47+
}
48+
Request<?> req = toSlackRequest(input);
49+
try {
50+
if (req == null) {
51+
return ApiGatewayResponse.builder().statusCode(400).rawBody("Invalid Request").build();
52+
}
53+
return toApiGatewayResponse(app.run(req));
54+
} catch (Exception e) {
55+
log.error("Failed to respond to a request (request: {}, error: {})", input.getBody(), e.getMessage(), e);
56+
// As this response body can be exposed, it should not have detailed information.
57+
return ApiGatewayResponse.builder().statusCode(500).rawBody("Internal Server Error").build();
58+
}
3159
}
3260

33-
protected App getApp() {
61+
// -------------------------------------------
62+
// Extensible internal methods
63+
// -------------------------------------------
64+
65+
/**
66+
* Returns the underlying {@link App} instance in this handler.
67+
*/
68+
protected App app() {
3469
return app;
3570
}
3671

@@ -57,29 +92,9 @@ protected ApiGatewayResponse toApiGatewayResponse(Response slackResp) {
5792
.build();
5893
}
5994

60-
protected abstract boolean isWarmupRequest(ApiGatewayRequest awsReq);
61-
62-
@Override
63-
public ApiGatewayResponse handleRequest(ApiGatewayRequest input, Context context) {
64-
if (isWarmupRequest(input)) {
65-
// This one is always an internal request
66-
// (It's not possible to create this request body via API Gateway)
67-
app.start();
68-
log.info("Successfully responded to a warmup request ({})", input);
69-
return null;
70-
}
71-
Request<?> req = toSlackRequest(input);
72-
try {
73-
if (req == null) {
74-
return ApiGatewayResponse.builder().statusCode(400).rawBody("Invalid Request").build();
75-
}
76-
return toApiGatewayResponse(app.run(req));
77-
} catch (Exception e) {
78-
log.error("Failed to respond to a request (request: {}, error: {})", input.getBody(), e.getMessage(), e);
79-
// As this response body can be exposed, it should not have detailed information.
80-
return ApiGatewayResponse.builder().statusCode(500).rawBody("Internal Server Error").build();
81-
}
82-
}
95+
// -------------------------------------------
96+
// Private utility methods
97+
// -------------------------------------------
8398

8499
private static Map<String, String> toStringToStringMap(Map<String, List<String>> stringToStringListMap) {
85100
Map<String, String> headers = new HashMap<>();

bolt-aws-lambda/src/test/java/test_locally/Issue419UseCaseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected boolean isWarmupRequest(ApiGatewayRequest awsReq) {
4545
}
4646

4747
public void enableEventSubscription() {
48-
App app = getApp();
48+
App app = app();
4949
app.event(MessageEvent.class, (event, ctx) -> {
5050
messageCount.incrementAndGet();
5151
return ctx.ack();

0 commit comments

Comments
 (0)