|
| 1 | +/* |
| 2 | + * Copyright 2023 Amazon.com, Inc. or its affiliates. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the |
| 4 | + * "License"); you may not use this file except in compliance |
| 5 | + * with the License. You may obtain a copy of the License at |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + * |
| 13 | + */ |
| 14 | + |
| 15 | +package software.amazon.lambda.powertools.e2e; |
| 16 | + |
| 17 | +import java.time.Duration; |
| 18 | +import java.time.Instant; |
| 19 | +import java.time.format.DateTimeFormatter; |
| 20 | +import java.time.temporal.ChronoUnit; |
| 21 | +import java.util.TimeZone; |
| 22 | + |
| 23 | +import com.amazonaws.services.lambda.runtime.Context; |
| 24 | +import com.amazonaws.services.lambda.runtime.RequestHandler; |
| 25 | + |
| 26 | +import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient; |
| 27 | +import software.amazon.awssdk.regions.Region; |
| 28 | +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; |
| 29 | +import software.amazon.lambda.powertools.idempotency.Idempotency; |
| 30 | +import software.amazon.lambda.powertools.idempotency.IdempotencyConfig; |
| 31 | +import software.amazon.lambda.powertools.idempotency.PowertoolsIdempotency; |
| 32 | +import software.amazon.lambda.powertools.idempotency.persistence.dynamodb.DynamoDBPersistenceStore; |
| 33 | + |
| 34 | +public class Function implements RequestHandler<Input, String> { |
| 35 | + |
| 36 | + public Function() { |
| 37 | + this(DynamoDbClient |
| 38 | + .builder() |
| 39 | + .httpClient(UrlConnectionHttpClient.builder().build()) |
| 40 | + .region(Region.of(System.getenv("AWS_REGION"))) |
| 41 | + .build()); |
| 42 | + } |
| 43 | + |
| 44 | + public Function(DynamoDbClient client) { |
| 45 | + Idempotency.config().withConfig( |
| 46 | + IdempotencyConfig.builder() |
| 47 | + .withExpiration(Duration.of(10, ChronoUnit.SECONDS)) |
| 48 | + .build()) |
| 49 | + .withPersistenceStore( |
| 50 | + DynamoDBPersistenceStore.builder() |
| 51 | + .withDynamoDbClient(client) |
| 52 | + .withTableName(System.getenv("IDEMPOTENCY_TABLE")) |
| 53 | + .build()) |
| 54 | + .configure(); |
| 55 | + } |
| 56 | + |
| 57 | + public String handleRequest(Input input, Context context) { |
| 58 | + Idempotency.registerLambdaContext(context); |
| 59 | + |
| 60 | + return PowertoolsIdempotency.makeIdempotent(this::processRequest, input, String.class); |
| 61 | + } |
| 62 | + |
| 63 | + private String processRequest(Input input) { |
| 64 | + DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME.withZone(TimeZone.getTimeZone("UTC").toZoneId()); |
| 65 | + return dtf.format(Instant.now()); |
| 66 | + } |
| 67 | +} |
0 commit comments