Ejemplo n.º 1
0
        /// <summary>
        /// Attempts to deserialize the <see cref="LambdaInvocationEvent.Payload"/> into one of the expected trigger event objects.
        /// </summary>
        /// <param name="invocationEvent"></param>
        /// <returns></returns>
        private static (APIGatewayProxyRequest api, SQSEvent sqs) GetEventBody(LambdaInvocationEvent invocationEvent)
        {
            var api = JsonConvert.DeserializeObject <APIGatewayProxyRequest>(invocationEvent.Payload);
            var sqs = JsonConvert.DeserializeObject <SQSEvent>(invocationEvent.Payload);

            // The serializer will create the default object regardless of whether any of the fields deserialized.
            // So if the object is missing a key field then set it to null to indicate the JSON was not for the event.
            return((api?.Resource == null ? null : api),
                   (sqs?.Records == null ? null : sqs));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Builds the <see cref="LambdaInvocationEvent"/> from a <see cref="InvocationUrls.NextInvocation"/> response.
        /// </summary>
        /// <param name="response"></param>
        public static LambdaInvocationEvent Build(HttpResponseMessage response)
        {
            LambdaInvocationEvent evnt = new LambdaInvocationEvent();

            var responseContentTask = response.Content.ReadAsStringAsync();

            responseContentTask.Wait();

            evnt.Payload            = responseContentTask.Result;
            evnt.AwsRequestId       = GetHeaderValue(response.Headers, "Lambda-Runtime-Aws-Request-Id");
            evnt.DeadlineTimestamp  = GetHeaderValue(response.Headers, "Lambda-Runtime-Deadline-Ms");
            evnt.InvokedFunctionArn = GetHeaderValue(response.Headers, "Lambda-Runtime-Invoked-Function-Arn");
            evnt.TraceId            = GetHeaderValue(response.Headers, "Lambda-Runtime-Aws-Trace-Id");
            evnt.ClientContext      = GetHeaderValue(response.Headers, "Lambda-Runtime-Aws-Client-Context");
            evnt.CognitoIdentity    = GetHeaderValue(response.Headers, "Lambda-Runtime-Aws-Cognito-Identity");

            return(evnt);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Retrieves the <see cref="LambdaInvocationEvent"/> associated with the currently executing Lambda.
        /// </summary>
        /// <param name="client"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        private static LambdaInvocationEvent GetInvocationEvent(CancellationToken token = default)
        {
            using (var client = new HttpClient())
                using (var request = new HttpRequestMessage())
                {
                    request.Method = new HttpMethod("GET");
                    request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
                    request.RequestUri = new Uri(NextInvocationUrl, UriKind.RelativeOrAbsolute);

                    var responseTask = client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token);
                    responseTask.Wait();

                    var response = responseTask.Result;

                    if ((response.StatusCode != System.Net.HttpStatusCode.OK) &&
                        (response.StatusCode != System.Net.HttpStatusCode.NoContent))
                    {
                        throw new Exception($"API request '{request.RequestUri}' returned error status code {response.StatusCode}");
                    }

                    return(LambdaInvocationEvent.Build(response));
                }
        }