1515
1616using System ;
1717using System . Collections . Generic ;
18- using System . Net . Http ;
1918using System . Text . Json ;
20- using System . Text . Json . Serialization ;
2119using System . Threading . Tasks ;
2220using Amazon . DynamoDBv2 ;
2321using Amazon . Lambda . APIGatewayEvents ;
2422using Amazon . Lambda . Core ;
2523using Amazon . Lambda . Serialization . SystemTextJson ;
2624using AWS . Lambda . Powertools . Idempotency ;
27- using AWS . Lambda . Powertools . Idempotency . Persistence ;
2825using AWS . Lambda . Powertools . Logging ;
2926
3027// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
@@ -34,34 +31,30 @@ namespace HelloWorld;
3431
3532public class Function
3633{
37- private static HttpClient ? _httpClient ;
3834 private static AmazonDynamoDBClient ? _dynamoDbClient ;
3935
4036 /// <summary>
4137 /// Function constructor
4238 /// </summary>
4339 public Function ( )
4440 {
45- _httpClient = new HttpClient ( ) ;
4641 _dynamoDbClient = new AmazonDynamoDBClient ( ) ;
4742
48- Init ( _dynamoDbClient , _httpClient ) ;
43+ Init ( _dynamoDbClient ) ;
4944 }
5045
5146 /// <summary>
5247 /// Test constructor
5348 /// </summary>
54- public Function ( AmazonDynamoDBClient amazonDynamoDb , HttpClient httpClient )
49+ public Function ( AmazonDynamoDBClient amazonDynamoDb )
5550 {
56- _httpClient = httpClient ;
5751 _dynamoDbClient = amazonDynamoDb ;
58- Init ( amazonDynamoDb , httpClient ) ;
52+ Init ( amazonDynamoDb ) ;
5953 }
60-
61- private void Init ( AmazonDynamoDBClient amazonDynamoDb , HttpClient httpClient )
54+
55+ private void Init ( AmazonDynamoDBClient amazonDynamoDb )
6256 {
6357 ArgumentNullException . ThrowIfNull ( amazonDynamoDb ) ;
64- ArgumentNullException . ThrowIfNull ( httpClient ) ;
6558 var tableName = Environment . GetEnvironmentVariable ( "TABLE_NAME" ) ;
6659 ArgumentNullException . ThrowIfNull ( tableName ) ;
6760
@@ -92,29 +85,13 @@ private void Init(AmazonDynamoDBClient amazonDynamoDb, HttpClient httpClient)
9285 [ Logging ( LogEvent = true ) ]
9386 public async Task < APIGatewayProxyResponse > FunctionHandler ( APIGatewayProxyRequest apigwProxyEvent , ILambdaContext context )
9487 {
95- var serializationOptions = new JsonSerializerOptions
96- {
97- PropertyNameCaseInsensitive = true
98- } ;
99- var request = JsonSerializer . Deserialize < LookupRequest > ( apigwProxyEvent . Body , serializationOptions ) ;
100- if ( request is null )
101- {
102- return new APIGatewayProxyResponse
103- {
104- Body = "Invalid request" ,
105- StatusCode = 403 ,
106- Headers = new Dictionary < string , string > { { "Content-Type" , "application/json" } }
107- } ;
108- }
109-
110- var location = await GetCallingIp ( request . Address ) ;
111-
11288 var requestContextRequestId = apigwProxyEvent . RequestContext . RequestId ;
11389 var response = new
11490 {
11591 RequestId = requestContextRequestId ,
11692 Greeting = "Hello Powertools for AWS Lambda (.NET)" ,
117- IpAddress = location
93+ MethodGuid = GenerateGuid ( ) , // Guid generated by the GenerateGuid method. used to compare Method output
94+ HandlerGuid = Guid . NewGuid ( ) . ToString ( ) // Guid generated in the Handler. used to compare Handler output
11895 } ;
11996
12097 try
@@ -138,33 +115,11 @@ public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyReques
138115 }
139116
140117 /// <summary>
141- /// Calls location api to return IP address
118+ /// Generates a new Guid to check if value is the same between calls (should be when idempotency enabled)
142119 /// </summary>
143- /// <param name="address">Uri of the service providing the calling IP</param>
144- /// <returns>IP address string</returns>
145- private static async Task < string ? > GetCallingIp ( string address )
146- {
147- if ( _httpClient == null ) return "0.0.0.0" ;
148- _httpClient . DefaultRequestHeaders . Accept . Clear ( ) ;
149- _httpClient . DefaultRequestHeaders . Add ( "User-Agent" , "AWS Lambda .Net Client" ) ;
150-
151- var response = await _httpClient . GetStringAsync ( address ) . ConfigureAwait ( false ) ;
152- var ip = response . Replace ( "\n " , "" ) ;
153-
154- return ip ;
155- }
156- }
157-
158- /// <summary>
159- /// Record to represent the data structure of Lookup request
160- /// </summary>
161- [ Serializable ]
162- public class LookupRequest
163- {
164- public string Address { get ; private set ; }
165-
166- public LookupRequest ( string address )
120+ /// <returns>GUID</returns>
121+ private static string GenerateGuid ( )
167122 {
168- Address = address ;
123+ return Guid . NewGuid ( ) . ToString ( ) ;
169124 }
170125}
0 commit comments