protected virtual void AddHeaders(IAbpMethodInvocation invocation, ActionApiDescriptionModel action, HttpRequestMessage requestMessage, ApiVersionInfo apiVersion)
        {
            //API Version
            if (!apiVersion.Version.IsNullOrEmpty())
            {
                //TODO: What about other media types?
                requestMessage.Headers.Add("accept", $"{MimeTypes.Text.Plain}; v={apiVersion.Version}");
                requestMessage.Headers.Add("accept", $"{MimeTypes.Application.Json}; v={apiVersion.Version}");
                requestMessage.Headers.Add("api-version", apiVersion.Version);
            }

            //Header parameters
            var headers = action.Parameters.Where(p => p.BindingSourceId == ParameterBindingSources.Header).ToArray();

            foreach (var headerParameter in headers)
            {
                var value = HttpActionParameterHelper.FindParameterValue(invocation.ArgumentsDictionary, headerParameter);
                if (value != null)
                {
                    requestMessage.Headers.Add(headerParameter.Name, value.ToString());
                }
            }

            //CorrelationId
            requestMessage.Headers.Add(CorrelationIdOptions.HttpHeaderName, CorrelationIdProvider.Get());
        }
예제 #2
0
        private static void AddQueryStringParameters(StringBuilder urlBuilder, IList <ParameterApiDescriptionModel> actionParameters, IReadOnlyDictionary <string, object> methodArguments, ApiVersionInfo apiVersion)
        {
            var queryStringParameters = actionParameters
                                        .Where(p => p.BindingSourceId.IsIn(ParameterBindingSources.ModelBinding, ParameterBindingSources.Query))
                                        .ToArray();

            var isFirstParam = true;

            foreach (var queryStringParameter in queryStringParameters)
            {
                var value = HttpActionParameterHelper.FindParameterValue(methodArguments, queryStringParameter);
                if (value == null)
                {
                    continue;
                }

                AddQueryStringParameter(urlBuilder, isFirstParam, queryStringParameter.Name, value);

                isFirstParam = false;
            }

            if (apiVersion.ShouldSendInQueryString())
            {
                AddQueryStringParameter(urlBuilder, isFirstParam, "api-version", apiVersion.Version);  //TODO: Constant!
            }
        }
예제 #3
0
        private static HttpContent GenerateBody(ActionApiDescriptionModel action, IReadOnlyDictionary <string, object> methodArguments, IJsonSerializer jsonSerializer)
        {
            var parameters = action
                             .Parameters
                             .Where(p => p.BindingSourceId == ParameterBindingSources.Body)
                             .ToArray();

            if (parameters.Length <= 0)
            {
                return(null);
            }

            if (parameters.Length > 1)
            {
                throw new AbpException(
                          $"Only one complex type allowed as argument to a controller action that's binding source is 'Body'. But action on URL: {action.Url} contains more than one!"
                          );
            }

            var value = HttpActionParameterHelper.FindParameterValue(methodArguments, parameters[0]);

            if (value == null)
            {
                return(null);
            }

            return(new StringContent(jsonSerializer.Serialize(value), Encoding.UTF8, MimeTypes.Application.Json));
        }
예제 #4
0
        private static HttpContent GenerateFormPostData(ActionApiDescriptionModel action, IReadOnlyDictionary <string, object> methodArguments)
        {
            var parameters = action
                             .Parameters
                             .Where(p => p.BindingSourceId == ParameterBindingSources.Form)
                             .ToArray();

            if (!parameters.Any())
            {
                return(null);
            }

            var postDataBuilder = new StringBuilder();

            var isFirstParam = true;

            foreach (var queryStringParameter in parameters)
            {
                var value = HttpActionParameterHelper.FindParameterValue(methodArguments, queryStringParameter);
                if (value == null)
                {
                    continue;
                }

                postDataBuilder.Append(isFirstParam ? "?" : "&");
                postDataBuilder.Append(queryStringParameter.Name + "=" + System.Net.WebUtility.UrlEncode(value.ToString()));

                isFirstParam = false;
            }

            return(new StringContent(postDataBuilder.ToString(), Encoding.UTF8, MimeTypes.Application.XWwwFormUrlencoded));
        }
