Пример #1
0
        protected async Task <bool> Respond(SlackRequest request, SlackResponse response)
        {
            var httpResponse = await HttpClient.SendAsync(new HttpRequestMessage {
                RequestUri = new Uri(request.ResponseUrl),
                Method     = HttpMethod.Post,
                Content    = new StringContent(_serializer.Serialize(response))
            });

            return(httpResponse.StatusCode == HttpStatusCode.OK);
        }
        private static Func <APIGatewayProxyRequest, InvocationTargetState, object?> CreateParameterResolver(ILambdaJsonSerializer serializer, MethodInfo method, ParameterInfo parameter)
        {
            // check if [FromUri] or [FromBody] attributes are present
            var hasFromUriAttribute  = parameter.GetCustomAttribute <FromUriAttribute>() != null;
            var hasFromBodyAttribute = parameter.GetCustomAttribute <FromBodyAttribute>() != null;

            if (hasFromUriAttribute && hasFromBodyAttribute)
            {
                throw new ApiGatewayInvocationTargetParameterException(method, parameter, "cannot have both [FromUri] and [FromBody] attributes");
            }

            // check if parameter is a proxy request
            var isProxyRequest = parameter.ParameterType == typeof(APIGatewayProxyRequest);

            if (isProxyRequest)
            {
                // parameter is the proxy request itself
                return((request, state) => request);
            }

            // check if parameter needs to deserialized from URI or BODY
            var isSimpleType = parameter.ParameterType.IsValueType || (parameter.ParameterType == typeof(string));

            if ((isSimpleType && !hasFromBodyAttribute) || hasFromUriAttribute)
            {
                // check if parameter is read from URI string directly or if its members are read from the URI string
                if (isSimpleType)
                {
                    // create function for getting default parameter value
                    Func <object?> getDefaultValue;
                    if (parameter.IsOptional)
                    {
                        getDefaultValue = () => parameter.DefaultValue;
                    }
                    else if ((Nullable.GetUnderlyingType(parameter.ParameterType) == null) && (parameter.ParameterType.IsValueType || parameter.ParameterType == typeof(string)))
                    {
                        getDefaultValue = () => throw new ApiGatewayInvocationTargetParameterException(method, parameter, "missing value");
                    }
                    else
                    {
                        getDefaultValue = () => null;
                    }

                    // create function to resolve parameter
                    return((request, state) => {
                        string?value = null;

                        // attempt to resolve the parameter from stage variables, path parameters, and query string parameters
                        var success = (request.PathParameters?.TryGetValue(parameter.Name, out value) ?? false) ||
                                      (request.QueryStringParameters?.TryGetValue(parameter.Name, out value) ?? false);

                        // if resolved, return the converted value; otherwise the default value
                        if (success)
                        {
                            try {
                                return Convert.ChangeType(value, parameter.ParameterType);
                            } catch (FormatException) {
                                throw new ApiGatewayInvocationTargetParameterException(method, parameter, "invalid parameter format");
                            }
                        }
                        return getDefaultValue();
                    });
                }
                else
                {
                    var queryParameterType = parameter.ParameterType;

                    // parameter represents the query-string key-value pairs
                    return((request, state) => {
                        try {
                            // construct a unified JSON representation of the path and query-string parameters
                            if (state.PathQueryParametersJson == null)
                            {
                                var pathParameters = request.PathParameters ?? Enumerable.Empty <KeyValuePair <string, string> >();
                                var queryStringParameters = request.QueryStringParameters ?? Enumerable.Empty <KeyValuePair <string, string> >();
                                state.PathQueryParametersJson = serializer.Serialize(
                                    pathParameters.Union(queryStringParameters)
                                    .GroupBy(kv => kv.Key)
                                    .Select(grouping => grouping.First())
                                    .ToDictionary(kv => kv.Key, kv => kv.Value)
                                    );
                            }
                            return serializer.Deserialize(state.PathQueryParametersJson, parameter.ParameterType);
                        } catch {
                            throw new ApiGatewayInvocationTargetParameterException(method, parameter, "invalid path/query-string parameters format");
                        }
                    });
                }
            }
            else
            {
                // parameter represents the body of the request
                return((request, state) => {
                    try {
                        return serializer.Deserialize(request.Body, parameter.ParameterType);
                    } catch {
                        throw new ApiGatewayInvocationTargetParameterException(method, parameter, "invalid JSON document in request body");
                    }
                });
            }
        }