예제 #1
0
        public static RequestDelegate ScheduleHandler <T>(IDictionary defatultEnvironmentVariables, Dictionary <string, string> serverlessEnvironmentVariables, HttpConfig httpConfig, MethodBase handlerMethod, IReadOnlyList <ParameterInfo> parameterInfos, T handler)
        {
            return(async(context) =>
            {
                if (parameterInfos.Count > 2)
                {
                    throw new Exception("Unrecongnized paramter count for Schedule method. Expected 2 paramters max. (object event, ILambdaContext context)");
                }
                // NOTE: KAL-19 unable to check paramter 2 to see if it ILambdaContext. Will allow (object e, object context) which won't work on AWS but will work here.
                // tried https://stackoverflow.com/questions/4963160/how-to-determine-if-a-type-implements-an-interface-with-c-sharp-reflection

                EnvironmentVariable.PrepareEnvironmentVariables(defatultEnvironmentVariables, serverlessEnvironmentVariables, httpConfig.Environment);

                var args = new List <object>();
                if (parameterInfos.Count > 0)
                {
                    var parameter0Type = parameterInfos[0].ParameterType;
                    object @event = DefaultScheduleEvent;
                    if (httpConfig.RequestBody != null)
                    {
                        @event = JsonConvert.DeserializeObject(httpConfig.RequestBody.ToString(), parameter0Type);
                    }
                    else if (context.Request.Body != null)
                    {
                        using (var reader = new StreamReader(context.Request.Body, Encoding.UTF8))
                        {
                            var body = await reader.ReadToEndAsync().ConfigureAwait(false);

                            @event = JsonConvert.DeserializeObject(body, parameter0Type);
                        }
                    }
                    args.Add(@event);
                }
                if (parameterInfos.Count > 1)
                {
                    args.Add(new TestLambdaContext());
                }

                var handlerResponse = handlerMethod.Invoke(handler, parameterInfos.Count == 0 ? null : args.ToArray());

                object response = null;
                if (handlerResponse is Task <string> task)
                {
                    response = await task.ConfigureAwait(false);
                }
                else if (handlerResponse != null)
                {
                    response = handlerResponse;
                }

                context.Response.StatusCode = 200;
                if (response != null)
                {
                    await context.Response.WriteAsync(JsonConvert.SerializeObject(response)).ConfigureAwait(false);
                }
            });
        }
        public static RequestDelegate ApiGatewayHandler <T>(IDictionary defatultEnvironmentVariables, Dictionary <string, string> serverlessEnvironmentVariables, HttpConfig httpConfig, MethodBase handlerMethod, IReadOnlyList <ParameterInfo> parameterInfos, T handler)
        {
            return(async(context) =>
            {
                if (parameterInfos.Count > 2)
                {
                    throw new Exception("Unrecongnized paramter count for HTTP method. Expected 2 paramters max. (object event, ILambdaContext context)");
                }
                // NOTE: KAL-19 unable to check paramter 2 to see if it ILambdaContext. Will allow (object e, object context) which won't work on AWS but will work here.
                // tried https://stackoverflow.com/questions/4963160/how-to-determine-if-a-type-implements-an-interface-with-c-sharp-reflection

                APIGatewayProxyResponse response;
                var apiGatewayProxyRequest = await context.ToApiGatewayProxyRequest(httpConfig.Path).ConfigureAwait(false);

                EnvironmentVariable.PrepareEnvironmentVariables(defatultEnvironmentVariables, serverlessEnvironmentVariables, httpConfig.Environment);

                var args = new List <object>();
                if (parameterInfos.Count > 0)
                {
                    args.Add(apiGatewayProxyRequest);
                }
                if (parameterInfos.Count > 1)
                {
                    args.Add(new TestLambdaContext());
                }

                var handlerResponse = handlerMethod.Invoke(handler, parameterInfos.Count == 0 ? null : args.ToArray());
                if (handlerResponse is Task <APIGatewayProxyResponse> task)
                {
                    response = await task.ConfigureAwait(false);
                }
                else if (handlerResponse is APIGatewayProxyResponse proxyResponse)
                {
                    response = proxyResponse;
                }
                else
                {
                    throw new Exception("The Method did not return an APIGatewayProxyResponse.");
                }

                if (response.Headers.Any())
                {
                    foreach (var header in response.Headers)
                    {
                        context.Response.Headers.Add(header.Key, header.Value);
                    }
                }

                context.Response.StatusCode = response.StatusCode;
                if (response.Body != null)
                {
                    await context.Response.WriteAsync(response.Body).ConfigureAwait(false);
                }
            });
        }