예제 #5
0
        protected virtual void AddHeaders(IAbpMethodInvocation invocation, ActionApiDescriptionModel action, HttpRequestMessage requestMessage, ApiVersionInfo apiVersion)
        {
            //API Version
            if (!apiVersion.Version.IsNullOrEmpty())
            {
                //TODO: What about other media types?
                requestMessage.Headers.Add("accept", $"{MimeTypes.Text.Plain}; v={apiVersion.Version}");
                requestMessage.Headers.Add("accept", $"{MimeTypes.Application.Json}; v={apiVersion.Version}");
                requestMessage.Headers.Add("api-version", apiVersion.Version);
            }

            //Header parameters
            var headers = action.Parameters.Where(p => p.BindingSourceId == ParameterBindingSources.Header).ToArray();

            foreach (var headerParameter in headers)
            {
                var value = HttpActionParameterHelper.FindParameterValue(invocation.ArgumentsDictionary, headerParameter);
                if (value != null)
                {
                    requestMessage.Headers.Add(headerParameter.Name, value.ToString());
                }
            }

            //CorrelationId
            requestMessage.Headers.Add(AbpCorrelationIdOptions.HttpHeaderName, CorrelationIdProvider.Get());

            //TenantId
            if (CurrentTenant.Id.HasValue)
            {
                //TODO: Use AbpAspNetCoreMultiTenancyOptions to get the key
                requestMessage.Headers.Add(TenantResolverConsts.DefaultTenantKey, CurrentTenant.Id.Value.ToString());
            }

            //Culture
            //TODO: Is that the way we want? Couldn't send the culture (not ui culture)
            var currentCulture = CultureInfo.CurrentUICulture.Name ?? CultureInfo.CurrentCulture.Name;

            if (!currentCulture.IsNullOrEmpty())
            {
                requestMessage.Headers.AcceptLanguage.Add(new StringWithQualityHeaderValue(currentCulture));
            }

            //X-Requested-With
            requestMessage.Headers.Add("X-Requested-With", "XMLHttpRequest");
        }
예제 #6
0
        private static void ReplacePathVariables(StringBuilder urlBuilder, IList <ParameterApiDescriptionModel> actionParameters, IReadOnlyDictionary <string, object> methodArguments, ApiVersionInfo apiVersion)
        {
            var pathParameters = actionParameters
                                 .Where(p => p.BindingSourceId == ParameterBindingSources.Path)
                                 .ToArray();

            if (!pathParameters.Any())
            {
                return;
            }

            if (pathParameters.Any(p => p.Name == "apiVersion"))
            {
                urlBuilder = urlBuilder.Replace("{apiVersion}", apiVersion.Version);
            }

            foreach (var pathParameter in pathParameters.Where(p => p.Name != "apiVersion")) //TODO: Constant!
            {
                var value = HttpActionParameterHelper.FindParameterValue(methodArguments, pathParameter);

                if (value == null)
                {
                    if (pathParameter.IsOptional)
                    {
                        urlBuilder = urlBuilder.Replace($"{{{pathParameter.Name}}}", "");
                    }
                    else if (pathParameter.DefaultValue != null)
                    {
                        urlBuilder = urlBuilder.Replace($"{{{pathParameter.Name}}}", pathParameter.DefaultValue.ToString());
                    }
                    else
                    {
                        throw new AbpException($"Missing path parameter value for {pathParameter.Name} ({pathParameter.NameOnMethod})");
                    }
                }
                else
                {
                    urlBuilder = urlBuilder.Replace($"{{{pathParameter.Name}}}", value.ToString());
                }
            }
        }
