using XFEExtension.NetCore.AutoImplement;
using XFEExtension.NetCore.ServerInteractive.Implements;
using XFEExtension.NetCore.ServerInteractive.Interfaces.Requester;
using XFEExtension.NetCore.ServerInteractive.Models.RequesterModels;
using XFEExtension.NetCore.ServerInteractive.Options;
using XFEExtension.NetCore.ServerInteractive.Utilities.Helpers;
using XFEExtension.NetCore.StringExtension;
namespace XFEExtension.NetCore.ServerInteractive.Utilities.Requester;
///
/// 客户端请求器构建器
///
[CreateImpl]
public abstract class ClientRequesterBuilder : XFEBuilderBase
{
private readonly ClientRequester _xFEClientRequester = new ClientRequesterImpl();
private readonly Dictionary> _requestServiceDictionary = [];
private readonly Dictionary> _standardRequestServiceDictionary = [];
private readonly List<(string Pattern, Func Factory)> _wildcardStandardRequestServiceList = [];
private readonly Dictionary _standardClientInstanceRequestDictionary = [];
///
/// 创建构建器
///
///
public static ClientRequesterBuilder CreateBuilder()
{
var builder = new ClientRequesterBuilderImpl();
builder.AddParameter("XFEClientRequester", builder._xFEClientRequester);
return builder;
}
///
/// 添加请求
///
/// 请求服务泛型
/// 请求名称
///
public ClientRequesterBuilder AddOriginRequest(string serviceName) where T : IRequestService, new()
{
_requestServiceDictionary.Add(serviceName, () =>
{
var inst = new T();
ApplyParameter(inst);
return inst;
});
return this;
}
///
/// 注册标准请求服务(从RequestPoints/ResponsePoints/RequestRouteMap自动获取路由路径和名称)
///
/// 请求服务泛型
///
public ClientRequesterBuilder AddRequest() where T : IStandardRequestService, new()
{
var probeService = new T();
ApplyParameter(probeService);
var routeKeys = probeService.RequestPoints.Keys
.Concat(probeService.ResponsePoints.Keys)
.Concat(probeService.RequestRouteMap.Keys)
.Distinct()
.ToList();
if (routeKeys.Count == 0)
throw new InvalidOperationException($"类型 {typeof(T).Name} 的 RequestPoints/ResponsePoints/RequestRouteMap 为空,请确保已使用[Request]或[Response]标记方法");
// 为每个路径/名称注册服务工厂(通配符路径注册到通配符列表,其余注册到标准字典)
// 对于Name别名,需检查其目标路径是否为通配符:Name→通配符路径不支持,因为无法确定具体请求路径
foreach (var key in routeKeys)
{
if (RouteMatchHelper.IsWildcardRoute(key))
{
if (_wildcardStandardRequestServiceList.Any(w => w.Pattern == key))
throw new InvalidOperationException($"通配符路由 '{key}' 重复注册");
_wildcardStandardRequestServiceList.Add((key, () =>
{
var inst = new T();
ApplyParameter(inst);
return inst;
}
));
}
else
{
// 检查Name别名是否映射到通配符路径
if (probeService.RequestRouteMap.TryGetValue(key, out var targetPath) && targetPath != key && RouteMatchHelper.IsWildcardRoute(targetPath))
throw new InvalidOperationException($"名称别名 '{key}' 映射到通配符路径 '{targetPath}',不支持通过名称调用通配符路由,请直接使用具体路径进行请求");
_standardRequestServiceDictionary.Add(key, () =>
{
var inst = new T();
ApplyParameter(inst);
return inst;
});
}
}
return this;
}
///
/// 添加实例请求
///
/// 路径
/// 请求实例
///
public ClientRequesterBuilder AddRequest(string route, StandardClientInstanceRequest xFEClientInstanceRequest)
{
_standardClientInstanceRequestDictionary.Add(route, xFEClientInstanceRequest);
return this;
}
///
/// 添加实例请求
///
/// 路径
/// 构造请求体方法 ( session, deviceInfo, [] parameters)
/// 处理响应方法 ( response)
///
public ClientRequesterBuilder AddRequest(string route, Func constructBody, Func? processResponse = null) => AddRequest(route, new StandardClientInstanceRequest
{
ConstructBody = constructBody,
ProcessResponse = processResponse
});
///
/// 使用表格请求器
///
///
///
public ClientRequesterBuilder UseTableRequester(TableRequester? tableRequester = null)
{
tableRequester ??= new();
_xFEClientRequester.TableRequester = tableRequester;
return this;
}
///
/// 构建XFE客户端请求器
///
/// XFE客户端请求器
public ClientRequester Build(XFEClientRequesterOptions options)
{
_xFEClientRequester.RequestAddress = options.RequestAddress;
_xFEClientRequester.Session = options.Session;
_xFEClientRequester.DeviceInfo = options.DeviceInfo;
if (_xFEClientRequester.TableRequester is not null)
{
if (_xFEClientRequester.TableRequester.RequestAddress.NullOrEmpty)
_xFEClientRequester.TableRequester.RequestAddress = options.RequestAddress;
if (_xFEClientRequester.TableRequester.DeviceInfo.NullOrEmpty)
_xFEClientRequester.TableRequester.DeviceInfo = options.DeviceInfo;
if (_xFEClientRequester.TableRequester.Session.NullOrEmpty)
_xFEClientRequester.TableRequester.Session = options.Session;
}
_xFEClientRequester.RequestServiceDictionary = _requestServiceDictionary;
_xFEClientRequester.StandardRequestServiceDictionary = _standardRequestServiceDictionary;
_xFEClientRequester.WildcardStandardRequestServiceList = _wildcardStandardRequestServiceList;
_xFEClientRequester.StandardClientInstanceRequestDictionary = _standardClientInstanceRequestDictionary;
return _xFEClientRequester;
}
///
/// 构建XFE客户端请求器
///
/// XFE客户端请求器
public ClientRequester Build(Action optionsBuilder)
{
var options = new XFEClientRequesterOptions();
optionsBuilder(options);
return Build(options);
}
}