Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.2.2 Code Refactor #856

Merged
merged 6 commits into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
## 2.2.2
- Code refactor
## 2.2.1
-Fix[#847]: DefaultRequestHeaders can not be null or empty with dotnet 8
-Fix[#849]: Temporary disable PublishTrimmed
- Fix[#847]: DefaultRequestHeaders can not be null or empty with dotnet 8
- Fix[#849]: Temporary disable PublishTrimmed
## 2.2.0
- Migrate from dotnet 6.0 to dotnet 8.0
- Add Bruno to document the APIs
- Fix[#824]: Log cookie when qinglong save env failed
- Fix[#648]: Set DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1 in qinglong to ignore random "Couldn't find a valid ICU package" issue
## 2.1.3
- Code refactory
- Code refactor
- Fix[#791]:修复VipBigPoint任务异常导致终止的问题
## 2.1.2
- Feature: enhancement CICD scripts
Expand Down
2 changes: 1 addition & 1 deletion common.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<Authors>Ray</Authors>
<Version>2.2.1</Version>
<Version>2.2.2</Version>
<NoWarn>$(NoWarn);CS1591;CS0436</NoWarn>
</PropertyGroup>
</Project>
114 changes: 48 additions & 66 deletions src/Ray.BiliBiliTool.Agent/Attributes/AppendHeaderAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,88 +1,70 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using WebApiClientCore;
using WebApiClientCore.Attributes;

namespace Ray.BiliBiliTool.Agent.Attributes
namespace Ray.BiliBiliTool.Agent.Attributes;

[DebuggerDisplay("{name} = {value}")]
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Parameter, AllowMultiple = true)]
public class AppendHeaderAttribute(
string name,
string value,
AppendHeaderType appendHeaderType = AppendHeaderType.AddOrReplace)
: ApiActionAttribute, IApiParameterAttribute
{
[DebuggerDisplay("{name} = {value}")]
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Parameter, AllowMultiple = true, Inherited = true)]
public class AppendHeaderAttribute : ApiActionAttribute, IApiParameterAttribute, IApiAttribute
{
//添加的顺序为:子类、基类、子类函数、子类函数入参
//添加的顺序为:子类、基类、子类函数、子类函数入参

private readonly string _name;
private readonly string _value;
private readonly AppendHeaderType _appendHeaderType;
private readonly string _aliasName;
private readonly string _aliasName;

public AppendHeaderAttribute(string name, string value, AppendHeaderType appendHeaderType = AppendHeaderType.AddOrReplace)
{
_name = name;
_value = value;
_appendHeaderType = appendHeaderType;
}
public AppendHeaderAttribute(string aliasName, AppendHeaderType appendHeaderType = AppendHeaderType.AddOrReplace) : this(null, null, appendHeaderType)
{
_aliasName = aliasName;
}

public AppendHeaderAttribute(string aliasName, AppendHeaderType appendHeaderType = AppendHeaderType.AddOrReplace)
{
_aliasName = aliasName;
_appendHeaderType = appendHeaderType;
}
public override Task OnRequestAsync(ApiRequestContext context)
{
AddByAppendType(context.HttpContext.RequestMessage.Headers, name, value);
return Task.CompletedTask;
}

public override Task OnRequestAsync(ApiRequestContext context)
public Task OnRequestAsync(ApiParameterContext context)
{
string parameterName = _aliasName;
if (string.IsNullOrEmpty(parameterName))
{
AddByAppendType(context.HttpContext.RequestMessage.Headers, _name, _value);
return Task.CompletedTask;
parameterName = context.ParameterName;
}

public Task OnRequestAsync(ApiParameterContext context)
string text = context.ParameterValue?.ToString();
if (!string.IsNullOrEmpty(text))
{
string parameterName = _aliasName;
if (string.IsNullOrEmpty(parameterName))
{
parameterName = context.ParameterName;
}

string text = context.ParameterValue?.ToString();
if (!string.IsNullOrEmpty(text))
{
AddByAppendType(context.HttpContext.RequestMessage.Headers, parameterName, text);
}

return Task.CompletedTask;
AddByAppendType(context.HttpContext.RequestMessage.Headers, parameterName, text);
}

private void AddByAppendType(HttpRequestHeaders headers, string key, string value)
{
switch (_appendHeaderType)
{
case AppendHeaderType.Add:
headers.TryAddWithoutValidation(key, value);
break;
case AppendHeaderType.AddIfNotExist:
if (!headers.Contains(key))
headers.TryAddWithoutValidation(key, value);
break;
case AppendHeaderType.AddOrReplace:
if (headers.Contains(key))
headers.Remove(key);
headers.TryAddWithoutValidation(key, value);
break;
default:
break;
}
}
return Task.CompletedTask;
}

public enum AppendHeaderType
private void AddByAppendType(HttpRequestHeaders headers, string key, string value)
{
Add,
AddIfNotExist,
AddOrReplace
switch (appendHeaderType)
{
case AppendHeaderType.Add:
headers.TryAddWithoutValidation(key, value);
break;
case AppendHeaderType.AddIfNotExist:
if (!headers.Contains(key))
headers.TryAddWithoutValidation(key, value);
break;
case AppendHeaderType.AddOrReplace:
if (headers.Contains(key))
headers.Remove(key);
headers.TryAddWithoutValidation(key, value);
break;
default:
break;
}
}
}
}
8 changes: 8 additions & 0 deletions src/Ray.BiliBiliTool.Agent/Attributes/AppendHeaderType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Ray.BiliBiliTool.Agent.Attributes;

public enum AppendHeaderType
{
Add,
AddIfNotExist,
AddOrReplace
}
62 changes: 27 additions & 35 deletions src/Ray.BiliBiliTool.Agent/Attributes/LogFilterAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,39 @@
using WebApiClientCore;
using WebApiClientCore.Attributes;

namespace Ray.BiliBiliTool.Agent.Attributes
namespace Ray.BiliBiliTool.Agent.Attributes;

public class LogFilterAttribute(bool logError = true) : LoggingFilterAttribute
{
public class LogFilterAttribute : LoggingFilterAttribute
protected override Task WriteLogAsync(ApiResponseContext context, LogMessage logMessage)
{
private readonly bool _logError;
ILoggerFactory loggerFactory = context.HttpContext.ServiceProvider.GetService<ILoggerFactory>();
if (loggerFactory == null) return Task.CompletedTask;

MethodInfo member = context.ApiAction.Member;
var strArray = new string[5];
Type declaringType1 = member.DeclaringType;
strArray[0] = (object)declaringType1 != null ? declaringType1.Namespace : null;
strArray[1] = ".";
Type declaringType2 = member.DeclaringType;
strArray[2] = (object)declaringType2 != null ? declaringType2.Name : null;
strArray[3] = ".";
strArray[4] = member.Name;
string categoryName = string.Concat(strArray);
ILogger logger = loggerFactory.CreateLogger(categoryName);

public LogFilterAttribute(bool logError = true)
if (logMessage.Exception == null)
{
_logError = logError;
logger.LogDebug(logMessage.ToString());//修改为Debug等级
}

protected override Task WriteLogAsync(ApiResponseContext context, LogMessage logMessage)
else
{
ILoggerFactory loggerFactory = context.HttpContext.ServiceProvider.GetService<ILoggerFactory>();
if (loggerFactory == null) return Task.CompletedTask;

MethodInfo member = context.ApiAction.Member;
var strArray = new string[5];
Type declaringType1 = member.DeclaringType;
strArray[0] = (object)declaringType1 != null ? declaringType1.Namespace : null;
strArray[1] = ".";
Type declaringType2 = member.DeclaringType;
strArray[2] = (object)declaringType2 != null ? declaringType2.Name : null;
strArray[3] = ".";
strArray[4] = member.Name;
string categoryName = string.Concat(strArray);
ILogger logger = loggerFactory.CreateLogger(categoryName);

if (logMessage.Exception == null)
{
logger.LogDebug(logMessage.ToString());//修改为Debug等级
}
if (logError)
logger.LogError(logMessage.ToString());
else
{
if (_logError)
logger.LogError(logMessage.ToString());
else
logger.LogDebug(logMessage.ToString());
}

return Task.CompletedTask;
logger.LogDebug(logMessage.ToString());
}

return Task.CompletedTask;
}
}
}
39 changes: 16 additions & 23 deletions src/Ray.BiliBiliTool.Agent/BiliBiliAgent/Dtos/AddCoinRequest.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos;

namespace Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos
public class AddCoinRequest
{
public class AddCoinRequest
public AddCoinRequest(long aid, string csrf)
{
public AddCoinRequest(long aid, string csrf)
{
Aid = aid;
Csrf = csrf;
}
Aid = aid;
Csrf = csrf;
}

public long Aid { get; set; }
public long Aid { get; set; }

public int Multiply { get; set; } = 1;
public int Multiply { get; set; } = 1;

public int Select_like { get; set; } = 1;
public int Select_like { get; set; } = 1;

public string Cross_domain { get; set; } = "true";
public string Cross_domain { get; set; } = "true";

public string Csrf { get; set; }
public string Csrf { get; set; }

public string Eab_x { get; set; } = "2";
public string Eab_x { get; set; } = "2";

public string Ramval { get; set; } = "3";
public string Ramval { get; set; } = "3";

public string Source { get; set; } = "web_normal";
public string Source { get; set; } = "web_normal";

public string Ga { get; set; } = "1";
}
}
public string Ga { get; set; } = "1";
}
21 changes: 10 additions & 11 deletions src/Ray.BiliBiliTool.Agent/BiliBiliAgent/Dtos/BiliApiResponse.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
namespace Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos
namespace Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos;

public class BiliApiResponse : BiliApiResponse<object>
{
public class BiliApiResponse : BiliApiResponse<object>
{

}
}

public class BiliApiResponse<TData>
{
public int Code { get; set; } = int.MinValue;
public class BiliApiResponse<TData>
{
public int Code { get; set; } = int.MinValue;

public string Message { get; set; }
public string Message { get; set; }

public TData Data { get; set; }
}
}
public TData Data { get; set; }
}
33 changes: 16 additions & 17 deletions src/Ray.BiliBiliTool.Agent/BiliBiliAgent/Dtos/BiliPageResult.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
namespace Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos
namespace Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos;

public class BiliPageResult
{
public class BiliPageResult
{
/// <summary>
/// 视频总数量
/// </summary>
public int Count { get; set; }
/// <summary>
/// 视频总数量
/// </summary>
public int Count { get; set; }

/// <summary>
/// 页码
/// </summary>
public int Pn { get; set; }
/// <summary>
/// 页码
/// </summary>
public int Pn { get; set; }

/// <summary>
/// 每页条数
/// </summary>
public int Ps { get; set; }
}
}
/// <summary>
/// 每页条数
/// </summary>
public int Ps { get; set; }
}
Loading
Loading