mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-07-16 07:59:44 +08:00
feat: 升级公共组件
This commit is contained in:
parent
0c78b72f74
commit
d4717ba570
@ -168,7 +168,7 @@
|
||||
<Reference Include="Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="SKIT.FlurlHttpClient.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
|
||||
<Reference Include="SKIT.FlurlHttpClient.Common, Version=2.1.1.0, Culture=neutral, PublicKeyToken=null" />
|
||||
<Reference Include="WebMatrix.Data, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\Microsoft.AspNet.WebPages.Data.3.2.7\lib\net45\WebMatrix.Data.dll</HintPath>
|
||||
</Reference>
|
||||
|
@ -27,7 +27,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="SKIT.FlurlHttpClient.Common" Version="2.0.0" />
|
||||
<PackageReference Include="SKIT.FlurlHttpClient.Common" Version="2.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -1,8 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Flurl.Http;
|
||||
@ -85,10 +82,12 @@ namespace SKIT.FlurlHttpClient.Wechat.Ads
|
||||
public async Task<T> SendRequestAsync<T>(IFlurlRequest flurlRequest, HttpContent? httpContent = null, CancellationToken cancellationToken = default)
|
||||
where T : WechatAdsResponse, new()
|
||||
{
|
||||
if (flurlRequest == null) throw new ArgumentNullException(nameof(flurlRequest));
|
||||
|
||||
try
|
||||
{
|
||||
using IFlurlResponse flurlResponse = await base.SendRequestAsync(flurlRequest, httpContent, cancellationToken).ConfigureAwait(false);
|
||||
return await GetResposneAsync<T>(flurlResponse).ConfigureAwait(false);
|
||||
return await WrapResponseWithJsonAsync<T>(flurlResponse, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (FlurlHttpException ex)
|
||||
{
|
||||
@ -107,39 +106,23 @@ namespace SKIT.FlurlHttpClient.Wechat.Ads
|
||||
public async Task<T> SendRequestWithJsonAsync<T>(IFlurlRequest flurlRequest, object? data = null, CancellationToken cancellationToken = default)
|
||||
where T : WechatAdsResponse, new()
|
||||
{
|
||||
if (flurlRequest == null) throw new ArgumentNullException(nameof(flurlRequest));
|
||||
|
||||
try
|
||||
{
|
||||
using IFlurlResponse flurlResponse = await base.SendRequestWithJsonAsync(flurlRequest, data, cancellationToken).ConfigureAwait(false);
|
||||
return await GetResposneAsync<T>(flurlResponse).ConfigureAwait(false);
|
||||
bool isSimpleRequest = data == null ||
|
||||
flurlRequest.Verb == HttpMethod.Get ||
|
||||
flurlRequest.Verb == HttpMethod.Head ||
|
||||
flurlRequest.Verb == HttpMethod.Options;
|
||||
using IFlurlResponse flurlResponse = isSimpleRequest ?
|
||||
await base.SendRequestAsync(flurlRequest, null, cancellationToken).ConfigureAwait(false):
|
||||
await base.SendRequestWithJsonAsync(flurlRequest, data, cancellationToken).ConfigureAwait(false);
|
||||
return await WrapResponseWithJsonAsync<T>(flurlResponse, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (FlurlHttpException ex)
|
||||
{
|
||||
throw new WechatAdsException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<T> GetResposneAsync<T>(IFlurlResponse flurlResponse)
|
||||
where T : WechatAdsResponse, new()
|
||||
{
|
||||
byte[] bytes = await flurlResponse.GetBytesAsync().ConfigureAwait(false);
|
||||
bool jsonable =
|
||||
(bytes.FirstOrDefault() == 91 && bytes.LastOrDefault() == 93) || // "[...]"
|
||||
(bytes.FirstOrDefault() == 123 && bytes.LastOrDefault() == 125); // "{...}"
|
||||
|
||||
T result = jsonable ? JsonSerializer.Deserialize<T>(Encoding.UTF8.GetString(bytes)) : new T();
|
||||
|
||||
result.RawStatus = flurlResponse.StatusCode;
|
||||
result.RawHeaders = new ReadOnlyDictionary<string, string>(
|
||||
flurlResponse.Headers
|
||||
.GroupBy(e => e.Name)
|
||||
.ToDictionary(
|
||||
k => k.Key,
|
||||
v => string.Join(", ", v.Select(e => e.Value))
|
||||
)
|
||||
);
|
||||
result.RawBytes = bytes;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,26 +8,53 @@ namespace SKIT.FlurlHttpClient.Wechat.Ads
|
||||
/// </summary>
|
||||
public abstract class WechatAdsResponse : ICommonResponse
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
int ICommonResponse.RawStatus { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
IDictionary<string, string> ICommonResponse.RawHeaders { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
byte[] ICommonResponse.RawBytes { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应状态码。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public int RawStatus { get; internal set; }
|
||||
public int RawStatus
|
||||
{
|
||||
get { return ((ICommonResponse)this).RawStatus; }
|
||||
internal set { ((ICommonResponse)this).RawStatus = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应表头集合。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public IDictionary<string, string> RawHeaders { get; internal set; } = default!;
|
||||
public IDictionary<string, string> RawHeaders
|
||||
{
|
||||
get { return ((ICommonResponse)this).RawHeaders; }
|
||||
internal set { ((ICommonResponse)this).RawHeaders = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应正文。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public byte[] RawBytes { get; internal set; } = default!;
|
||||
public byte[] RawBytes
|
||||
{
|
||||
get { return ((ICommonResponse)this).RawBytes; }
|
||||
internal set { ((ICommonResponse)this).RawBytes = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取微信广告平台 API 返回的错误码。
|
||||
|
@ -27,7 +27,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="SKIT.FlurlHttpClient.Common" Version="2.0.0" />
|
||||
<PackageReference Include="SKIT.FlurlHttpClient.Common" Version="2.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -1,8 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Flurl.Http;
|
||||
@ -74,10 +71,12 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
|
||||
public async Task<T> SendRequestAsync<T>(IFlurlRequest flurlRequest, HttpContent? httpContent = null, CancellationToken cancellationToken = default)
|
||||
where T : WechatApiResponse, new()
|
||||
{
|
||||
if (flurlRequest == null) throw new ArgumentNullException(nameof(flurlRequest));
|
||||
|
||||
try
|
||||
{
|
||||
using IFlurlResponse flurlResponse = await base.SendRequestAsync(flurlRequest, httpContent, cancellationToken).ConfigureAwait(false);
|
||||
return await GetResposneAsync<T>(flurlResponse).ConfigureAwait(false);
|
||||
return await WrapResponseWithJsonAsync<T>(flurlResponse, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (FlurlHttpException ex)
|
||||
{
|
||||
@ -96,39 +95,23 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
|
||||
public async Task<T> SendRequestWithJsonAsync<T>(IFlurlRequest flurlRequest, object? data = null, CancellationToken cancellationToken = default)
|
||||
where T : WechatApiResponse, new()
|
||||
{
|
||||
if (flurlRequest == null) throw new ArgumentNullException(nameof(flurlRequest));
|
||||
|
||||
try
|
||||
{
|
||||
using IFlurlResponse flurlResponse = await base.SendRequestWithJsonAsync(flurlRequest, data, cancellationToken).ConfigureAwait(false);
|
||||
return await GetResposneAsync<T>(flurlResponse).ConfigureAwait(false);
|
||||
bool isSimpleRequest = data == null ||
|
||||
flurlRequest.Verb == HttpMethod.Get ||
|
||||
flurlRequest.Verb == HttpMethod.Head ||
|
||||
flurlRequest.Verb == HttpMethod.Options;
|
||||
using IFlurlResponse flurlResponse = isSimpleRequest ?
|
||||
await base.SendRequestAsync(flurlRequest, null, cancellationToken).ConfigureAwait(false) :
|
||||
await base.SendRequestWithJsonAsync(flurlRequest, data, cancellationToken).ConfigureAwait(false);
|
||||
return await WrapResponseWithJsonAsync<T>(flurlResponse, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (FlurlHttpException ex)
|
||||
{
|
||||
throw new WechatApiException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<T> GetResposneAsync<T>(IFlurlResponse flurlResponse)
|
||||
where T : WechatApiResponse, new()
|
||||
{
|
||||
byte[] bytes = await flurlResponse.GetBytesAsync().ConfigureAwait(false);
|
||||
bool jsonable =
|
||||
(bytes.FirstOrDefault() == 91 && bytes.LastOrDefault() == 93) || // "[...]"
|
||||
(bytes.FirstOrDefault() == 123 && bytes.LastOrDefault() == 125); // "{...}"
|
||||
|
||||
T result = jsonable ? JsonSerializer.Deserialize<T>(Encoding.UTF8.GetString(bytes)) : new T();
|
||||
|
||||
result.RawStatus = flurlResponse.StatusCode;
|
||||
result.RawHeaders = new ReadOnlyDictionary<string, string>(
|
||||
flurlResponse.Headers
|
||||
.GroupBy(e => e.Name)
|
||||
.ToDictionary(
|
||||
k => k.Key,
|
||||
v => string.Join(", ", v.Select(e => e.Value))
|
||||
)
|
||||
);
|
||||
result.RawBytes = bytes;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,26 +8,53 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
|
||||
/// </summary>
|
||||
public abstract class WechatApiResponse : ICommonResponse
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
int ICommonResponse.RawStatus { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
IDictionary<string, string> ICommonResponse.RawHeaders { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
byte[] ICommonResponse.RawBytes { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应状态码。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public int RawStatus { get; internal set; }
|
||||
public int RawStatus
|
||||
{
|
||||
get { return ((ICommonResponse)this).RawStatus; }
|
||||
internal set { ((ICommonResponse)this).RawStatus = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应表头集合。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public IDictionary<string, string> RawHeaders { get; internal set; } = default!;
|
||||
public IDictionary<string, string> RawHeaders
|
||||
{
|
||||
get { return ((ICommonResponse)this).RawHeaders; }
|
||||
internal set { ((ICommonResponse)this).RawHeaders = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应正文。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public byte[] RawBytes { get; internal set; } = default!;
|
||||
public byte[] RawBytes
|
||||
{
|
||||
get { return ((ICommonResponse)this).RawBytes; }
|
||||
internal set { ((ICommonResponse)this).RawBytes = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取微信 API 返回的错误码。
|
||||
|
@ -27,8 +27,8 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="JWT" Version="8.4.2" />
|
||||
<PackageReference Include="SKIT.FlurlHttpClient.Common" Version="2.0.0" />
|
||||
<PackageReference Include="JWT" Version="8.8.1" />
|
||||
<PackageReference Include="SKIT.FlurlHttpClient.Common" Version="2.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -1,8 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Flurl.Http;
|
||||
@ -75,10 +72,12 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||
public async Task<T> SendRequestAsync<T>(IFlurlRequest flurlRequest, HttpContent? httpContent = null, CancellationToken cancellationToken = default)
|
||||
where T : WechatOpenAIPlatformResponse, new()
|
||||
{
|
||||
if (flurlRequest == null) throw new ArgumentNullException(nameof(flurlRequest));
|
||||
|
||||
try
|
||||
{
|
||||
using IFlurlResponse flurlResponse = await base.SendRequestAsync(flurlRequest, httpContent, cancellationToken).ConfigureAwait(false);
|
||||
return await GetResposneAsync<T>(flurlResponse).ConfigureAwait(false);
|
||||
return await WrapResponseWithJsonAsync<T>(flurlResponse, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (FlurlHttpException ex)
|
||||
{
|
||||
@ -97,6 +96,8 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||
public async Task<T> SendRequestWithJsonAsync<T>(IFlurlRequest flurlRequest, object? data = null, CancellationToken cancellationToken = default)
|
||||
where T : WechatOpenAIPlatformResponse, new()
|
||||
{
|
||||
if (flurlRequest == null) throw new ArgumentNullException(nameof(flurlRequest));
|
||||
|
||||
try
|
||||
{
|
||||
if (data is WechatOpenAIPlatformRequest.Serialization.IEncryptedXmlable)
|
||||
@ -106,8 +107,14 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||
data = new { encrypt = encryptedXml };
|
||||
}
|
||||
|
||||
using IFlurlResponse flurlResponse = await base.SendRequestWithJsonAsync(flurlRequest, data, cancellationToken).ConfigureAwait(false);
|
||||
return await GetResposneAsync<T>(flurlResponse).ConfigureAwait(false);
|
||||
bool isSimpleRequest = data == null ||
|
||||
flurlRequest.Verb == HttpMethod.Get ||
|
||||
flurlRequest.Verb == HttpMethod.Head ||
|
||||
flurlRequest.Verb == HttpMethod.Options;
|
||||
using IFlurlResponse flurlResponse = isSimpleRequest ?
|
||||
await base.SendRequestAsync(flurlRequest, null, cancellationToken).ConfigureAwait(false) :
|
||||
await base.SendRequestWithJsonAsync(flurlRequest, data, cancellationToken).ConfigureAwait(false);
|
||||
return await WrapResponseWithJsonAsync<T>(flurlResponse, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (FlurlHttpException ex)
|
||||
{
|
||||
@ -126,6 +133,8 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||
public async Task<T> SendRequestWithUrlEncodedAsync<T>(IFlurlRequest flurlRequest, object? data = null, CancellationToken cancellationToken = default)
|
||||
where T : WechatOpenAIPlatformResponse, new()
|
||||
{
|
||||
if (flurlRequest == null) throw new ArgumentNullException(nameof(flurlRequest));
|
||||
|
||||
try
|
||||
{
|
||||
if (data is WechatOpenAIPlatformRequest.Serialization.IEncryptedUrlEncoded)
|
||||
@ -139,36 +148,12 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||
.AllowAnyHttpStatus()
|
||||
.SendUrlEncodedAsync(flurlRequest.Verb, data, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
return await GetResposneAsync<T>(flurlResponse).ConfigureAwait(false);
|
||||
return await WrapResponseWithJsonAsync<T>(flurlResponse, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (FlurlHttpException ex)
|
||||
{
|
||||
throw new WechatOpenAIException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<T> GetResposneAsync<T>(IFlurlResponse flurlResponse)
|
||||
where T : WechatOpenAIPlatformResponse, new()
|
||||
{
|
||||
byte[] bytes = await flurlResponse.GetBytesAsync().ConfigureAwait(false);
|
||||
bool jsonable =
|
||||
(bytes.FirstOrDefault() == 91 && bytes.LastOrDefault() == 93) || // "[...]"
|
||||
(bytes.FirstOrDefault() == 123 && bytes.LastOrDefault() == 125); // "{...}"
|
||||
|
||||
T result = jsonable ? JsonSerializer.Deserialize<T>(Encoding.UTF8.GetString(bytes)) : new T();
|
||||
|
||||
result.RawStatus = flurlResponse.StatusCode;
|
||||
result.RawHeaders = new ReadOnlyDictionary<string, string>(
|
||||
flurlResponse.Headers
|
||||
.GroupBy(e => e.Name)
|
||||
.ToDictionary(
|
||||
k => k.Key,
|
||||
v => string.Join(", ", v.Select(e => e.Value))
|
||||
)
|
||||
);
|
||||
result.RawBytes = bytes;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,26 +8,53 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||
/// </summary>
|
||||
public abstract class WechatOpenAIPlatformResponse : ICommonResponse
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
int ICommonResponse.RawStatus { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
IDictionary<string, string> ICommonResponse.RawHeaders { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
byte[] ICommonResponse.RawBytes { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应状态码。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public int RawStatus { get; internal set; }
|
||||
public int RawStatus
|
||||
{
|
||||
get { return ((ICommonResponse)this).RawStatus; }
|
||||
internal set { ((ICommonResponse)this).RawStatus = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应表头集合。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public IDictionary<string, string> RawHeaders { get; internal set; } = default!;
|
||||
public IDictionary<string, string> RawHeaders
|
||||
{
|
||||
get { return ((ICommonResponse)this).RawHeaders; }
|
||||
internal set { ((ICommonResponse)this).RawHeaders = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应正文。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public byte[] RawBytes { get; internal set; } = default!;
|
||||
public byte[] RawBytes
|
||||
{
|
||||
get { return ((ICommonResponse)this).RawBytes; }
|
||||
internal set { ((ICommonResponse)this).RawBytes = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取微信智能对话 API 返回的错误码。
|
||||
|
@ -1,8 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Flurl.Http;
|
||||
@ -91,10 +88,12 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||
public async Task<T> SendRequestAsync<T>(IFlurlRequest flurlRequest, HttpContent? httpContent = null, CancellationToken cancellationToken = default)
|
||||
where T : WechatOpenAIThirdPartyResponse, new()
|
||||
{
|
||||
if (flurlRequest == null) throw new ArgumentNullException(nameof(flurlRequest));
|
||||
|
||||
try
|
||||
{
|
||||
using IFlurlResponse flurlResponse = await base.SendRequestAsync(flurlRequest, httpContent, cancellationToken).ConfigureAwait(false);
|
||||
return await GetResposneAsync<T>(flurlResponse).ConfigureAwait(false);
|
||||
return await WrapResponseWithJsonAsync<T>(flurlResponse, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (FlurlHttpException ex)
|
||||
{
|
||||
@ -113,39 +112,23 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||
public async Task<T> SendRequestWithJsonAsync<T>(IFlurlRequest flurlRequest, object? data = null, CancellationToken cancellationToken = default)
|
||||
where T : WechatOpenAIThirdPartyResponse, new()
|
||||
{
|
||||
if (flurlRequest == null) throw new ArgumentNullException(nameof(flurlRequest));
|
||||
|
||||
try
|
||||
{
|
||||
using IFlurlResponse flurlResponse = await base.SendRequestWithJsonAsync(flurlRequest, data, cancellationToken).ConfigureAwait(false);
|
||||
return await GetResposneAsync<T>(flurlResponse).ConfigureAwait(false);
|
||||
bool isSimpleRequest = data == null ||
|
||||
flurlRequest.Verb == HttpMethod.Get ||
|
||||
flurlRequest.Verb == HttpMethod.Head ||
|
||||
flurlRequest.Verb == HttpMethod.Options;
|
||||
using IFlurlResponse flurlResponse = isSimpleRequest ?
|
||||
await base.SendRequestAsync(flurlRequest, null, cancellationToken).ConfigureAwait(false) :
|
||||
await base.SendRequestWithJsonAsync(flurlRequest, data, cancellationToken).ConfigureAwait(false);
|
||||
return await WrapResponseWithJsonAsync<T>(flurlResponse, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (FlurlHttpException ex)
|
||||
{
|
||||
throw new WechatOpenAIException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<T> GetResposneAsync<T>(IFlurlResponse flurlResponse)
|
||||
where T : WechatOpenAIThirdPartyResponse, new()
|
||||
{
|
||||
byte[] bytes = await flurlResponse.GetBytesAsync().ConfigureAwait(false);
|
||||
bool jsonable =
|
||||
(bytes.FirstOrDefault() == 91 && bytes.LastOrDefault() == 93) || // "[...]"
|
||||
(bytes.FirstOrDefault() == 123 && bytes.LastOrDefault() == 125); // "{...}"
|
||||
|
||||
T result = jsonable ? JsonSerializer.Deserialize<T>(Encoding.UTF8.GetString(bytes)) : new T();
|
||||
|
||||
result.RawStatus = flurlResponse.StatusCode;
|
||||
result.RawHeaders = new ReadOnlyDictionary<string, string>(
|
||||
flurlResponse.Headers
|
||||
.GroupBy(e => e.Name)
|
||||
.ToDictionary(
|
||||
k => k.Key,
|
||||
v => string.Join(", ", v.Select(e => e.Value))
|
||||
)
|
||||
);
|
||||
result.RawBytes = bytes;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,26 +8,53 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||
/// </summary>
|
||||
public abstract class WechatOpenAIThirdPartyResponse : ICommonResponse
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
int ICommonResponse.RawStatus { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
IDictionary<string, string> ICommonResponse.RawHeaders { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
byte[] ICommonResponse.RawBytes { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应状态码。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public int RawStatus { get; internal set; }
|
||||
public int RawStatus
|
||||
{
|
||||
get { return ((ICommonResponse)this).RawStatus; }
|
||||
internal set { ((ICommonResponse)this).RawStatus = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应表头集合。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public IDictionary<string, string> RawHeaders { get; internal set; } = default!;
|
||||
public IDictionary<string, string> RawHeaders
|
||||
{
|
||||
get { return ((ICommonResponse)this).RawHeaders; }
|
||||
internal set { ((ICommonResponse)this).RawHeaders = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应正文。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public byte[] RawBytes { get; internal set; } = default!;
|
||||
public byte[] RawBytes
|
||||
{
|
||||
get { return ((ICommonResponse)this).RawBytes; }
|
||||
internal set { ((ICommonResponse)this).RawBytes = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取微信智能对话 API 返回的请求唯一标识。
|
||||
|
@ -29,7 +29,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.ValueTuple" Version="4.5.0" Condition="'$(TargetFramework)' == 'net461'" />
|
||||
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
|
||||
<PackageReference Include="SKIT.FlurlHttpClient.Common" Version="2.0.0" />
|
||||
<PackageReference Include="SKIT.FlurlHttpClient.Common" Version="2.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -1,9 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Flurl.Http;
|
||||
@ -49,12 +47,12 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
||||
AutoDecryptResponseSensitiveProperty = options.AutoDecryptResponseSensitiveProperty;
|
||||
|
||||
FlurlClient.BaseUrl = options.Endpoints ?? WechatTenpayEndpoints.DEFAULT;
|
||||
FlurlClient.Headers.Remove("Accept");
|
||||
FlurlClient.Headers.Remove("Accept-Language");
|
||||
FlurlClient.Headers.Remove("User-Agent");
|
||||
FlurlClient.WithHeader("Accept", "application/json");
|
||||
FlurlClient.WithHeader("Accept-Language", options.AcceptLanguage);
|
||||
FlurlClient.WithHeader("User-Agent", options.UserAgent);
|
||||
FlurlClient.Headers.Remove(Contants.HttpHeaders.Accept);
|
||||
FlurlClient.Headers.Remove(Contants.HttpHeaders.AcceptLanguage);
|
||||
FlurlClient.Headers.Remove(Contants.HttpHeaders.UserAgent);
|
||||
FlurlClient.WithHeader(Contants.HttpHeaders.Accept, "application/json");
|
||||
FlurlClient.WithHeader(Contants.HttpHeaders.AcceptLanguage, options.AcceptLanguage);
|
||||
FlurlClient.WithHeader(Contants.HttpHeaders.UserAgent, options.UserAgent);
|
||||
FlurlClient.WithTimeout(TimeSpan.FromMilliseconds(options.Timeout));
|
||||
|
||||
Interceptors.Add(new Interceptors.WechatTenpaySignInterceptor(
|
||||
@ -106,6 +104,8 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
||||
public async Task<T> SendRequestAsync<T>(IFlurlRequest flurlRequest, HttpContent? httpContent = null, CancellationToken cancellationToken = default)
|
||||
where T : WechatTenpayResponse, new()
|
||||
{
|
||||
if (flurlRequest == null) throw new ArgumentNullException(nameof(flurlRequest));
|
||||
|
||||
if (httpContent != null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(httpContent.Headers.ContentType?.MediaType))
|
||||
@ -115,7 +115,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
||||
try
|
||||
{
|
||||
using IFlurlResponse flurlResponse = await base.SendRequestAsync(flurlRequest, httpContent, cancellationToken).ConfigureAwait(false);
|
||||
return await GetResposneAsync<T>(flurlResponse).ConfigureAwait(false);
|
||||
return await WrapResponseWithJsonAsync<T>(flurlResponse, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (FlurlHttpException ex)
|
||||
{
|
||||
@ -134,10 +134,18 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
||||
public async Task<T> SendRequestWithJsonAsync<T>(IFlurlRequest flurlRequest, object? data = null, CancellationToken cancellationToken = default)
|
||||
where T : WechatTenpayResponse, new()
|
||||
{
|
||||
if (flurlRequest == null) throw new ArgumentNullException(nameof(flurlRequest));
|
||||
|
||||
try
|
||||
{
|
||||
using IFlurlResponse flurlResponse = await base.SendRequestWithJsonAsync(flurlRequest, data, cancellationToken).ConfigureAwait(false);
|
||||
return await GetResposneAsync<T>(flurlResponse).ConfigureAwait(false);
|
||||
bool isSimpleRequest = data == null ||
|
||||
flurlRequest.Verb == HttpMethod.Get ||
|
||||
flurlRequest.Verb == HttpMethod.Head ||
|
||||
flurlRequest.Verb == HttpMethod.Options;
|
||||
using IFlurlResponse flurlResponse = isSimpleRequest ?
|
||||
await base.SendRequestAsync(flurlRequest, null, cancellationToken).ConfigureAwait(false) :
|
||||
await base.SendRequestWithJsonAsync(flurlRequest, data, cancellationToken).ConfigureAwait(false);
|
||||
return await WrapResponseWithJsonAsync<T>(flurlResponse, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (FlurlHttpException ex)
|
||||
{
|
||||
@ -145,32 +153,16 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<T> GetResposneAsync<T>(IFlurlResponse flurlResponse)
|
||||
where T : WechatTenpayResponse, new()
|
||||
private new async Task<TResponse> WrapResponseWithJsonAsync<TResponse>(IFlurlResponse flurlResponse, CancellationToken cancellationToken = default)
|
||||
where TResponse : WechatTenpayResponse, new()
|
||||
{
|
||||
byte[] bytes = await flurlResponse.GetBytesAsync().ConfigureAwait(false);
|
||||
bool jsonable =
|
||||
(bytes.FirstOrDefault() == 91 && bytes.LastOrDefault() == 93) || // "[...]"
|
||||
(bytes.FirstOrDefault() == 123 && bytes.LastOrDefault() == 125); // "{...}"
|
||||
|
||||
T result = jsonable ? JsonSerializer.Deserialize<T>(Encoding.UTF8.GetString(bytes)) : new T();
|
||||
TResponse result = await base.WrapResponseWithJsonAsync<TResponse>(flurlResponse, cancellationToken);
|
||||
result.WechatpayRequestId = flurlResponse.Headers.GetAll("Request-ID").FirstOrDefault() ?? string.Empty;
|
||||
result.WechatpayNonce = flurlResponse.Headers.GetAll("Wechatpay-Nonce").FirstOrDefault() ?? string.Empty;
|
||||
result.WechatpayTimestamp = flurlResponse.Headers.GetAll("Wechatpay-Timestamp").FirstOrDefault() ?? string.Empty;
|
||||
result.WechatpaySignature = flurlResponse.Headers.GetAll("Wechatpay-Signature").FirstOrDefault() ?? string.Empty;
|
||||
result.WechatpayCertSerialNumber = flurlResponse.Headers.GetAll("Wechatpay-Serial").FirstOrDefault() ?? string.Empty;
|
||||
|
||||
result.RawStatus = flurlResponse.StatusCode;
|
||||
result.RawHeaders = new ReadOnlyDictionary<string, string>(
|
||||
flurlResponse.Headers
|
||||
.GroupBy(e => e.Name)
|
||||
.ToDictionary(
|
||||
k => k.Key,
|
||||
v => string.Join(", ", v.Select(e => e.Value))
|
||||
)
|
||||
);
|
||||
result.RawBytes = bytes;
|
||||
|
||||
if (AutoDecryptResponseSensitiveProperty && result.IsSuccessful())
|
||||
{
|
||||
this.DecryptResponseSensitiveProperty(result);
|
||||
|
@ -8,26 +8,53 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
||||
/// </summary>
|
||||
public abstract class WechatTenpayResponse : ICommonResponse
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
int ICommonResponse.RawStatus { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
IDictionary<string, string> ICommonResponse.RawHeaders { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
byte[] ICommonResponse.RawBytes { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应状态码。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public int RawStatus { get; internal set; }
|
||||
public int RawStatus
|
||||
{
|
||||
get { return ((ICommonResponse)this).RawStatus; }
|
||||
internal set { ((ICommonResponse)this).RawStatus = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应表头集合。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public IDictionary<string, string> RawHeaders { get; internal set; } = default!;
|
||||
public IDictionary<string, string> RawHeaders
|
||||
{
|
||||
get { return ((ICommonResponse)this).RawHeaders; }
|
||||
internal set { ((ICommonResponse)this).RawHeaders = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应正文。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public byte[] RawBytes { get; internal set; } = default!;
|
||||
public byte[] RawBytes
|
||||
{
|
||||
get { return ((ICommonResponse)this).RawBytes; }
|
||||
internal set { ((ICommonResponse)this).RawBytes = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取微信请求唯一标识。
|
||||
|
@ -27,7 +27,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="SKIT.FlurlHttpClient.Common" Version="2.0.0" />
|
||||
<PackageReference Include="SKIT.FlurlHttpClient.Common" Version="2.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -1,8 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Flurl.Http;
|
||||
@ -75,10 +72,12 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
|
||||
public async Task<T> SendRequestAsync<T>(IFlurlRequest flurlRequest, HttpContent? httpContent = null, CancellationToken cancellationToken = default)
|
||||
where T : WechatWorkResponse, new()
|
||||
{
|
||||
if (flurlRequest == null) throw new ArgumentNullException(nameof(flurlRequest));
|
||||
|
||||
try
|
||||
{
|
||||
using IFlurlResponse flurlResponse = await base.SendRequestAsync(flurlRequest, httpContent, cancellationToken).ConfigureAwait(false);
|
||||
return await GetResposneAsync<T>(flurlResponse).ConfigureAwait(false);
|
||||
return await WrapResponseWithJsonAsync<T>(flurlResponse, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (FlurlHttpException ex)
|
||||
{
|
||||
@ -97,39 +96,23 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
|
||||
public async Task<T> SendRequestWithJsonAsync<T>(IFlurlRequest flurlRequest, object? data = null, CancellationToken cancellationToken = default)
|
||||
where T : WechatWorkResponse, new()
|
||||
{
|
||||
if (flurlRequest == null) throw new ArgumentNullException(nameof(flurlRequest));
|
||||
|
||||
try
|
||||
{
|
||||
using IFlurlResponse flurlResponse = await base.SendRequestWithJsonAsync(flurlRequest, data, cancellationToken).ConfigureAwait(false);
|
||||
return await GetResposneAsync<T>(flurlResponse).ConfigureAwait(false);
|
||||
bool isSimpleRequest = data == null ||
|
||||
flurlRequest.Verb == HttpMethod.Get ||
|
||||
flurlRequest.Verb == HttpMethod.Head ||
|
||||
flurlRequest.Verb == HttpMethod.Options;
|
||||
using IFlurlResponse flurlResponse = isSimpleRequest ?
|
||||
await base.SendRequestAsync(flurlRequest, null, cancellationToken).ConfigureAwait(false) :
|
||||
await base.SendRequestWithJsonAsync(flurlRequest, data, cancellationToken).ConfigureAwait(false);
|
||||
return await WrapResponseWithJsonAsync<T>(flurlResponse, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (FlurlHttpException ex)
|
||||
{
|
||||
throw new WechatWorkException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<T> GetResposneAsync<T>(IFlurlResponse flurlResponse)
|
||||
where T : WechatWorkResponse, new()
|
||||
{
|
||||
byte[] bytes = await flurlResponse.GetBytesAsync().ConfigureAwait(false);
|
||||
bool jsonable =
|
||||
(bytes.FirstOrDefault() == 91 && bytes.LastOrDefault() == 93) || // "[...]"
|
||||
(bytes.FirstOrDefault() == 123 && bytes.LastOrDefault() == 125); // "{...}"
|
||||
|
||||
T result = jsonable ? JsonSerializer.Deserialize<T>(Encoding.UTF8.GetString(bytes)) : new T();
|
||||
|
||||
result.RawStatus = flurlResponse.StatusCode;
|
||||
result.RawHeaders = new ReadOnlyDictionary<string, string>(
|
||||
flurlResponse.Headers
|
||||
.GroupBy(e => e.Name)
|
||||
.ToDictionary(
|
||||
k => k.Key,
|
||||
v => string.Join(", ", v.Select(e => e.Value))
|
||||
)
|
||||
);
|
||||
result.RawBytes = bytes;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,26 +8,53 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
|
||||
/// </summary>
|
||||
public abstract class WechatWorkResponse : ICommonResponse
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
int ICommonResponse.RawStatus { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
IDictionary<string, string> ICommonResponse.RawHeaders { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
byte[] ICommonResponse.RawBytes { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应状态码。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public int RawStatus { get; internal set; }
|
||||
public int RawStatus
|
||||
{
|
||||
get { return ((ICommonResponse)this).RawStatus; }
|
||||
internal set { ((ICommonResponse)this).RawStatus = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应表头集合。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public IDictionary<string, string> RawHeaders { get; internal set; } = default!;
|
||||
public IDictionary<string, string> RawHeaders
|
||||
{
|
||||
get { return ((ICommonResponse)this).RawHeaders; }
|
||||
internal set { ((ICommonResponse)this).RawHeaders = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应正文。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public byte[] RawBytes { get; internal set; } = default!;
|
||||
public byte[] RawBytes
|
||||
{
|
||||
get { return ((ICommonResponse)this).RawBytes; }
|
||||
internal set { ((ICommonResponse)this).RawBytes = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取企业微信 API 返回的错误码。
|
||||
|
@ -17,6 +17,9 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests
|
||||
var request = new Models.QueryCertificatesRequest();
|
||||
var response = await TestClients.Instance.ExecuteQueryCertificatesAsync(request);
|
||||
|
||||
Assert.True(response.RawStatus == 200);
|
||||
Assert.True(response.RawHeaders.Count > 0);
|
||||
Assert.True(response.RawBytes.Length > 0);
|
||||
Assert.True(TestClients.Instance.VerifyResponseSignature(response));
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="SKIT.FlurlHttpClient.Common" Version="2.0.0" />
|
||||
<PackageReference Include="SKIT.FlurlHttpClient.Common" Version="2.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
Loading…
Reference in New Issue
Block a user