Skip to content

Commit

Permalink
更新依赖
Browse files Browse the repository at this point in the history
Signed-off-by: Vicente.Yu <^@^>
  • Loading branch information
Vicente.Yu committed Jun 23, 2024
1 parent 081bb2b commit 048cd04
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 34 deletions.
18 changes: 0 additions & 18 deletions src/Attributes/EnableLoggingFilterAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using FeishuNetSdk;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace WebApiClientCore.Attributes
Expand Down Expand Up @@ -39,22 +38,5 @@ protected override Task WriteLogAsync(ApiResponseContext context, LogMessage log

return base.WriteLogAsync(context, logMessage);
}

/// <summary>
/// 写日志到ILogger
/// </summary>
/// <param name="logger">日志</param>
/// <param name="logMessage">日志消息</param>
protected override void WriteLog(ILogger logger, LogMessage logMessage)
{
if (logMessage.Exception == null)
{
logger.LogInformation("\n{info}", logMessage.ToString());
}
else
{
logger.LogError("\n{error}", logMessage.ToString());
}
}
}
}
29 changes: 15 additions & 14 deletions src/Attributes/IgnoreStatusExceptionFilterAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using WebApiClientCore.Attributes;
using WebApiClientCore.Exceptions;
using WebApiClientCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System.Text.Json;
using WebApiClientCore;
using WebApiClientCore.Attributes;
using WebApiClientCore.Exceptions;

namespace FeishuNetSdk.Attributes;
/// <summary>
Expand All @@ -24,21 +24,22 @@ public override System.Threading.Tasks.Task OnRequestAsync(ApiRequestContext con
/// </summary>
public override async System.Threading.Tasks.Task OnResponseAsync(ApiResponseContext context)
{
var sdkOptions = context.HttpContext.ServiceProvider
.GetRequiredService<IOptionsMonitor<FeishuNetSdkOptions>>();
if (!sdkOptions.CurrentValue.IgnoreStatusException)
return;

if (context.Exception is ApiResponseStatusException statusException && statusException != null)
{
if (!context.ApiAction.Return.DataType.IsRawType && context.HttpContext.ResponseMessage != null)
var sdkOptions = context.HttpContext.ServiceProvider
.GetRequiredService<IOptionsMonitor<FeishuNetSdkOptions>>();

if (!sdkOptions.CurrentValue.IgnoreStatusException)
return;

if (!context.ActionDescriptor.Return.DataType.IsRawType && context.HttpContext.ResponseMessage != null)
{
string content = await context.HttpContext.ResponseMessage.Content.ReadAsStringAsync();
context.Result = JsonSerializer.Deserialize(content, context.ApiAction.Return.DataType.Type);
}
else
{
context.Result = context.HttpContext.ResponseMessage;
bool isJson = context.HttpContext.ResponseMessage.Content.Headers.IsJsonContent();

context.Result = JsonSerializer.Deserialize(
isJson ? content : $"{{\"msg\":\"{statusException.Message} - {content}\"}}",
context.ActionDescriptor.Return.DataType.Type);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/DependencyInjection/FeishuNetSdkExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public static IServiceCollection AddFeishuNetSdk(this IServiceCollection service
private static IServiceCollection AddFeishuNetSdk(this IServiceCollection services,
FeishuNetSdkOptions options)
{
services.AddWebApiClient();
services.AddWebApiClient()
.UseJsonFirstApiActionDescriptor();

services.AddHttpApi<IFeishuApi>();
services.AddHttpApi<IFeishuAppApi>();
Expand Down
10 changes: 10 additions & 0 deletions src/Extensions/HttpContentExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace FeishuNetSdk
{
internal static class HttpContentExtension
{
public static bool IsJsonContent(this System.Net.Http.Headers.HttpContentHeaders headers)
{
return headers.Any(p => p.Key == "Content-Type" && p.Value.Any(k => k.Contains("application/json")));
}
}
}
2 changes: 1 addition & 1 deletion src/FeishuNetSdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="WebApiClientCore.Extensions.OAuths" Version="2.1.1" />
<PackageReference Include="WebApiClientCore.Extensions.OAuths" Version="2.1.3" />
</ItemGroup>

</Project>

0 comments on commit 048cd04

Please sign in to comment.