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>
|
2022-11-21 11:30:38 +08:00
|
|
|
public static class WechatOpenAIClientEventExtensions
|
2021-10-09 12:53:21 +08:00
|
|
|
{
|
2022-11-21 11:30:38 +08:00
|
|
|
private static TEvent InnerDeserializeEventFromXml<TEvent>(this WechatOpenAIClient client, string callbackXml)
|
|
|
|
|
where TEvent : WechatOpenAIEvent
|
2021-10-09 12:53:21 +08:00
|
|
|
{
|
|
|
|
|
if (client == null) throw new ArgumentNullException(nameof(client));
|
2023-06-19 19:48:19 +08:00
|
|
|
if (callbackXml == null) throw new ArgumentNullException(callbackXml);
|
2021-10-09 12:53:21 +08:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-11-21 11:30:38 +08:00
|
|
|
if (!Utilities.WxMsgCryptor.TryParseXml(callbackXml, out string? encryptedXml))
|
2023-03-09 18:12:30 +08:00
|
|
|
throw new Exceptions.WechatOpenAIEventSerializationException("Failed to encrypt event data, because of empty encrypted data.");
|
2021-10-09 12:53:21 +08:00
|
|
|
|
2022-11-21 11:30:38 +08:00
|
|
|
callbackXml = Utilities.WxMsgCryptor.AESDecrypt(cipherText: encryptedXml!, encodingAESKey: client.Credentials.EncodingAESKey!, out _);
|
2021-10-19 10:32:30 +08:00
|
|
|
return Utilities.XmlUtility.Deserialize<TEvent>(callbackXml);
|
2021-10-09 12:53:21 +08:00
|
|
|
}
|
|
|
|
|
catch (WechatOpenAIException)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-03-09 18:12:30 +08:00
|
|
|
throw new Exceptions.WechatOpenAIEventSerializationException("Failed to deserialize event data. Please see the inner exception for more details.", ex);
|
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>
|
|
|
|
|
/// <param name="callbackXml"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-11-21 11:30:38 +08:00
|
|
|
public static TEvent DeserializeEventFromXml<TEvent>(this WechatOpenAIClient client, string callbackXml)
|
|
|
|
|
where TEvent : WechatOpenAIEvent, WechatOpenAIEvent.Serialization.IXmlSerializable, new()
|
2021-10-09 12:53:21 +08:00
|
|
|
{
|
|
|
|
|
return InnerDeserializeEventFromXml<TEvent>(client, callbackXml);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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>
|
|
|
|
|
/// <param name="callbackXml"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-11-21 11:30:38 +08:00
|
|
|
public static WechatOpenAIEvent DeserializeEventFromXml(this WechatOpenAIClient client, string callbackXml)
|
2021-10-09 12:53:21 +08:00
|
|
|
{
|
2022-11-21 11:30:38 +08:00
|
|
|
return InnerDeserializeEventFromXml<WechatOpenAIEvent>(client, callbackXml);
|
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>
|
|
|
|
|
/// <param name="callbackModel"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-11-21 11:30:38 +08:00
|
|
|
public static string SerializeEventToXml<TEvent>(this WechatOpenAIClient client, TEvent callbackModel)
|
|
|
|
|
where TEvent : WechatOpenAIEvent, WechatOpenAIEvent.Serialization.IXmlSerializable, new()
|
2021-10-09 12:53:21 +08:00
|
|
|
{
|
|
|
|
|
string xml;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-10-09 15:17:35 +08:00
|
|
|
xml = Utilities.XmlUtility.Serialize(callbackModel);
|
2021-10-09 12:53:21 +08:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-03-09 18:12:30 +08:00
|
|
|
throw new Exceptions.WechatOpenAIEventSerializationException("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))
|
2023-03-09 18:12:30 +08:00
|
|
|
throw new Exceptions.WechatOpenAIEventSerializationException("Failed to encrypt event data, because there is no encoding AES key.");
|
2021-10-11 18:34:21 +08:00
|
|
|
if (string.IsNullOrEmpty(client.Credentials.Token))
|
2023-03-09 18:12:30 +08:00
|
|
|
throw new Exceptions.WechatOpenAIEventSerializationException("Failed to encrypt event data, because there is no token.");
|
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)
|
|
|
|
|
{
|
2023-03-09 18:12:30 +08:00
|
|
|
throw new Exceptions.WechatOpenAIEventSerializationException("Failed to encrypt event data. Please see the inner exception for more details.", ex);
|
2021-10-09 12:53:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return xml;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|