mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-09-21 02:58:06 +08:00
feat(openai): 新增注册用户相关接口
This commit is contained in:
@@ -0,0 +1,35 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Flurl;
|
||||||
|
using Flurl.Http;
|
||||||
|
|
||||||
|
namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||||
|
{
|
||||||
|
public static class WechatOpenAIClientExecuteUserExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// <para>异步调用 [POST] /v1/user/register 接口。</para>
|
||||||
|
/// <para>REF: https://developers.weixin.qq.com/doc/aispeech/openapi/api/v1/user_register.html </para>
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="client"></param>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static async Task<Models.UserRegisterResponse> ExecuteUserRegisterAsync(this WechatOpenAIClient client, Models.UserRegisterRequest request, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
if (client is null) throw new ArgumentNullException(nameof(client));
|
||||||
|
if (request is null) throw new ArgumentNullException(nameof(request));
|
||||||
|
|
||||||
|
IFlurlRequest flurlReq = client
|
||||||
|
.CreateRequest(request, HttpMethod.Post, "v1", "user", "register")
|
||||||
|
.WithHeader("Client-Id", client.Credentials.ClientId)
|
||||||
|
.WithHeader("Client-Key", client.Credentials.ClientKey);
|
||||||
|
|
||||||
|
return await client.SendRequestWithJsonAsync<Models.UserRegisterResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,18 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Models
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// <para>表示 [POST] /v1/user/register 接口的请求。</para>
|
||||||
|
/// </summary>
|
||||||
|
public class UserRegisterRequest : WechatOpenAIRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取或设置平台自定义用户名。
|
||||||
|
/// </summary>
|
||||||
|
[Newtonsoft.Json.JsonProperty("uid")]
|
||||||
|
[System.Text.Json.Serialization.JsonPropertyName("uid")]
|
||||||
|
public string UserId { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,24 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Models
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// <para>表示 [POST] /v1/user/register 接口的响应。</para>
|
||||||
|
/// </summary>
|
||||||
|
public class UserRegisterResponse : WechatOpenAIResponse<UserRegisterResponse.Types.Data>
|
||||||
|
{
|
||||||
|
public static class Types
|
||||||
|
{
|
||||||
|
public class Data
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取或设置用户唯一标识。
|
||||||
|
/// </summary>
|
||||||
|
[Newtonsoft.Json.JsonProperty("openid")]
|
||||||
|
[System.Text.Json.Serialization.JsonPropertyName("openid")]
|
||||||
|
public string OpenId { get; set; } = default!;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -60,10 +60,9 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
|||||||
flurlRequest.WithTimeout(TimeSpan.FromMilliseconds(request.Timeout.Value));
|
flurlRequest.WithTimeout(TimeSpan.FromMilliseconds(request.Timeout.Value));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.RequestId != null)
|
if (request.RequestId == null)
|
||||||
{
|
request.RequestId = Guid.NewGuid().ToString("N");
|
||||||
flurlRequest.WithHeader("request_id", request.RequestId);
|
flurlRequest.WithHeader("request_id", request.RequestId);
|
||||||
}
|
|
||||||
|
|
||||||
if (request.BotId != null)
|
if (request.BotId != null)
|
||||||
{
|
{
|
||||||
@@ -128,7 +127,7 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
|||||||
string contentType = flurlResponse.Headers.GetAll("Content-Type").FirstOrDefault() ?? string.Empty;
|
string contentType = flurlResponse.Headers.GetAll("Content-Type").FirstOrDefault() ?? string.Empty;
|
||||||
bool contentTypeIsNotJson =
|
bool contentTypeIsNotJson =
|
||||||
(flurlResponse.StatusCode != (int)HttpStatusCode.OK) ||
|
(flurlResponse.StatusCode != (int)HttpStatusCode.OK) ||
|
||||||
(!contentType.StartsWith("application/json") && !contentType.StartsWith("text/json") && !contentType.StartsWith("text/plain"));
|
(!contentType.StartsWith("application/json") && !contentType.StartsWith("text/json"));
|
||||||
|
|
||||||
T result = contentTypeIsNotJson ? new T() : await flurlResponse.GetJsonAsync<T>().ConfigureAwait(false);
|
T result = contentTypeIsNotJson ? new T() : await flurlResponse.GetJsonAsync<T>().ConfigureAwait(false);
|
||||||
result.RawStatus = flurlResponse.StatusCode;
|
result.RawStatus = flurlResponse.StatusCode;
|
||||||
|
@@ -63,7 +63,7 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 表示微信智能对话 API 响应的泛型基类。
|
/// 表示微信智能对话 API 响应的泛型基类。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class WechatOpenAIResponse<TData>
|
public abstract class WechatOpenAIResponse<TData> : WechatOpenAIResponse
|
||||||
where TData : class, new()
|
where TData : class, new()
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
1
test/SKIT.FlurlHttpClient.Wechat.OpenAI.UnitTests/.gitignore
vendored
Normal file
1
test/SKIT.FlurlHttpClient.Wechat.OpenAI.UnitTests/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
appsettings.local.json
|
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"uid": "robot_2"
|
||||||
|
}
|
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"code": 0,
|
||||||
|
"data": {
|
||||||
|
"openid": "e36926e48e9ddfe1cc1f3cb6c8813a38966c4b8d"
|
||||||
|
},
|
||||||
|
"msg": "success",
|
||||||
|
"request_id": "363a6ce7-2908-4da0-a3a0-35539648eab6"
|
||||||
|
}
|
@@ -7,6 +7,10 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Remove=".gitignore" />
|
<None Remove=".gitignore" />
|
||||||
|
<None Remove="appsettings.local.json" />
|
||||||
|
<Content Include="appsettings.local.json">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
<Content Include="appsettings.json">
|
<Content Include="appsettings.json">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</Content>
|
</Content>
|
||||||
|
Reference in New Issue
Block a user