2021-10-07 21:19:24 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Net.Http;
|
2021-12-09 00:28:40 +08:00
|
|
|
|
using System.Text;
|
2021-10-07 21:19:24 +08:00
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Flurl.Http;
|
|
|
|
|
|
|
|
|
|
|
|
namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2021-10-11 22:53:34 +08:00
|
|
|
|
/// 一个微信智能对话平台接入 API HTTP 客户端。
|
2021-10-07 21:19:24 +08:00
|
|
|
|
/// </summary>
|
2021-11-09 15:23:23 +08:00
|
|
|
|
public class WechatOpenAIPlatformClient : CommonClientBase, ICommonClient
|
2021-10-07 21:19:24 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2021-10-11 22:53:34 +08:00
|
|
|
|
/// 获取当前客户端使用的微信智能对话平台凭证。
|
2021-10-07 21:19:24 +08:00
|
|
|
|
/// </summary>
|
2021-10-11 22:53:34 +08:00
|
|
|
|
public Settings.PlatformCredentials Credentials { get; }
|
2021-10-07 21:19:24 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-10-11 22:53:34 +08:00
|
|
|
|
/// 用指定的配置项初始化 <see cref="WechatOpenAIPlatformClient"/> 类的新实例。
|
2021-10-07 21:19:24 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="options">配置项。</param>
|
2021-10-11 22:53:34 +08:00
|
|
|
|
public WechatOpenAIPlatformClient(WechatOpenAIPlatformClientOptions options)
|
2021-10-07 21:19:24 +08:00
|
|
|
|
: base()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (options == null) throw new ArgumentNullException(nameof(options));
|
|
|
|
|
|
|
2021-10-11 22:53:34 +08:00
|
|
|
|
Credentials = new Settings.PlatformCredentials(options);
|
2021-10-07 21:19:24 +08:00
|
|
|
|
|
2021-10-11 22:53:34 +08:00
|
|
|
|
FlurlClient.BaseUrl = options.Endpoints ?? WechatOpenAIPlatformEndpoints.DEFAULT;
|
2021-10-07 21:19:24 +08:00
|
|
|
|
FlurlClient.WithTimeout(TimeSpan.FromMilliseconds(options.Timeout));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-10-11 22:53:34 +08:00
|
|
|
|
/// 用指定的微信智能对话 AppId、Token、EncodingAESKey 初始化 <see cref="WechatOpenAIThirdPartyClient"/> 类的新实例。
|
2021-10-07 21:19:24 +08:00
|
|
|
|
/// </summary>
|
2021-10-11 22:53:34 +08:00
|
|
|
|
/// <param name="appId">微信智能对话 AppId。</param>
|
|
|
|
|
|
/// <param name="token">微信智能对话 Token。</param>
|
|
|
|
|
|
/// <param name="encodingAESKey">微信智能对话 EncodingAESKey。</param>
|
|
|
|
|
|
public WechatOpenAIPlatformClient(string appId, string token, string encodingAESKey)
|
|
|
|
|
|
: this(new WechatOpenAIPlatformClientOptions() { AppId = appId, Token = token, EncodingAESKey = encodingAESKey })
|
2021-10-07 21:19:24 +08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 使用当前客户端生成一个新的 <see cref="IFlurlRequest"/> 对象。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
|
/// <param name="method"></param>
|
|
|
|
|
|
/// <param name="urlSegments"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2021-10-11 22:53:34 +08:00
|
|
|
|
public IFlurlRequest CreateRequest(WechatOpenAIPlatformRequest request, HttpMethod method, params object[] urlSegments)
|
2021-10-07 21:19:24 +08:00
|
|
|
|
{
|
|
|
|
|
|
IFlurlRequest flurlRequest = FlurlClient.Request(urlSegments).WithVerb(method);
|
|
|
|
|
|
|
2021-10-18 19:40:11 +08:00
|
|
|
|
if (request.Timeout != null)
|
2021-10-07 21:19:24 +08:00
|
|
|
|
{
|
|
|
|
|
|
flurlRequest.WithTimeout(TimeSpan.FromMilliseconds(request.Timeout.Value));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return flurlRequest;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步发起请求。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="flurlRequest"></param>
|
|
|
|
|
|
/// <param name="httpContent"></param>
|
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<T> SendRequestAsync<T>(IFlurlRequest flurlRequest, HttpContent? httpContent = null, CancellationToken cancellationToken = default)
|
2021-10-11 22:53:34 +08:00
|
|
|
|
where T : WechatOpenAIPlatformResponse, new()
|
2021-10-07 21:19:24 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
using IFlurlResponse flurlResponse = await base.SendRequestAsync(flurlRequest, httpContent, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
return await GetResposneAsync<T>(flurlResponse).ConfigureAwait(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (FlurlHttpException ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new WechatOpenAIException(ex.Message, ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步发起请求。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="flurlRequest"></param>
|
|
|
|
|
|
/// <param name="data"></param>
|
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<T> SendRequestWithJsonAsync<T>(IFlurlRequest flurlRequest, object? data = null, CancellationToken cancellationToken = default)
|
2021-10-11 22:53:34 +08:00
|
|
|
|
where T : WechatOpenAIPlatformResponse, new()
|
2021-10-07 21:19:24 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2021-10-11 22:53:34 +08:00
|
|
|
|
if (data is WechatOpenAIPlatformRequest.Serialization.IEncryptedXmlable)
|
2021-10-09 15:17:35 +08:00
|
|
|
|
{
|
2021-10-09 16:57:16 +08:00
|
|
|
|
string plainXml = Utilities.XmlUtility.Serialize(data);
|
2021-10-11 18:34:21 +08:00
|
|
|
|
string encryptedXml = Utilities.WxBizMsgCryptor.AESEncrypt(plainText: plainXml, encodingAESKey: Credentials.EncodingAESKey!, appId: Credentials.AppId!);
|
2021-10-09 16:57:16 +08:00
|
|
|
|
data = new { encrypt = encryptedXml };
|
2021-10-09 15:17:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-07 21:19:24 +08:00
|
|
|
|
using IFlurlResponse flurlResponse = await base.SendRequestWithJsonAsync(flurlRequest, data, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
return await GetResposneAsync<T>(flurlResponse).ConfigureAwait(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (FlurlHttpException ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new WechatOpenAIException(ex.Message, ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-11 18:34:21 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步发起请求。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="flurlRequest"></param>
|
|
|
|
|
|
/// <param name="data"></param>
|
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<T> SendRequestWithUrlEncodedAsync<T>(IFlurlRequest flurlRequest, object? data = null, CancellationToken cancellationToken = default)
|
2021-10-11 22:53:34 +08:00
|
|
|
|
where T : WechatOpenAIPlatformResponse, new()
|
2021-10-11 18:34:21 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2021-10-11 22:53:34 +08:00
|
|
|
|
if (data is WechatOpenAIPlatformRequest.Serialization.IEncryptedUrlEncoded)
|
2021-10-11 18:34:21 +08:00
|
|
|
|
{
|
|
|
|
|
|
string jwt = Utilities.JWTUtility.EncodeWithHS256(payload: data, secret: Credentials.EncodingAESKey!);
|
|
|
|
|
|
data = new { query = jwt };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
using IFlurlResponse flurlResponse = await flurlRequest
|
|
|
|
|
|
.WithClient(FlurlClient)
|
|
|
|
|
|
.AllowAnyHttpStatus()
|
|
|
|
|
|
.SendUrlEncodedAsync(flurlRequest.Verb, data, cancellationToken)
|
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
return await GetResposneAsync<T>(flurlResponse).ConfigureAwait(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (FlurlHttpException ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new WechatOpenAIException(ex.Message, ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-07 21:19:24 +08:00
|
|
|
|
private async Task<T> GetResposneAsync<T>(IFlurlResponse flurlResponse)
|
2021-10-11 22:53:34 +08:00
|
|
|
|
where T : WechatOpenAIPlatformResponse, new()
|
2021-10-07 21:19:24 +08:00
|
|
|
|
{
|
2021-12-09 00:28:40 +08:00
|
|
|
|
byte[] bytes = await flurlResponse.GetBytesAsync().ConfigureAwait(false);
|
2021-12-15 15:39:43 +08:00
|
|
|
|
bool jsonable =
|
|
|
|
|
|
(bytes.FirstOrDefault() == 91 && bytes.LastOrDefault() == 93) || // "[...]"
|
|
|
|
|
|
(bytes.FirstOrDefault() == 123 && bytes.LastOrDefault() == 125); // "{...}"
|
2021-12-09 00:28:40 +08:00
|
|
|
|
|
|
|
|
|
|
T result = jsonable ? JsonSerializer.Deserialize<T>(Encoding.UTF8.GetString(bytes)) : new T();
|
2021-10-07 21:19:24 +08:00
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
|
)
|
|
|
|
|
|
);
|
2021-12-09 00:28:40 +08:00
|
|
|
|
result.RawBytes = bytes;
|
|
|
|
|
|
|
2021-10-07 21:19:24 +08:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|