using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Amazon.Lambda.APIGatewayEvents;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
// If you rename this namespace, you will need to update the invocation shim
// to match if you intend to test the function with 'amplify mock function'
namespace <%= props.resourceName %>
{
// If you rename this class, you will need to update the invocation shim
// to match if you intend to test the function with 'amplify mock function'
public class <%= props.functionName %>
{
///
/// A Lambda function to respond to HTTP Get methods from API Gateway
///
///
/// The list of blogs
///
/// If you rename this function, you will need to update the invocation shim
/// to match if you intend to test the function with 'amplify mock function'
///
#pragma warning disable CS1998
public async Task LambdaHandler(APIGatewayProxyRequest request, ILambdaContext context)
{
var response = new APIGatewayProxyResponse {
Headers = new Dictionary {
{ "Access-Control-Allow-Origin", "*" },
{ "Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept" }
}
};
string contentType = null;
request.Headers?.TryGetValue("Content-Type", out contentType);
switch (request.HttpMethod) {
case "GET":
context.Logger.LogLine($"Get Request: {request.Path}\n");
response.StatusCode = (int)HttpStatusCode.OK;
response.Body = "{ \"message\": \"Hello AWS Serverless\" }";
response.Headers["Content-Type"] = "application/json";
break;
case "POST":
context.Logger.LogLine($"Post Request: {request.Path}\n");
if (!String.IsNullOrEmpty(contentType)) {
context.Logger.LogLine($"Content type: {contentType}");
}
context.Logger.LogLine($"Body: {request.Body}");
response.StatusCode = (int)HttpStatusCode.OK;
break;
case "PUT":
context.Logger.LogLine($"Put Request: {request.Path}\n");
if (!String.IsNullOrEmpty(contentType)) {
context.Logger.LogLine($"Content type: {contentType}");
}
context.Logger.LogLine($"Body: {request.Body}");
response.StatusCode = (int)HttpStatusCode.OK;
break;
case "DELETE":
context.Logger.LogLine($"Delete Request: {request.Path}\n");
response.StatusCode = (int)HttpStatusCode.OK;
break;
default:
context.Logger.LogLine($"Unrecognized verb {request.HttpMethod}\n");
response.StatusCode = (int)HttpStatusCode.BadRequest;
break;
}
return response;
}
}
}