修改代码

This commit is contained in:
chfei committed 2026-11-12 18:22:14 +08:00
1 parent f03e2b08f4
commit 3dcb6a1c34
24 files changed
+882 -480

No files matched your search

@@ -3,7 +3,6 @@ namespace Store.Management.Api;
public sealed class ApiClientOptions
{
public required Uri BaseUrl { get; init; }
public string Version { get; init; } = "v1";
public TimeSpan Timeout { get; init; } = TimeSpan.FromSeconds(30);
internal void Validate()
@@ -12,8 +11,6 @@ public sealed class ApiClientOptions
(BaseUrl.Scheme != "https" && BaseUrl.Scheme != "http") ||
!string.IsNullOrEmpty(BaseUrl.UserInfo) || !string.IsNullOrEmpty(BaseUrl.Query) || !string.IsNullOrEmpty(BaseUrl.Fragment))
throw new ArgumentException("BaseUrl 必须是无凭据、查询参数和片段的 HTTP(S) 绝对地址。");
if (!System.Text.RegularExpressions.Regex.IsMatch(Version, @"^v\d+(\.\d+)?$"))
throw new ArgumentException("Version 格式应为 v1 或 v1.0。");
if (Timeout <= TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(Timeout));
}
}
+20 -17
View File
@@ -7,23 +7,26 @@ namespace Store.Management.Api;
/// <summary>一个服务地址与一个用户会话对应一个实例;在应用容器中注册为单例。</summary>
public sealed class StoreApiClient : IDisposable
{
public TokenSession Session { get; } = new();
public ApiTransport Transport { get; }
public AuthApi Auth { get; }
public UserApi Users { get; }
public TokenSession Session { get; } = new();
public ApiTransport Transport { get; }
public AuthApi Auth { get; }
public UserApi Users { get; }
/// <param name="httpClient">可选的外部 HTTP 客户端,由调用方管理其生命周期。</param>
public StoreApiClient(ApiClientOptions options, HttpClient? httpClient = null)
{
ArgumentNullException.ThrowIfNull(options);
Transport = new ApiTransport(options, Session, httpClient);
Auth = new AuthApi(Transport, Session);
Users = new UserApi(Transport);
}
/// <param name="httpClient">可选的外部 HTTP 客户端,由调用方管理其生命周期。</param>
public StoreApiClient(ApiClientOptions options, HttpClient? httpClient = null)
{
ArgumentNullException.ThrowIfNull(options);
#if DEBUG
Session.Set("store-local-dev-7f2c9e4a6b814d38a5910e62c4f837bd");
#endif
Transport = new ApiTransport(options, Session, httpClient);
Auth = new AuthApi(Transport, Session);
Users = new UserApi(Transport);
}
public void Dispose()
{
Session.Clear();
Transport.Dispose();
}
public void Dispose()
{
Session.Clear();
Transport.Dispose();
}
}
@@ -12,14 +12,12 @@ public sealed class ApiTransport : IDisposable
{
private readonly RestClient _client;
private readonly TokenSession _session;
private readonly string _version;
private static readonly JsonSerializerOptions Json = new(JsonSerializerDefaults.Web);
internal ApiTransport(ApiClientOptions options, TokenSession session, HttpClient? httpClient)
{
options.Validate();
_session = session;
_version = options.Version;
RestClientOptions settings = new RestClientOptions(new Uri(options.BaseUrl.AbsoluteUri.TrimEnd('/') + "/"))
{
Timeout = options.Timeout,
@@ -47,7 +45,7 @@ public sealed class ApiTransport : IDisposable
{
throw new ApiException(ApiErrorKind.Unauthorized, "请先登录。", code: 401);
}
RestRequest request = new RestRequest($"{_version}/{route}", method);
RestRequest request = new RestRequest($"/{route}", method);
if (token is not null)
{
request.AddHeader("Authorization", $"Bearer {token}");
@@ -6,7 +6,7 @@ namespace Store.Management.Api.Users;
public sealed class UserSearchRequest : PageRequest
{
[StringLength(100)] public string? Keyword { get; set; }
public int? GroupId { get; set; }
public int? GroupId { get; set; }
[StringLength(20)] public string? ParentUserName { get; set; }
}