示例#1
0
        public APIGatewayProxyResponse Handler(
            APIGatewayProxyRequest request,
            ILambdaContext context)
        {
            try
            {
                ConfigureLogger(context.AwsRequestId);

                var notification = JsonConvert.DeserializeObject <Notification>(
                    request.Body
                    );

                Log.Logger.RecordNotification(
                    notification
                    );

                var validationFailureReason = notification.Validate(out var isValid);

                if (isValid == false)
                {
                    var errorResult = new ErrorResult(
                        validationFailureReason);

                    Log.Logger.RecordValidationFailureReason(
                        errorResult
                        );

                    return(new APIGatewayProxyResponse
                    {
                        StatusCode = (int)HttpStatusCode.BadRequest,
                        Body = CreateResponseBodyFromErrorResult(
                            errorResult)
                    });
                }

                var smsClient = CreateSmsClient();

                smsClient.Notify(notification);

                return(new APIGatewayProxyResponse
                {
                    StatusCode = (int)HttpStatusCode.OK
                });
            }
            catch (Exception exception)
            {
                var statusCode = HttpStatusCode.InternalServerError;
                switch (exception)
                {
                case InvalidCredentials _:
                    statusCode = HttpStatusCode.Unauthorized;
                    Log.Logger.RecordInvalidCredentials();
                    break;

                case InsufficientCredits _:
                    statusCode = HttpStatusCode.Forbidden;
                    Log.Logger.RecordInsufficientCredits();
                    break;

                default:
                    Log.Logger.RecordException(exception);
                    break;
                }

                return(new APIGatewayProxyResponse
                {
                    StatusCode = (int)statusCode
                });
            }
        }
示例#2
0
 private static string CreateResponseBodyFromErrorResult(
     ErrorResult errorResult)
 {
     return(JsonConvert.SerializeObject(errorResult));
 }