feat(wxapi): 基于 ErroredResult 改造验签相关扩展方法

This commit is contained in:
Fu Diwei
2024-02-05 13:25:49 +08:00
committed by RHQYZ
parent 70611189cc
commit abf0a8f596
2 changed files with 78 additions and 39 deletions

View File

@@ -5,32 +5,8 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
/// <summary>
/// 为 <see cref="WechatOpenAIClient"/> 提供回调通知事件的扩展方法。
/// </summary>
public static class WechatOpenAIClientEventExtensions
public static partial class WechatOpenAIClientEventExtensions
{
private static TEvent InnerDeserializeEventFromXml<TEvent>(this WechatOpenAIClient client, string webhookXml)
where TEvent : WechatOpenAIEvent
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (webhookXml is null) throw new ArgumentNullException(webhookXml);
try
{
if (!Utilities.WxMsgCryptor.TryParseXml(webhookXml, out string? encryptedXml))
throw new WechatOpenAIException("Failed to decrypt event data, because of the encrypted data is empty.");
webhookXml = Utilities.WxMsgCryptor.AESDecrypt(cipherText: encryptedXml!, encodingAESKey: client.Credentials.EncodingAESKey!, out _);
return Utilities.XmlHelper.Deserialize<TEvent>(webhookXml);
}
catch (WechatOpenAIException)
{
throw;
}
catch (Exception ex)
{
throw new WechatOpenAIException("Failed to deserialize event data. Please see the inner exception for more details.", ex);
}
}
/// <summary>
/// <para>从 XML 反序列化得到 <see cref="WechatOpenAIEvent"/> 对象。</para>
/// </summary>
@@ -99,4 +75,31 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
return xml;
}
}
partial class WechatOpenAIClientEventExtensions
{
private static TEvent InnerDeserializeEventFromXml<TEvent>(this WechatOpenAIClient client, string webhookXml)
where TEvent : WechatOpenAIEvent
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (webhookXml is null) throw new ArgumentNullException(webhookXml);
try
{
if (!Utilities.WxMsgCryptor.TryParseXml(webhookXml, out string? encryptedXml))
throw new WechatOpenAIException("Failed to decrypt event data, because of the encrypted data is empty.");
webhookXml = Utilities.WxMsgCryptor.AESDecrypt(cipherText: encryptedXml!, encodingAESKey: client.Credentials.EncodingAESKey!, out _);
return Utilities.XmlHelper.Deserialize<TEvent>(webhookXml);
}
catch (WechatOpenAIException)
{
throw;
}
catch (Exception ex)
{
throw new WechatOpenAIException("Failed to deserialize event data. Please see the inner exception for more details.", ex);
}
}
}
}