Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ExtraPropertyDictionaryToQueryString/FormData. #17705

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Castle;
using Volo.Abp.Data;
using Volo.Abp.EventBus;
using Volo.Abp.Modularity;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Threading;
using Volo.Abp.Validation;
using Volo.Abp.ExceptionHandling;
using Volo.Abp.Http.Client.ClientProxying;
using Volo.Abp.Http.Client.ClientProxying.ExtraPropertyDictionaryConverts;
using Volo.Abp.Http.Client.DynamicProxying;
using Volo.Abp.RemoteServices;

Expand All @@ -27,5 +31,11 @@ public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddHttpClient();
context.Services.AddTransient(typeof(DynamicHttpProxyInterceptorClientProxy<>));

Configure<AbpHttpClientProxyingOptions>(options =>
{
options.QueryStringConverts.Add(typeof(ExtraPropertyDictionary), typeof(ExtraPropertyDictionaryToQueryString));
options.FormDataConverts.Add(typeof(ExtraPropertyDictionary), typeof(ExtraPropertyDictionaryToFormData));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Http.Modeling;

namespace Volo.Abp.Http.Client.ClientProxying.ExtraPropertyDictionaryConverts;

public class ExtraPropertyDictionaryToFormData : IObjectToFormData<ExtraPropertyDictionary>, ITransientDependency
{
public Task<List<KeyValuePair<string, HttpContent>>> ConvertAsync(ActionApiDescriptionModel actionApiDescription, ParameterApiDescriptionModel parameterApiDescription, ExtraPropertyDictionary extraPropertyDictionary)
{
if (extraPropertyDictionary.IsNullOrEmpty())
{
return Task.FromResult<List<KeyValuePair<string, HttpContent>>>(null!);
}

var formDataContents = new List<KeyValuePair<string, HttpContent>>();
foreach (var item in extraPropertyDictionary)
{
formDataContents.Add(new KeyValuePair<string, HttpContent>($"ExtraProperties[{item.Key}]", new StringContent(item.Value!.ToString()!, Encoding.UTF8)));
}

return Task.FromResult(formDataContents);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Http.Modeling;

namespace Volo.Abp.Http.Client.ClientProxying.ExtraPropertyDictionaryConverts;

public class ExtraPropertyDictionaryToQueryString : IObjectToQueryString<ExtraPropertyDictionary>, ITransientDependency
{
public Task<string> ConvertAsync(ActionApiDescriptionModel actionApiDescription, ParameterApiDescriptionModel parameterApiDescription, ExtraPropertyDictionary extraPropertyDictionary)
{
if (extraPropertyDictionary.IsNullOrEmpty())
{
return Task.FromResult<string>(null!);
}

var sb = new StringBuilder();
foreach (var item in extraPropertyDictionary)
{
sb.Append($"ExtraProperties[{item.Key}]={item.Value}&");
}
if (sb.Length > 0)
{
sb.Remove(sb.Length - 1, 1);
}

return Task.FromResult(sb.ToString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Volo.Abp.Application.Dtos;
using Volo.Abp.AspNetCore.Mvc.Conventions;
using Volo.Abp.Content;
using Volo.Abp.Data;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Http.Client;
using Volo.Abp.TestApp.Application;
Expand Down Expand Up @@ -280,7 +281,7 @@ public async Task CreateMultipleFileAsync()
[Fact]
public async Task GetParamsFromQueryAsync()
{
var result = await _peopleAppService.GetParamsFromQueryAsync(new GetParamsInput()
var input = new GetParamsInput()
{
NameValues = new List<GetParamsNameValue>()
{
Expand All @@ -300,34 +301,39 @@ public async Task GetParamsFromQueryAsync()
Name = "name3",
Value = "value3"
}
});
result.ShouldBe("name1-value1:name2-value2:name3-value3");
};
input.SetProperty("TestProperty", "TestPropertyValue");
foreach (var nameValue in input.NameValues)
{
nameValue.SetProperty("TestPropertyInList", "TestPropertyValueInList");
}

var result = await _peopleAppService.GetParamsFromQueryAsync(input);
result.ShouldBe("name1-value1:TestPropertyValueInList:name2-value2:name3-value3:TestPropertyValue");
}

[Fact]
public async Task GetParamsFromFormAsync()
{
var result = await _peopleAppService.GetParamsFromFormAsync(new GetParamsInput()
var input = new GetParamsInput()
{
NameValues = new List<GetParamsNameValue>()
{
new GetParamsNameValue()
{
Name = "name1",
Value = "value1"
},
new GetParamsNameValue()
{
Name = "name2",
Value = "value2"
}
new GetParamsNameValue() {Name = "name1", Value = "value1"},
new GetParamsNameValue() {Name = "name2", Value = "value2"}
},
NameValue = new GetParamsNameValue()
{
Name = "name3",
Value = "value3"
Name = "name3", Value = "value3"
}
});
result.ShouldBe("name1-value1:name2-value2:name3-value3");
};
input.SetProperty("TestProperty", "TestPropertyValue");
foreach (var nameValue in input.NameValues)
{
nameValue.SetProperty("TestPropertyInList", "TestPropertyValueInList");
}

var result = await _peopleAppService.GetParamsFromFormAsync(input);
result.ShouldBe("name1-value1:TestPropertyValueInList:name2-value2:name3-value3:TestPropertyValue");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public Task<List<KeyValuePair<string, HttpContent>>> ConvertAsync(ActionApiDescr
{
formDataContents.Add(new KeyValuePair<string, HttpContent>($"NameValues[{i}].Name", new StringContent(values[i].Name, Encoding.UTF8)));
formDataContents.Add(new KeyValuePair<string, HttpContent>($"NameValues[{i}].Value", new StringContent(values[i].Value, Encoding.UTF8)));

foreach (var item in values[i].ExtraProperties)
{
formDataContents.Add(new KeyValuePair<string, HttpContent>($"NameValues[{i}].ExtraProperties[{item.Key}]", new StringContent(item.Value!.ToString()!, Encoding.UTF8)));
}
}

return Task.FromResult(formDataContents);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public Task<string> ConvertAsync(ActionApiDescriptionModel actionApiDescription,
for (var i = 0; i < values.Count; i++)
{
sb.Append($"NameValues[{i}].Name={values[i].Name}&NameValues[{i}].Value={values[i].Value}&");
foreach (var item in values[i].ExtraProperties)
{
sb.Append($"NameValues[{i}].ExtraProperties[{item.Key}]={item.Value}&");
}
}

sb.Remove(sb.Length - 1, 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using System.Collections.Generic;
using Volo.Abp.ObjectExtending;

namespace Volo.Abp.TestApp.Application.Dto;

public class GetParamsInput
public class GetParamsInput : ExtensibleObject
{
public List<GetParamsNameValue> NameValues { get; set; }

public GetParamsNameValue NameValue { get; set; }
}

public class GetParamsNameValue
public class GetParamsNameValue : ExtensibleObject
{
public string Name { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,19 @@ public async Task<string> CreateMultipleFileAsync(CreateMultipleFileInput input)

public Task<string> GetParamsFromQueryAsync([FromQuery] GetParamsInput input)
{
return Task.FromResult(input.NameValues?.FirstOrDefault()?.Name + "-" +
input.NameValues?.FirstOrDefault()?.Value + ":" +
return Task.FromResult(input.NameValues?.FirstOrDefault()?.Name + "-" + input.NameValues?.FirstOrDefault()?.Value + ":" +
input.NameValues?.FirstOrDefault()?.ExtraProperties["TestPropertyInList"] + ":" +
input.NameValues?.LastOrDefault()?.Name + "-" + input.NameValues?.LastOrDefault()?.Value + ":" +
input.NameValue?.Name + "-" + input.NameValue?.Value);
input.NameValue?.Name + "-" + input.NameValue?.Value + ":" +
input.ExtraProperties["TestProperty"]);
}

public Task<string> GetParamsFromFormAsync([FromForm] GetParamsInput input)
{
return Task.FromResult(input.NameValues?.FirstOrDefault()?.Name + "-" +
input.NameValues?.FirstOrDefault()?.Value + ":" +
return Task.FromResult(input.NameValues?.FirstOrDefault()?.Name + "-" + input.NameValues?.FirstOrDefault()?.Value + ":" +
input.NameValues?.FirstOrDefault()?.ExtraProperties["TestPropertyInList"] + ":" +
input.NameValues?.LastOrDefault()?.Name + "-" + input.NameValues?.LastOrDefault()?.Value + ":" +
input.NameValue?.Name + "-" + input.NameValue?.Value);
input.NameValue?.Name + "-" + input.NameValue?.Value + ":" +
input.ExtraProperties["TestProperty"]);
}
}
Loading