-
Notifications
You must be signed in to change notification settings - Fork 90
feature: Idempotency module #717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 18 commits
4440dce
08c575b
03999db
b1d0262
2ac9f65
4ed575d
a8e0ff5
8ec3b90
a4db313
9d29528
2aa583e
92fafe6
c284584
b2ec97d
32fd1e5
a48a5d3
1803bb9
cd71b5f
3245ba6
69521ec
96eadc0
d302f7a
f713c3c
38522bb
7401f22
c39d3a9
4855a20
81a7052
cefd1f7
1550cc4
221855e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ hs_err_pid* | |
|
||
# Maven build | ||
target/ | ||
native-libs/ | ||
|
||
###################### | ||
# IntelliJ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package helloworld; | ||
|
||
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.amazonaws.services.lambda.runtime.RequestHandler; | ||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; | ||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import software.amazon.lambda.powertools.idempotency.Idempotency; | ||
import software.amazon.lambda.powertools.idempotency.IdempotencyConfig; | ||
import software.amazon.lambda.powertools.idempotency.Idempotent; | ||
import software.amazon.lambda.powertools.idempotency.persistence.DynamoDBPersistenceStore; | ||
import software.amazon.lambda.powertools.utilities.JsonConfig; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.URL; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class AppIdempotency implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would avoid anything new to the examples project. see #726. it will be great to have a working app with demo showcased here instead https://github.com/aws-samples/aws-lambda-powertools-examples There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can i just let it here and also add it to the other repo, now that it's done. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean if you want to leave it here for now thats fine, as long as its added in the example repo too. Eventually examples folder will be gone from the project. We are tracking this against this issue #732 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sample is created, see PR aws-samples/aws-lambda-powertools-examples#14 |
||
private final static Logger LOG = LogManager.getLogger(); | ||
|
||
public AppIdempotency() { | ||
// we need to initialize idempotency configuration before the handleRequest method is called | ||
Idempotency.config().withConfig( | ||
jeromevdl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
IdempotencyConfig.builder() | ||
.withEventKeyJMESPath("powertools_json(body).address") | ||
msailes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.build()) | ||
.withPersistenceStore( | ||
DynamoDBPersistenceStore.builder() | ||
.withTableName("idempotency_table") | ||
.build() | ||
).configure(); | ||
msailes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
|
||
/** | ||
* Try with: | ||
* <pre> | ||
* curl -X POST https://[REST-API-ID].execute-api.[REGION].amazonaws.com/Prod/helloidem/ -H "Content-Type: application/json" -d '{"address": "https://checkip.amazonaws.com"}' | ||
* </pre> | ||
* @param input | ||
* @param context | ||
* @return | ||
*/ | ||
@Idempotent | ||
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) { | ||
Map<String, String> headers = new HashMap<>(); | ||
|
||
headers.put("Content-Type", "application/json"); | ||
headers.put("Access-Control-Allow-Origin", "*"); | ||
headers.put("Access-Control-Allow-Methods", "GET, OPTIONS"); | ||
headers.put("Access-Control-Allow-Headers", "*"); | ||
|
||
APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent() | ||
.withHeaders(headers); | ||
try { | ||
String address = JsonConfig.get().getObjectMapper().readTree(input.getBody()).get("address").asText(); | ||
final String pageContents = this.getPageContents(address); | ||
String output = String.format("{ \"message\": \"hello world\", \"location\": \"%s\" }", pageContents); | ||
|
||
LOG.debug("ip is {}", pageContents); | ||
return response | ||
.withStatusCode(200) | ||
.withBody(output); | ||
|
||
} catch (IOException e) { | ||
return response | ||
.withBody("{}") | ||
.withStatusCode(500); | ||
} | ||
} | ||
|
||
// we could actually also put the @Idempotent annotation here | ||
private String getPageContents(String address) throws IOException { | ||
URL url = new URL(address); | ||
try (BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))) { | ||
return br.lines().collect(Collectors.joining(System.lineSeparator())); | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.