15
15
16
16
using System ;
17
17
using System . Collections . Generic ;
18
- using System . Net . Http ;
19
18
using System . Text . Json ;
20
- using System . Text . Json . Serialization ;
21
19
using System . Threading . Tasks ;
22
20
using Amazon . DynamoDBv2 ;
23
21
using Amazon . Lambda . APIGatewayEvents ;
24
22
using Amazon . Lambda . Core ;
25
23
using Amazon . Lambda . Serialization . SystemTextJson ;
26
24
using AWS . Lambda . Powertools . Idempotency ;
27
- using AWS . Lambda . Powertools . Idempotency . Persistence ;
28
25
using AWS . Lambda . Powertools . Logging ;
29
26
30
27
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
@@ -34,34 +31,30 @@ namespace HelloWorld;
34
31
35
32
public class Function
36
33
{
37
- private static HttpClient ? _httpClient ;
38
34
private static AmazonDynamoDBClient ? _dynamoDbClient ;
39
35
40
36
/// <summary>
41
37
/// Function constructor
42
38
/// </summary>
43
39
public Function ( )
44
40
{
45
- _httpClient = new HttpClient ( ) ;
46
41
_dynamoDbClient = new AmazonDynamoDBClient ( ) ;
47
42
48
- Init ( _dynamoDbClient , _httpClient ) ;
43
+ Init ( _dynamoDbClient ) ;
49
44
}
50
45
51
46
/// <summary>
52
47
/// Test constructor
53
48
/// </summary>
54
- public Function ( AmazonDynamoDBClient amazonDynamoDb , HttpClient httpClient )
49
+ public Function ( AmazonDynamoDBClient amazonDynamoDb )
55
50
{
56
- _httpClient = httpClient ;
57
51
_dynamoDbClient = amazonDynamoDb ;
58
- Init ( amazonDynamoDb , httpClient ) ;
52
+ Init ( amazonDynamoDb ) ;
59
53
}
60
-
61
- private void Init ( AmazonDynamoDBClient amazonDynamoDb , HttpClient httpClient )
54
+
55
+ private void Init ( AmazonDynamoDBClient amazonDynamoDb )
62
56
{
63
57
ArgumentNullException . ThrowIfNull ( amazonDynamoDb ) ;
64
- ArgumentNullException . ThrowIfNull ( httpClient ) ;
65
58
var tableName = Environment . GetEnvironmentVariable ( "TABLE_NAME" ) ;
66
59
ArgumentNullException . ThrowIfNull ( tableName ) ;
67
60
@@ -92,29 +85,13 @@ private void Init(AmazonDynamoDBClient amazonDynamoDb, HttpClient httpClient)
92
85
[ Logging ( LogEvent = true ) ]
93
86
public async Task < APIGatewayProxyResponse > FunctionHandler ( APIGatewayProxyRequest apigwProxyEvent , ILambdaContext context )
94
87
{
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
-
112
88
var requestContextRequestId = apigwProxyEvent . RequestContext . RequestId ;
113
89
var response = new
114
90
{
115
91
RequestId = requestContextRequestId ,
116
92
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
118
95
} ;
119
96
120
97
try
@@ -138,33 +115,11 @@ public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyReques
138
115
}
139
116
140
117
/// <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)
142
119
/// </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 ( )
167
122
{
168
- Address = address ;
123
+ return Guid . NewGuid ( ) . ToString ( ) ;
169
124
}
170
125
}
0 commit comments