mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2026-02-11 10:16:20 +08:00
feat(openai): 新增文件上传相关接口
This commit is contained in:
@@ -0,0 +1,47 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Web;
|
||||||
|
using Flurl;
|
||||||
|
using Flurl.Http;
|
||||||
|
|
||||||
|
namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||||
|
{
|
||||||
|
public static class WechatOpenAIClientExecuteFileExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// <para>异步调用 [POST] /v1/file/upload 接口。</para>
|
||||||
|
/// <para>REF: https://developers.weixin.qq.com/doc/aispeech/openapi/api/v1/file_upload.html </para>
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="client"></param>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static async Task<Models.FileUploadResponse> ExecuteFileUploadAsync(this WechatOpenAIClient client, Models.FileUploadRequest request, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
if (client is null) throw new ArgumentNullException(nameof(client));
|
||||||
|
if (request is null) throw new ArgumentNullException(nameof(request));
|
||||||
|
|
||||||
|
if (request.FileName == null)
|
||||||
|
request.FileName = Guid.NewGuid().ToString("N").ToLower() + ".csv";
|
||||||
|
|
||||||
|
if (request.FileContentType == null)
|
||||||
|
request.FileContentType = "text/csv";
|
||||||
|
|
||||||
|
IFlurlRequest flurlReq = client
|
||||||
|
.CreateRequest(request, HttpMethod.Post, "v1", "file", "upload");
|
||||||
|
|
||||||
|
using var fileContent = new ByteArrayContent(request.FileBytes ?? new byte[0]);
|
||||||
|
using var httpContent = new MultipartFormDataContent();
|
||||||
|
httpContent.Add(fileContent, "\"file\"", $"\"{HttpUtility.UrlEncode(request.FileName)}\"");
|
||||||
|
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(request.FileContentType);
|
||||||
|
fileContent.Headers.ContentLength = request.FileBytes?.Length;
|
||||||
|
|
||||||
|
return await client.SendRequestAsync<Models.FileUploadResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Models
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// <para>表示 [POST] /v1/file/upload 接口的请求。</para>
|
||||||
|
/// </summary>
|
||||||
|
public class FileUploadRequest : WechatOpenAIRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取或设置文件字节数组。
|
||||||
|
/// </summary>
|
||||||
|
[Newtonsoft.Json.JsonIgnore]
|
||||||
|
[System.Text.Json.Serialization.JsonIgnore]
|
||||||
|
public byte[] FileBytes { get; set; } = new byte[0];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取或设置文件名。如果不指定将由系统自动生成。
|
||||||
|
/// </summary>
|
||||||
|
[Newtonsoft.Json.JsonIgnore]
|
||||||
|
[System.Text.Json.Serialization.JsonIgnore]
|
||||||
|
public string? FileName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取或设置文件 Conent-Type。如果不指定将由系统自动生成。
|
||||||
|
/// </summary>
|
||||||
|
[Newtonsoft.Json.JsonIgnore]
|
||||||
|
[System.Text.Json.Serialization.JsonIgnore]
|
||||||
|
public string? FileContentType { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Models
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// <para>表示 [POST] /v1/file/upload 接口的响应。</para>
|
||||||
|
/// </summary>
|
||||||
|
public class FileUploadResponse : WechatOpenAIResponse<FileUploadResponse.Types.Data>
|
||||||
|
{
|
||||||
|
public static class Types
|
||||||
|
{
|
||||||
|
public class Data
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取或设置文件唯一标识。
|
||||||
|
/// </summary>
|
||||||
|
[Newtonsoft.Json.JsonProperty("file_key")]
|
||||||
|
[System.Text.Json.Serialization.JsonPropertyName("file_key")]
|
||||||
|
public string FileKey { get; set; } = default!;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
{}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"code": 0,
|
||||||
|
"msg": "请求成功",
|
||||||
|
"request_id": "255i0ug8-l9q4-3801-44ft-w7csjn9e5142",
|
||||||
|
"data": {
|
||||||
|
"file_key": "fe48940d7310e79d6bf0a4aced56d522"
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user