mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-04-05 08:37:22 +08:00
feat(openai): 导入对话开放平台项目
This commit is contained in:
parent
9aa017fdfe
commit
038d1d1293
@ -129,6 +129,7 @@
|
||||
- _tenpayv3_:关于 `SKIT.FlurlHttpClient.Wechat.TenpayV3` 项目的变化;
|
||||
- _work_:关于 `SKIT.FlurlHttpClient.Wechat.Work` 项目的变化;
|
||||
- _wxads_:关于 `SKIT.FlurlHttpClient.Wechat.Ads` 项目的变化;
|
||||
- _openai_:关于 `SKIT.FlurlHttpClient.Wechat.OpenAI` 项目的变化;
|
||||
- 留空:不属于上述任何情形。
|
||||
|
||||
- **建议**:提交记录应遵循最小化原则,避免修改或新增了几十处、却混杂在一起提交,以免为日后搜索查询造成困扰。
|
||||
|
@ -43,6 +43,7 @@
|
||||
| 商户平台(微信支付)模块 | [](https://www.nuget.org/packages/SKIT.FlurlHttpClient.Wechat.TenpayV3) <br> [](https://www.nuget.org/packages/SKIT.FlurlHttpClient.Wechat.TenpayV3) | [开发文档](./docs/WechatTenpayV3/README.md)|[示例项目](./docs/WechatTenpayV3/Sample.md) |
|
||||
| 企业微信(企业号)模块 | [](https://www.nuget.org/packages/SKIT.FlurlHttpClient.Wechat.Work) <br> [](https://www.nuget.org/packages/SKIT.FlurlHttpClient.Wechat.Work) | [开发文档](./docs/WechatWork/README.md) |
|
||||
| 广告平台(广点通)模块 | [](https://www.nuget.org/packages/SKIT.FlurlHttpClient.Wechat.Ads) <br> [](https://www.nuget.org/packages/SKIT.FlurlHttpClient.Wechat.Ads) | [开发文档](./docs/WechatAds/README.md) |
|
||||
| 对话开放平台(智能对话)模块 | | _开发中_ |
|
||||
|
||||
---
|
||||
|
||||
|
@ -20,6 +20,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SKIT.FlurlHttpClient.Wechat
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SKIT.FlurlHttpClient.Wechat.Ads", "src\SKIT.FlurlHttpClient.Wechat.Ads\SKIT.FlurlHttpClient.Wechat.Ads.csproj", "{7F155EFB-152F-4798-9984-99102B21D2F8}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SKIT.FlurlHttpClient.Wechat.OpenAI", "src\SKIT.FlurlHttpClient.Wechat.OpenAI\SKIT.FlurlHttpClient.Wechat.OpenAI.csproj", "{AAE2E9BC-4D0B-4495-8825-DF8C405DB4A0}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{C95AF531-CF44-44AA-AC90-F4DF9F941674}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{4C48D9D5-1D7F-4616-A05D-256555C310FC}"
|
||||
@ -34,11 +36,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SKIT.FlurlHttpClient.Wechat
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SKIT.FlurlHttpClient.Wechat.Ads.UnitTests", "test\SKIT.FlurlHttpClient.Wechat.Ads.UnitTests\SKIT.FlurlHttpClient.Wechat.Ads.UnitTests.csproj", "{561E0BFB-7817-41FE-BAF2-D78817679AC1}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SKIT.FlurlHttpClient.Wechat.OpenAI.UnitTests", "test\SKIT.FlurlHttpClient.Wechat.OpenAI.UnitTests\SKIT.FlurlHttpClient.Wechat.OpenAI.UnitTests.csproj", "{9689135B-44BA-4D55-8663-7C669BAFE066}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{35C901ED-C234-4A91-9561-AD89B3BB788D}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5", "samples\SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5\SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.csproj", "{D1B321C9-3004-4645-A78D-A85C152062FA}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5", "samples\SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5\SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.csproj", "{65E51735-73CE-4E9B-AA65-4BF5E4C8A705}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5", "samples\SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5\SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.csproj", "{65E51735-73CE-4E9B-AA65-4BF5E4C8A705}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@ -94,6 +98,14 @@ Global
|
||||
{65E51735-73CE-4E9B-AA65-4BF5E4C8A705}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{65E51735-73CE-4E9B-AA65-4BF5E4C8A705}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{65E51735-73CE-4E9B-AA65-4BF5E4C8A705}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{AAE2E9BC-4D0B-4495-8825-DF8C405DB4A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{AAE2E9BC-4D0B-4495-8825-DF8C405DB4A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{AAE2E9BC-4D0B-4495-8825-DF8C405DB4A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{AAE2E9BC-4D0B-4495-8825-DF8C405DB4A0}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{9689135B-44BA-4D55-8663-7C669BAFE066}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{9689135B-44BA-4D55-8663-7C669BAFE066}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{9689135B-44BA-4D55-8663-7C669BAFE066}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{9689135B-44BA-4D55-8663-7C669BAFE066}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
@ -112,6 +124,8 @@ Global
|
||||
{561E0BFB-7817-41FE-BAF2-D78817679AC1} = {C95AF531-CF44-44AA-AC90-F4DF9F941674}
|
||||
{D1B321C9-3004-4645-A78D-A85C152062FA} = {35C901ED-C234-4A91-9561-AD89B3BB788D}
|
||||
{65E51735-73CE-4E9B-AA65-4BF5E4C8A705} = {35C901ED-C234-4A91-9561-AD89B3BB788D}
|
||||
{AAE2E9BC-4D0B-4495-8825-DF8C405DB4A0} = {3E34ADB9-1F52-4C96-9A42-DE782DE1AAA3}
|
||||
{9689135B-44BA-4D55-8663-7C669BAFE066} = {C95AF531-CF44-44AA-AC90-F4DF9F941674}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {F08ED64E-2517-4B51-A4BE-D33D56CC7B39}
|
||||
|
@ -0,0 +1,37 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net461; netstandard2.0; net5.0</TargetFrameworks>
|
||||
<LangVersion>8.0</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<NullableReferenceTypes>true</NullableReferenceTypes>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<PackageId>SKIT.FlurlHttpClient.Wechat.OpenAI</PackageId>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<PackageProjectUrl>https://github.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat</PackageProjectUrl>
|
||||
<PackageTags>Flurl.Http Wechat Weixin MicroMessage WxOpenAI 微信 微信智能对话 微信对话开放平台 微信智能对话开放平台 智能对话平台</PackageTags>
|
||||
<Version>0.1.0-alpha</Version>
|
||||
<Description>基于 Flurl.Http 的微信对话开放平台(微信智能对话) API 客户端。</Description>
|
||||
<Authors>Fu Diwei</Authors>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<RepositoryUrl>https://github.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git</RepositoryUrl>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<IncludeSymbols>true</IncludeSymbols>
|
||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="System.Web" Condition="'$(TargetFramework)' == 'net461'" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SKIT.FlurlHttpClient.Wechat\SKIT.FlurlHttpClient.Wechat.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Settings
|
||||
{
|
||||
public class Credentials
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化客户端时 <see cref="WechatOpenAIClientOptions.ClientId"/> 的副本。
|
||||
/// </summary>
|
||||
public string ClientId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 初始化客户端时 <see cref="WechatOpenAIClientOptions.ClientKey"/> 的副本。
|
||||
/// </summary>
|
||||
public string ClientKey { get; }
|
||||
|
||||
internal Credentials(WechatOpenAIClientOptions options)
|
||||
{
|
||||
if (options == null) throw new ArgumentNullException(nameof(options));
|
||||
|
||||
ClientId = options.ClientId;
|
||||
ClientKey = options.ClientKey;
|
||||
}
|
||||
}
|
||||
}
|
147
src/SKIT.FlurlHttpClient.Wechat.OpenAI/WechatOpenAIClient.cs
Normal file
147
src/SKIT.FlurlHttpClient.Wechat.OpenAI/WechatOpenAIClient.cs
Normal file
@ -0,0 +1,147 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Flurl.Http;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||
{
|
||||
/// <summary>
|
||||
/// 一个微信智能对话 API HTTP 客户端。
|
||||
/// </summary>
|
||||
public class WechatOpenAIClient : CommonClientBase, IWechatClient
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取当前客户端使用的微信公众平台凭证。
|
||||
/// </summary>
|
||||
public Settings.Credentials Credentials { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 用指定的配置项初始化 <see cref="WechatOpenAIClient"/> 类的新实例。
|
||||
/// </summary>
|
||||
/// <param name="options">配置项。</param>
|
||||
public WechatOpenAIClient(WechatOpenAIClientOptions options)
|
||||
: base()
|
||||
{
|
||||
if (options == null) throw new ArgumentNullException(nameof(options));
|
||||
|
||||
Credentials = new Settings.Credentials(options);
|
||||
|
||||
FlurlClient.BaseUrl = options.Endpoints ?? WechatOpenAIEndpoints.DEFAULT;
|
||||
FlurlClient.WithTimeout(TimeSpan.FromMilliseconds(options.Timeout));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用指定的微信智能对话 ClientId 和微信智能对话 ClientKey 初始化 <see cref="WechatOpenAIClient"/> 类的新实例。
|
||||
/// </summary>
|
||||
/// <param name="clientId">微信智能对话 ClientId。</param>
|
||||
/// <param name="clientKey">微信智能对话 ClientKey。</param>
|
||||
public WechatOpenAIClient(string clientId, string clientKey)
|
||||
: this(new WechatOpenAIClientOptions() { ClientId = clientId, ClientKey = clientKey })
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用当前客户端生成一个新的 <see cref="IFlurlRequest"/> 对象。
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="method"></param>
|
||||
/// <param name="urlSegments"></param>
|
||||
/// <returns></returns>
|
||||
public IFlurlRequest CreateRequest(WechatOpenAIRequest request, HttpMethod method, params object[] urlSegments)
|
||||
{
|
||||
IFlurlRequest flurlRequest = FlurlClient.Request(urlSegments).WithVerb(method);
|
||||
|
||||
if (request.Timeout.HasValue)
|
||||
{
|
||||
flurlRequest.WithTimeout(TimeSpan.FromMilliseconds(request.Timeout.Value));
|
||||
}
|
||||
|
||||
if (request.RequestId != null)
|
||||
{
|
||||
flurlRequest.WithHeader("request_id", request.RequestId);
|
||||
}
|
||||
|
||||
if (request.BotId != null)
|
||||
{
|
||||
flurlRequest.WithHeader("wxbot_bid", request.BotId);
|
||||
}
|
||||
|
||||
if (request.AccessToken != null)
|
||||
{
|
||||
flurlRequest.WithHeader("X-OPENAI-TOKEN", request.AccessToken);
|
||||
}
|
||||
|
||||
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)
|
||||
where T : WechatOpenAIResponse, new()
|
||||
{
|
||||
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)
|
||||
where T : WechatOpenAIResponse, new()
|
||||
{
|
||||
try
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<T> GetResposneAsync<T>(IFlurlResponse flurlResponse)
|
||||
where T : WechatOpenAIResponse, new()
|
||||
{
|
||||
string contentType = flurlResponse.Headers.GetAll("Content-Type").FirstOrDefault() ?? string.Empty;
|
||||
bool contentTypeIsNotJson =
|
||||
(flurlResponse.StatusCode != (int)HttpStatusCode.OK) ||
|
||||
(!contentType.StartsWith("application/json") && !contentType.StartsWith("text/json") && !contentType.StartsWith("text/plain"));
|
||||
|
||||
T result = contentTypeIsNotJson ? new T() : await flurlResponse.GetJsonAsync<T>().ConfigureAwait(false);
|
||||
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 = await flurlResponse.ResponseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||
{
|
||||
/// <summary>
|
||||
/// 一个用于构造 <see cref="WechatOpenAIClient"/> 时使用的配置项。
|
||||
/// </summary>
|
||||
public class WechatOpenAIClientOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取或设置请求超时时间(单位:毫秒)。
|
||||
/// <para>默认值:30000</para>
|
||||
/// </summary>
|
||||
public int Timeout { get; set; } = 30 * 1000;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置微微信智能对话 API 域名。
|
||||
/// <para>默认值:<see cref="WechatOpenAIEndpoints.DEFAULT"/></para>
|
||||
/// </summary>
|
||||
public string Endpoints { get; set; } = WechatOpenAIEndpoints.DEFAULT;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置微信智能对话 ClientId。
|
||||
/// </summary>
|
||||
public string ClientId { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置微信智能对话 ClientKey。
|
||||
/// </summary>
|
||||
public string ClientKey { get; set; } = default!;
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>微信智能对话 API 接口域名。</para>
|
||||
/// </summary>
|
||||
public static class WechatOpenAIEndpoints
|
||||
{
|
||||
/// <summary>
|
||||
/// 主域名(默认)。
|
||||
/// </summary>
|
||||
public const string DEFAULT = "https://openaiapi.weixin.qq.com";
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||
{
|
||||
/// <summary>
|
||||
/// 当调用微信智能对话 API 出错时引发的异常。
|
||||
/// </summary>
|
||||
public class WechatOpenAIException : CommonExceptionBase
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public WechatOpenAIException()
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public WechatOpenAIException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public WechatOpenAIException(string message, Exception innerException)
|
||||
: base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
using System;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||
{
|
||||
/// <summary>
|
||||
/// 表示微信智能对话 API 请求的基类。
|
||||
/// </summary>
|
||||
public abstract class WechatOpenAIRequest : IWechatRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取或设置请求超时时间(单位:毫秒)。如果不指定将使用构造 <see cref="WechatOpenAIClient"/> 时的 <see cref="WechatOpenAIClientOptions.Timeout"/> 参数,这在需要指定特定耗时请求(比如上传或下载文件)的超时时间时很有用。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public virtual int? Timeout { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置请求唯一标识。如果不指定将有系统自动生成。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public virtual string? RequestId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置 Bot ID。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public virtual string? BotId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置请求令牌。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public virtual string? AccessToken { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||
{
|
||||
/// <summary>
|
||||
/// 表示微信智能对话 API 响应的基类。
|
||||
/// </summary>
|
||||
public abstract class WechatOpenAIResponse : IWechatResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应状态码。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public int RawStatus { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应表头集合。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public IDictionary<string, string> RawHeaders { get; internal set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应正文。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public byte[] RawBytes { get; internal set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取微信智能对话 API 返回的错误码。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("code")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("code")]
|
||||
public virtual int ErrorCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取微信智能对话 API 返回的错误描述。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("msg")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("msg")]
|
||||
public virtual string? ErrorMessage { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取微信智能对话 API 返回的请求唯一标识。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("request_id")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("request_id")]
|
||||
public virtual string? RequestId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取一个值,该值指示调用微信 API 是否成功(即 HTTP 状态码为 200、且 errcode 值为 0)。
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual bool IsSuccessful()
|
||||
{
|
||||
return RawStatus == 200 && ErrorCode == 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 表示微信智能对话 API 响应的泛型基类。
|
||||
/// </summary>
|
||||
public abstract class WechatOpenAIResponse<TData>
|
||||
where TData : class, new()
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取微信智能对话 API 返回的数据。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("data")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("data")]
|
||||
public virtual TData? Data { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<LangVersion>8.0</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove=".gitignore" />
|
||||
<Content Include="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="ModelSamples/**/*.json" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\SKIT.FlurlHttpClient.Wechat.OpenAI\SKIT.FlurlHttpClient.Wechat.OpenAI.csproj" />
|
||||
<ProjectReference Include="..\SKIT.FlurlHttpClient.Wechat.TestTools\SKIT.FlurlHttpClient.Wechat.TestTools.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.UnitTests
|
||||
{
|
||||
class TestClients
|
||||
{
|
||||
static TestClients()
|
||||
{
|
||||
Instance = new WechatOpenAIClient(new WechatOpenAIClientOptions()
|
||||
{
|
||||
ClientId = TestConfigs.WechatClientId,
|
||||
ClientKey = TestConfigs.WechatClientKey
|
||||
});
|
||||
}
|
||||
|
||||
public static readonly WechatOpenAIClient Instance;
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.UnitTests
|
||||
{
|
||||
class TestConfigs
|
||||
{
|
||||
static TestConfigs()
|
||||
{
|
||||
// NOTICE: 请在项目根目录下按照 appsettings.json 的格式填入测试参数。
|
||||
// WARN: 敏感信息请不要提交到 git!
|
||||
|
||||
using var stream = File.OpenRead("appsettings.json");
|
||||
using var json = JsonDocument.Parse(stream);
|
||||
|
||||
var config = json.RootElement.GetProperty("WechatConfig");
|
||||
WechatClientId = config.GetProperty("ClientId").GetString();
|
||||
WechatClientKey = config.GetProperty("ClientKey").GetString();
|
||||
WechatAccessToken = config.GetProperty("AccessToken").GetString();
|
||||
|
||||
ProjectSourceDirectory = json.RootElement.GetProperty("ProjectSourceDirectory").GetString();
|
||||
ProjectTestDirectory = json.RootElement.GetProperty("ProjectTestDirectory").GetString();
|
||||
}
|
||||
|
||||
public static readonly string WechatClientId;
|
||||
public static readonly string WechatClientKey;
|
||||
public static readonly string WechatAccessToken;
|
||||
|
||||
public static readonly string ProjectSourceDirectory;
|
||||
public static readonly string ProjectTestDirectory;
|
||||
}
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.UnitTests
|
||||
{
|
||||
public class WechatOpenAIDeclarationTests
|
||||
{
|
||||
private static readonly Assembly _assembly = Assembly.Load("SKIT.FlurlHttpClient.Wechat.OpenAI");
|
||||
|
||||
[Fact(DisplayName = "验证 API 模型命名")]
|
||||
public void ApiModelsNamingTest()
|
||||
{
|
||||
CodeStyleUtil.VerifyApiModelsNaming(_assembly, out var ex);
|
||||
|
||||
if (ex != null)
|
||||
throw ex;
|
||||
|
||||
Assert.Null(ex);
|
||||
}
|
||||
|
||||
[Fact(DisplayName = "验证 API 模型定义")]
|
||||
public void ApiModelsDefinitionTest()
|
||||
{
|
||||
string workdir = Path.Combine(TestConfigs.ProjectTestDirectory, "ModelSamples");
|
||||
Assert.True(Directory.Exists(workdir));
|
||||
|
||||
CodeStyleUtil.VerifyApiModelsDefinition(_assembly, workdir, out var ex);
|
||||
|
||||
if (ex != null)
|
||||
throw ex;
|
||||
|
||||
Assert.Null(ex);
|
||||
}
|
||||
|
||||
[Fact(DisplayName = "验证 API 事件定义")]
|
||||
public void ApiEventsDefinitionTest()
|
||||
{
|
||||
string workdir = Path.Combine(TestConfigs.ProjectTestDirectory, "EventSamples");
|
||||
Assert.True(Directory.Exists(workdir));
|
||||
|
||||
CodeStyleUtil.VerifyApiEventsDefinition(_assembly, workdir, out var ex);
|
||||
|
||||
if (ex != null)
|
||||
throw ex;
|
||||
|
||||
Assert.Null(ex);
|
||||
}
|
||||
|
||||
[Fact(DisplayName = "验证 API 接口命名")]
|
||||
public void ApiExtensionsNamingTest()
|
||||
{
|
||||
CodeStyleUtil.VerifyApiExtensionsNaming(_assembly, out var ex);
|
||||
|
||||
if (ex != null)
|
||||
throw ex;
|
||||
|
||||
Assert.Null(ex);
|
||||
}
|
||||
|
||||
[Fact(DisplayName = "验证代码规范")]
|
||||
public void CodeStyleTest()
|
||||
{
|
||||
string workdir = Path.Combine(TestConfigs.ProjectSourceDirectory);
|
||||
Assert.True(Directory.Exists(workdir));
|
||||
|
||||
CodeStyleUtil.VerifySourceCodeStyle(workdir, out var ex);
|
||||
|
||||
if (ex != null)
|
||||
throw ex;
|
||||
|
||||
Assert.Null(ex);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
{
|
||||
"WechatConfig": {
|
||||
"ClientId": "请在此填写用于测试的微信智能对话 ClientId",
|
||||
"ClientKey": "请在此填写用于测试的微信智能对话 ClientKey",
|
||||
"AccessToken": "请在此填写用于测试的微信智能对话 AccessToken"
|
||||
},
|
||||
"ProjectSourceDirectory": "请输入当前 SDK 项目所在的目录完整路径,如 C:\\Project\\src\\SKIT.FlurlHttpClient.Wechat.OpenAI\\",
|
||||
"ProjectTestDirectory": "请输入当前测试项目所在的目录完整路径,如 C:\\Project\\test\\SKIT.FlurlHttpClient.Wechat.OpenAI.UnitTests\\"
|
||||
}
|
Loading…
Reference in New Issue
Block a user