From 6808effa17fb09ae8dc090bb94af746a9945d0c9 Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 6 Oct 2021 11:53:19 +0800 Subject: [PATCH] Add `AbpHttpClientProxyingOptions` --- .../AbpHttpClientProxyingOptions.cs | 18 ++++++++ .../ClientProxyRequestPayloadBuilder.cs | 46 +++++++++++-------- .../ClientProxying/ClientProxyUrlBuilder.cs | 46 +++++++++++-------- .../Volo/Abp/Http/AbpHttpClientTestModule.cs | 11 ++++- 4 files changed, 80 insertions(+), 41 deletions(-) create mode 100644 framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/AbpHttpClientProxyingOptions.cs diff --git a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/AbpHttpClientProxyingOptions.cs b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/AbpHttpClientProxyingOptions.cs new file mode 100644 index 0000000000..87ee7de37c --- /dev/null +++ b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/AbpHttpClientProxyingOptions.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; + +namespace Volo.Abp.Http.Client.ClientProxying +{ + public class AbpHttpClientProxyingOptions + { + public Dictionary QueryStringConverts { get; set; } + + public Dictionary FormDataConverts { get; set; } + + public AbpHttpClientProxyingOptions() + { + QueryStringConverts = new Dictionary(); + FormDataConverts = new Dictionary(); + } + } +} diff --git a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/ClientProxyRequestPayloadBuilder.cs b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/ClientProxyRequestPayloadBuilder.cs index f76afae6a3..cee57d7663 100644 --- a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/ClientProxyRequestPayloadBuilder.cs +++ b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/ClientProxyRequestPayloadBuilder.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading.Tasks; using JetBrains.Annotations; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using Volo.Abp.Content; using Volo.Abp.DependencyInjection; using Volo.Abp.Http.Client.Proxying; @@ -30,9 +31,12 @@ namespace Volo.Abp.Http.Client.ClientProxying protected IServiceScopeFactory ServiceScopeFactory { get; } - public ClientProxyRequestPayloadBuilder(IServiceScopeFactory serviceScopeFactory) + protected AbpHttpClientProxyingOptions HttpClientProxyingOptions { get; } + + public ClientProxyRequestPayloadBuilder(IServiceScopeFactory serviceScopeFactory, IOptions httpClientProxyingOptions) { ServiceScopeFactory = serviceScopeFactory; + HttpClientProxyingOptions = httpClientProxyingOptions.Value; } [CanBeNull] @@ -99,18 +103,28 @@ namespace Volo.Abp.Http.Client.ClientProxying continue; } - var formDataContents = await (Task>>)CallObjectToFormDataAsyncMethod - .MakeGenericMethod(value.GetType()) - .Invoke(this, new object[] { value }); - - if (formDataContents != null) + if (HttpClientProxyingOptions.FormDataConverts.ContainsKey(value.GetType())) { - foreach (var content in formDataContents) + using (var scope = ServiceScopeFactory.CreateScope()) { - formData.Add(content.Value, content.Key); + var formDataContents = await (Task>>)CallObjectToFormDataAsyncMethod + .MakeGenericMethod(value.GetType()) + .Invoke(this, new object[] + { + scope.ServiceProvider.GetRequiredService( + typeof(IObjectToFormData<>).MakeGenericType(value.GetType())), + value + }); + + if (formDataContents != null) + { + foreach (var content in formDataContents) + { + formData.Add(content.Value, content.Key); + } + continue; + } } - - continue; } if (value is IRemoteStreamContent remoteStreamContent) @@ -147,17 +161,9 @@ namespace Volo.Abp.Http.Client.ClientProxying return formData; } - protected virtual async Task>> ObjectToFormDataAsync(T value) + protected virtual async Task>> ObjectToFormDataAsync(IObjectToFormData converter, T value) { - using (var scope = ServiceScopeFactory.CreateScope()) - { - var objectToFormData = scope.ServiceProvider.GetService>(); - if (objectToFormData != null) - { - return await objectToFormData.ConvertAsync(value); - } - } - return null; + return await converter.ConvertAsync(value); } } } diff --git a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/ClientProxyUrlBuilder.cs b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/ClientProxyUrlBuilder.cs index 97b9d747cf..e59cac6911 100644 --- a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/ClientProxyUrlBuilder.cs +++ b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/ClientProxyUrlBuilder.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading.Tasks; using JetBrains.Annotations; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using Volo.Abp.DependencyInjection; using Volo.Abp.Http.Client.Proxying; using Volo.Abp.Http.Modeling; @@ -28,10 +29,12 @@ namespace Volo.Abp.Http.Client.ClientProxying } protected IServiceScopeFactory ServiceScopeFactory { get; } + protected AbpHttpClientProxyingOptions HttpClientProxyingOptions { get; } - public ClientProxyUrlBuilder(IServiceScopeFactory serviceScopeFactory) + public ClientProxyUrlBuilder(IServiceScopeFactory serviceScopeFactory, IOptions httpClientProxyingOptions) { ServiceScopeFactory = serviceScopeFactory; + HttpClientProxyingOptions = httpClientProxyingOptions.Value; } public async Task GenerateUrlWithParametersAsync(ActionApiDescriptionModel action, IReadOnlyDictionary methodArguments, ApiVersionInfo apiVersion) @@ -110,16 +113,27 @@ namespace Volo.Abp.Http.Client.ClientProxying continue; } - var queryString = await (Task)CallObjectToQueryStringAsyncMethod - .MakeGenericMethod(value.GetType()) - .Invoke(this, new object[] { value }); - - if (queryString != null) + if (HttpClientProxyingOptions.QueryStringConverts.ContainsKey(value.GetType())) { - urlBuilder.Append(isFirstParam ? "?" : "&"); - urlBuilder.Append(queryString); - isFirstParam = false; - continue; + using (var scope = ServiceScopeFactory.CreateScope()) + { + var queryString = await (Task)CallObjectToQueryStringAsyncMethod + .MakeGenericMethod(value.GetType()) + .Invoke(this, new object[] + { + scope.ServiceProvider.GetRequiredService( + typeof(IObjectToQueryString<>).MakeGenericType(value.GetType())), + value + }); + + if (queryString != null) + { + urlBuilder.Append(isFirstParam ? "?" : "&"); + urlBuilder.Append(queryString); + isFirstParam = false; + continue; + } + } } if (await AddQueryStringParameterAsync(urlBuilder, isFirstParam, queryStringParameter.Name, value)) @@ -134,17 +148,9 @@ namespace Volo.Abp.Http.Client.ClientProxying } } - protected virtual async Task ObjectToQueryStringAsync(T value) + protected virtual async Task ObjectToQueryStringAsync(IObjectToQueryString converter, T value) { - using (var scope = ServiceScopeFactory.CreateScope()) - { - var objectToQueryString = scope.ServiceProvider.GetService>(); - if (objectToQueryString != null) - { - return await objectToQueryString.ConvertAsync(value); - } - } - return null; + return await converter.ConvertAsync(value); } protected virtual async Task AddQueryStringParameterAsync( diff --git a/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/AbpHttpClientTestModule.cs b/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/AbpHttpClientTestModule.cs index 966ab01840..85713929cf 100644 --- a/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/AbpHttpClientTestModule.cs +++ b/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/AbpHttpClientTestModule.cs @@ -1,7 +1,9 @@ -using Microsoft.Extensions.DependencyInjection; +using System.Collections.Generic; +using Microsoft.Extensions.DependencyInjection; using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc.Conventions; using Volo.Abp.Http.Client; +using Volo.Abp.Http.Client.ClientProxying; using Volo.Abp.Http.DynamicProxying; using Volo.Abp.Http.Localization; using Volo.Abp.Localization; @@ -52,6 +54,13 @@ namespace Volo.Abp.Http options.ConventionalControllers.FormBodyBindingIgnoredTypes.Add(typeof(CreateFileInput)); options.ConventionalControllers.FormBodyBindingIgnoredTypes.Add(typeof(CreateMultipleFileInput)); }); + + + Configure(options => + { + options.QueryStringConverts.Add(typeof(List), typeof(TestObjectToQueryString)); + options.FormDataConverts.Add(typeof(List), typeof(TestObjectToFormData)); + }); } } }