1+ /*
2+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License").
5+ * You may not use this file except in compliance with the License.
6+ * A copy of the License is located at
7+ *
8+ * http://aws.amazon.com/apache2.0
9+ *
10+ * or in the "license" file accompanying this file. This file is distributed
11+ * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+ * express or implied. See the License for the specific language governing
13+ * permissions and limitations under the License.
14+ */
15+
16+ using System ;
17+ using System . Collections . Generic ;
18+ using System . IO ;
19+ using System . Net . Http ;
20+ using System . Text . Json ;
21+ using System . Threading . Tasks ;
22+ using Amazon . DynamoDBv2 ;
23+ using Amazon . Lambda . APIGatewayEvents ;
24+ using Amazon . Lambda . Core ;
25+
26+ namespace AWS . Lambda . Powertools . Idempotency . Tests . Handlers ;
27+
28+ public class IdempotencyFunctionMethodDecorated
29+ {
30+ public bool MethodCalled ;
31+
32+ public IdempotencyFunctionMethodDecorated ( AmazonDynamoDBClient client )
33+ {
34+ Idempotency . Configure ( builder =>
35+ builder
36+ . UseDynamoDb ( storeBuilder =>
37+ storeBuilder
38+ . WithTableName ( "idempotency_table" )
39+ . WithDynamoDBClient ( client )
40+ ) ) ;
41+ }
42+
43+
44+ public async Task < APIGatewayProxyResponse > Handle ( APIGatewayProxyRequest apigProxyEvent , ILambdaContext context )
45+ {
46+ Idempotency . RegisterLambdaContext ( context ) ;
47+ var result = await InternalFunctionHandler ( apigProxyEvent ) ;
48+
49+ return result ;
50+ }
51+
52+ private async Task < APIGatewayProxyResponse > InternalFunctionHandler ( APIGatewayProxyRequest apigProxyEvent )
53+ {
54+ Dictionary < string , string > headers = new ( )
55+ {
56+ { "Content-Type" , "application/json" } ,
57+ { "Access-Control-Allow-Origin" , "*" } ,
58+ { "Access-Control-Allow-Methods" , "GET, OPTIONS" } ,
59+ { "Access-Control-Allow-Headers" , "*" }
60+ } ;
61+
62+ try
63+ {
64+ var address = JsonDocument . Parse ( apigProxyEvent . Body ) . RootElement . GetProperty ( "address" ) . GetString ( ) ;
65+ var pageContents = await GetPageContents ( address ) ;
66+ var output = $ "{{ \" message\" : \" hello world\" , \" location\" : \" { pageContents } \" }}";
67+
68+ return new APIGatewayProxyResponse
69+ {
70+ Body = output ,
71+ StatusCode = 200 ,
72+ Headers = headers
73+ } ;
74+
75+ }
76+ catch ( IOException )
77+ {
78+ return new APIGatewayProxyResponse
79+ {
80+ Body = "{}" ,
81+ StatusCode = 500 ,
82+ Headers = headers
83+ } ;
84+ }
85+ }
86+
87+ [ Idempotent ]
88+ private async Task < string > GetPageContents ( string address )
89+ {
90+ MethodCalled = true ;
91+
92+ var client = new HttpClient ( ) ;
93+ using var response = await client . GetAsync ( address ) ;
94+ using var content = response . Content ;
95+ var pageContent = await content . ReadAsStringAsync ( ) ;
96+
97+ return pageContent ;
98+ }
99+ }
0 commit comments