feat(openai): 移除 JWT 工具类

This commit is contained in:
Fu Diwei
2022-11-21 13:51:33 +08:00
parent e356449b67
commit 07429761cb
5 changed files with 0 additions and 95 deletions

View File

@@ -40,7 +40,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="JWT" Version="9.0.3" />
<PackageReference Include="SKIT.FlurlHttpClient.Common" Version="2.5.0" />
</ItemGroup>

View File

@@ -1,27 +0,0 @@
using System;
using JWT;
using JWT.Algorithms;
using JWT.Serializers;
using Newtonsoft.Json;
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Utilities
{
internal static class JwtUtility
{
private static readonly Lazy<IJwtEncoder> _encoder = new Lazy<IJwtEncoder>(() =>
{
#pragma warning disable CS0618
IJwtAlgorithm algorithm = new HMACSHA256Algorithm();
#pragma warning restore CS0618
IJsonSerializer serializer = new JsonNetSerializer(new JsonSerializer() { NullValueHandling = NullValueHandling.Ignore });
IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder);
return encoder;
}, isThreadSafe: true);
public static string EncodeWithHS256(object payload, string secret)
{
return _encoder.Value.Encode(payload, secret);
}
}
}

View File

@@ -125,42 +125,5 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
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> SendRequestWithUrlEncodedAsync<T>(IFlurlRequest flurlRequest, object? data = null, CancellationToken cancellationToken = default)
where T : WechatOpenAIResponse, new()
{
if (flurlRequest == null) throw new ArgumentNullException(nameof(flurlRequest));
try
{
if (data is WechatOpenAIRequest.Serialization.IEncryptedUrlEncoded)
{
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);
return await WrapResponseWithJsonAsync<T>(flurlResponse, cancellationToken);
}
catch (FlurlHttpTimeoutException ex)
{
throw new Exceptions.WechatOpenAIRequestTimeoutException(ex.Message, ex);
}
catch (FlurlHttpException ex)
{
throw new WechatOpenAIException(ex.Message, ex);
}
}
}
}

View File

@@ -10,10 +10,6 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
public interface IEncryptedXmlable
{
}
public interface IEncryptedUrlEncoded
{
}
}
/// <summary>