Skip to content

Commit

Permalink
feat: Enhanced the API requester to support conversion of HTTP GET pa…
Browse files Browse the repository at this point in the history
…rameters.
  • Loading branch information
real-zony committed Jan 6, 2024
1 parent 691d48a commit 27e3162
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 4 deletions.
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('&');
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using EasyAbp.Abp.WeChat.Common.Extensions;
using EasyAbp.Abp.WeChat.Pay.Options;
using EasyAbp.Abp.WeChat.Pay.Security;
using Newtonsoft.Json;
Expand Down Expand Up @@ -43,7 +46,8 @@ public async Task<string> RequestAsync(HttpMethod method, string url, string bod
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.AcceptLanguage.Add(new StringWithQualityHeaderValue(language));
request.Headers.UserAgent.Add(new ProductInfoHeaderValue("EasyAbp.Abp.WeChat.Pay", "1.0.0"));
request.Headers.Add("Authorization", await _authorizationGenerator.GenerateAuthorizationAsync(method, url, body, mchId));
request.Headers.Add("Authorization",
await _authorizationGenerator.GenerateAuthorizationAsync(method, url, body, mchId));

// Sending the request.
var client = await _httpClientFactory.CreateAsync(mchId);
Expand All @@ -54,7 +58,8 @@ public async Task<string> RequestAsync(HttpMethod method, string url, string bod
return await response.Content.ReadAsStringAsync();
}

public async Task<TResponse> RequestAsync<TResponse>(HttpMethod method, string url, string body, string mchId = null)
public async Task<TResponse> RequestAsync<TResponse>(HttpMethod method, string url, string body,
string mchId = null)
{
var responseString = await RequestAsync(method, url, body, mchId);

Expand Down Expand Up @@ -83,7 +88,7 @@ private HttpRequestMessage CreateRequest(HttpMethod method, string url, string b

if (method == HttpMethod.Get)
{
return new HttpRequestMessage(HttpMethod.Get, $"{url}{body}");
return new HttpRequestMessage(HttpMethod.Get, $"{url}?{body}");
}

return new HttpRequestMessage(HttpMethod.Get, url);
Expand All @@ -102,7 +107,8 @@ private string HandleRequestObject(HttpMethod method, object body)
return bodyStr;
}

return null;
// Convert the object to query string.
return WeChatReflectionHelper.ConvertToQueryString(body);
}

protected virtual async Task ValidateResponseAsync(HttpResponseMessage responseMessage)
Expand Down
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");
}
}

0 comments on commit 27e3162

Please sign in to comment.