-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Enhanced the API requester to support conversion of HTTP GET pa…
…rameters.
- Loading branch information
Showing
3 changed files
with
91 additions
and
4 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/Common/EasyAbp.Abp.WeChat.Common/Extensions/WeChatReflectionHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.Reflection; | ||
using System.Text; | ||
using Newtonsoft.Json; | ||
|
||
namespace EasyAbp.Abp.WeChat.Common.Extensions; | ||
|
||
public static class WeChatReflectionHelper | ||
{ | ||
// TODO: This method has performance issues because it uses reflection; it can be optimized using expression trees. | ||
public static string ConvertToQueryString(object obj) | ||
{ | ||
var sb = new StringBuilder(); | ||
var properties = obj.GetType().GetProperties(); | ||
foreach (var propertyInfo in properties) | ||
{ | ||
var value = propertyInfo.GetValue(obj); | ||
if (value == null) | ||
{ | ||
continue; | ||
} | ||
|
||
var jsonPropertyAttribute = propertyInfo.GetCustomAttribute<JsonPropertyAttribute>(); | ||
var name = jsonPropertyAttribute?.PropertyName ?? propertyInfo.Name; | ||
var type = propertyInfo.PropertyType; | ||
if (type.IsPrimitive || type == typeof(string)) | ||
{ | ||
sb.Append($"{name}={value}&"); | ||
} | ||
} | ||
|
||
return sb.ToString().TrimEnd('&'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
tests/EasyAbp.Abp.WeChat.Common.Tests/Extensions/WeChatReflectionHelperTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using EasyAbp.Abp.WeChat.Common.Extensions; | ||
using Newtonsoft.Json; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace EasyAbp.Abp.WeChat.Common.Tests.Extensions; | ||
|
||
public class WeChatReflectionHelperTests : AbpWeChatCommonTestBase<AbpWeChatCommonModule> | ||
{ | ||
[Fact] | ||
public void Should_Convert_To_Query_String() | ||
{ | ||
// Arrange | ||
var obj = new | ||
{ | ||
Name = "test", | ||
Age = 18 | ||
}; | ||
|
||
// Act | ||
var queryString = WeChatReflectionHelper.ConvertToQueryString(obj); | ||
|
||
// Assert | ||
queryString.ShouldBe("Name=test&Age=18"); | ||
} | ||
|
||
public class JsonPropertyTestClass | ||
{ | ||
[JsonProperty("test")] | ||
public string Name { get; set; } | ||
|
||
public int Age { get; set; } | ||
} | ||
|
||
[Fact] | ||
public void Should_Convert_To_Query_String_With_Json_Property_Name() | ||
{ | ||
// Arrange & Act | ||
var queryString = WeChatReflectionHelper.ConvertToQueryString(new JsonPropertyTestClass | ||
{ | ||
Name = "admin", | ||
Age = 18 | ||
}); | ||
|
||
// Assert | ||
queryString.ShouldBe("test=admin&Age=18"); | ||
} | ||
} |