2022-05-06 20:29:27 +08:00
|
|
|
using System;
|
2021-10-09 12:53:21 +08:00
|
|
|
|
|
|
|
|
namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2022-11-21 11:30:38 +08:00
|
|
|
/// 为 <see cref="WechatOpenAIClient"/> 提供回调通知事件的扩展方法。
|
2021-10-09 12:53:21 +08:00
|
|
|
/// </summary>
|
2024-02-05 13:25:49 +08:00
|
|
|
public static partial class WechatOpenAIClientEventExtensions
|
2021-10-09 12:53:21 +08:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
2022-11-21 11:30:38 +08:00
|
|
|
/// <para>从 XML 反序列化得到 <see cref="WechatOpenAIEvent"/> 对象。</para>
|
2021-10-09 12:53:21 +08:00
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TEvent"></typeparam>
|
|
|
|
|
/// <param name="client"></param>
|
2024-01-29 23:12:04 +08:00
|
|
|
/// <param name="webhookXml"></param>
|
2021-10-09 12:53:21 +08:00
|
|
|
/// <returns></returns>
|
2024-01-29 23:12:04 +08:00
|
|
|
public static TEvent DeserializeEventFromXml<TEvent>(this WechatOpenAIClient client, string webhookXml)
|
|
|
|
|
where TEvent : WechatOpenAIEvent, new()
|
2021-10-09 12:53:21 +08:00
|
|
|
{
|
2024-01-29 23:12:04 +08:00
|
|
|
return InnerDeserializeEventFromXml<TEvent>(client, webhookXml);
|
2021-10-09 12:53:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-11-21 11:30:38 +08:00
|
|
|
/// <para>从 XML 反序列化得到 <see cref="WechatOpenAIEvent"/> 对象。</para>
|
2021-10-09 12:53:21 +08:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
2024-01-29 23:12:04 +08:00
|
|
|
/// <param name="webhookXml"></param>
|
2021-10-09 12:53:21 +08:00
|
|
|
/// <returns></returns>
|
2024-01-29 23:12:04 +08:00
|
|
|
public static WechatOpenAIEvent DeserializeEventFromXml(this WechatOpenAIClient client, string webhookXml)
|
2021-10-09 12:53:21 +08:00
|
|
|
{
|
2024-01-29 23:12:04 +08:00
|
|
|
return InnerDeserializeEventFromXml<WechatOpenAIEvent>(client, webhookXml);
|
2021-10-09 12:53:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-11-21 11:30:38 +08:00
|
|
|
/// 将 <see cref="WechatOpenAIEvent"/> 对象序列化成 XML。
|
2021-10-09 12:53:21 +08:00
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TEvent"></typeparam>
|
|
|
|
|
/// <param name="client"></param>
|
2024-01-29 23:12:04 +08:00
|
|
|
/// <param name="webhookEvent"></param>
|
2021-10-09 12:53:21 +08:00
|
|
|
/// <returns></returns>
|
2024-01-29 23:12:04 +08:00
|
|
|
public static string SerializeEventToXml<TEvent>(this WechatOpenAIClient client, TEvent webhookEvent)
|
|
|
|
|
where TEvent : WechatOpenAIEvent, new()
|
2021-10-09 12:53:21 +08:00
|
|
|
{
|
|
|
|
|
string xml;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-01-29 23:12:04 +08:00
|
|
|
xml = Utilities.XmlHelper.Serialize(webhookEvent);
|
2021-10-09 12:53:21 +08:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-01-29 23:12:04 +08:00
|
|
|
throw new WechatOpenAIException("Failed to serialize event data. Please see the inner exception for more details.", ex);
|
2021-10-09 12:53:21 +08:00
|
|
|
}
|
|
|
|
|
|
2021-10-11 18:34:21 +08:00
|
|
|
if (string.IsNullOrEmpty(client.Credentials.EncodingAESKey))
|
2024-01-29 23:12:04 +08:00
|
|
|
throw new WechatOpenAIException("Failed to encrypt event data, because the push encoding AES key is not set.");
|
2021-10-11 18:34:21 +08:00
|
|
|
if (string.IsNullOrEmpty(client.Credentials.Token))
|
2024-01-29 23:12:04 +08:00
|
|
|
throw new WechatOpenAIException("Failed to encrypt event data, because the push token is not set.");
|
2021-10-09 12:53:21 +08:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-11-21 11:30:38 +08:00
|
|
|
string cipher = Utilities.WxMsgCryptor.AESEncrypt(
|
2021-10-09 12:53:21 +08:00
|
|
|
plainText: xml,
|
2021-10-11 18:34:21 +08:00
|
|
|
encodingAESKey: client.Credentials.EncodingAESKey!,
|
2021-10-09 12:53:21 +08:00
|
|
|
appId: client.Credentials.AppId!
|
|
|
|
|
);
|
|
|
|
|
|
2022-11-21 11:30:38 +08:00
|
|
|
xml = Utilities.WxMsgCryptor.WrapXml(sToken: client.Credentials.Token!, sMsgEncrypt: cipher);
|
2021-10-09 12:53:21 +08:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-01-29 23:12:04 +08:00
|
|
|
throw new WechatOpenAIException("Failed to encrypt event data. Please see the inner exception for more details.", ex);
|
2021-10-09 12:53:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return xml;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-05 13:25:49 +08:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-09 12:53:21 +08:00
|
|
|
}
|