Add `AbpHttpClientProxyingOptions`

pull/10220/head
maliming 4 years ago
parent 4551acda13
commit 6808effa17

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
namespace Volo.Abp.Http.Client.ClientProxying
{
public class AbpHttpClientProxyingOptions
{
public Dictionary<Type, Type> QueryStringConverts { get; set; }
public Dictionary<Type, Type> FormDataConverts { get; set; }
public AbpHttpClientProxyingOptions()
{
QueryStringConverts = new Dictionary<Type, Type>();
FormDataConverts = new Dictionary<Type, Type>();
}
}
}

@ -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<AbpHttpClientProxyingOptions> httpClientProxyingOptions)
{
ServiceScopeFactory = serviceScopeFactory;
HttpClientProxyingOptions = httpClientProxyingOptions.Value;
}
[CanBeNull]
@ -99,18 +103,28 @@ namespace Volo.Abp.Http.Client.ClientProxying
continue;
}
var formDataContents = await (Task<List<KeyValuePair<string, HttpContent>>>)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<List<KeyValuePair<string, HttpContent>>>)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<List<KeyValuePair<string, HttpContent>>> ObjectToFormDataAsync<T>(T value)
protected virtual async Task<List<KeyValuePair<string, HttpContent>>> ObjectToFormDataAsync<T>(IObjectToFormData<T> converter, T value)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var objectToFormData = scope.ServiceProvider.GetService<IObjectToFormData<T>>();
if (objectToFormData != null)
{
return await objectToFormData.ConvertAsync(value);
}
}
return null;
return await converter.ConvertAsync(value);
}
}
}

@ -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<AbpHttpClientProxyingOptions> httpClientProxyingOptions)
{
ServiceScopeFactory = serviceScopeFactory;
HttpClientProxyingOptions = httpClientProxyingOptions.Value;
}
public async Task<string> GenerateUrlWithParametersAsync(ActionApiDescriptionModel action, IReadOnlyDictionary<string, object> methodArguments, ApiVersionInfo apiVersion)
@ -110,16 +113,27 @@ namespace Volo.Abp.Http.Client.ClientProxying
continue;
}
var queryString = await (Task<string>)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<string>)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<string> ObjectToQueryStringAsync<T>(T value)
protected virtual async Task<string> ObjectToQueryStringAsync<T>(IObjectToQueryString<T> converter, T value)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var objectToQueryString = scope.ServiceProvider.GetService<IObjectToQueryString<T>>();
if (objectToQueryString != null)
{
return await objectToQueryString.ConvertAsync(value);
}
}
return null;
return await converter.ConvertAsync(value);
}
protected virtual async Task<bool> AddQueryStringParameterAsync(

@ -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<AbpHttpClientProxyingOptions>(options =>
{
options.QueryStringConverts.Add(typeof(List<GetParamsNameValue>), typeof(TestObjectToQueryString));
options.FormDataConverts.Add(typeof(List<GetParamsNameValue>), typeof(TestObjectToFormData));
});
}
}
}

Loading…
Cancel
Save