예제 #7
0
        private static void AddHeaders(IAbpMethodInvocation invocation, ActionApiDescriptionModel action, HttpRequestMessage requestMessage, ApiVersionInfo apiVersion)
        {
            if (!apiVersion.Version.IsNullOrEmpty())
            {
                //TODO: What about other media types?
                requestMessage.Headers.Add("accept", $"text/plain; v={apiVersion.Version}");
                requestMessage.Headers.Add("accept", $"application/json; v={apiVersion.Version}");
                requestMessage.Headers.Add("api-version", apiVersion.Version);
            }

            var headers = action.Parameters.Where(p => p.BindingSourceId == ParameterBindingSources.Header).ToArray();

            foreach (var headerParameter in headers)
            {
                var value = HttpActionParameterHelper.FindParameterValue(invocation.ArgumentsDictionary, headerParameter);
                if (value != null)
                {
                    requestMessage.Headers.Add(headerParameter.Name, value.ToString());
                }
            }
        }
예제 #8
0
        private static HttpContent GenerateFormPostData(ActionApiDescriptionModel action, IReadOnlyDictionary <string, object> methodArguments)
        {
            var parameters = action
                             .Parameters
                             .Where(p => p.BindingSourceId == ParameterBindingSources.Form || p.BindingSourceId == ParameterBindingSources.FormFile)
                             .ToArray();

            if (!parameters.Any())
            {
                return(null);
            }

            if (parameters.Any(x => x.BindingSourceId == ParameterBindingSources.FormFile))
            {
                var formData = new MultipartFormDataContent();
                foreach (var parameter in parameters)
                {
                    var value = HttpActionParameterHelper.FindParameterValue(methodArguments, parameter);
                    if (value == null)
                    {
                        continue;
                    }

                    if (value is IRemoteStreamContent remoteStreamContent)
                    {
                        var stream = remoteStreamContent.GetStream();
                        if (stream.CanSeek)
                        {
                            stream.Position = 0;
                        }
                        var streamContent = new StreamContent(stream);
                        if (!remoteStreamContent.ContentType.IsNullOrWhiteSpace())
                        {
                            streamContent.Headers.ContentType = new MediaTypeHeaderValue(remoteStreamContent.ContentType);
                        }
                        formData.Add(streamContent, parameter.Name, parameter.Name);
                    }
                    else if (value is IEnumerable <IRemoteStreamContent> remoteStreamContents)
                    {
                        foreach (var content in remoteStreamContents)
                        {
                            var stream = content.GetStream();
                            if (stream.CanSeek)
                            {
                                stream.Position = 0;
                            }
                            var streamContent = new StreamContent(stream);
                            if (!content.ContentType.IsNullOrWhiteSpace())
                            {
                                streamContent.Headers.ContentType = new MediaTypeHeaderValue(content.ContentType);
                            }
                            formData.Add(streamContent, parameter.Name, parameter.Name);
                        }
                    }
                    else
                    {
                        formData.Add(new StringContent(value.ToString(), Encoding.UTF8), parameter.Name);
                    }
                }

                return(formData);
            }
            else
            {
                var postDataBuilder = new StringBuilder();

                var isFirstParam = true;
                foreach (var parameter in parameters.Where(p => p.BindingSourceId == ParameterBindingSources.Form))
                {
                    var value = HttpActionParameterHelper.FindParameterValue(methodArguments, parameter);
                    if (value == null)
                    {
                        continue;
                    }

                    postDataBuilder.Append(isFirstParam ? "?" : "&");
                    postDataBuilder.Append(parameter.Name + "=" + System.Net.WebUtility.UrlEncode(value.ToString()));

                    isFirstParam = false;
                }

                return(new StringContent(postDataBuilder.ToString(), Encoding.UTF8, MimeTypes.Application.XWwwFormUrlencoded));
            }
        }