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;
namespace XFEExtension.NetCore.ServerInteractive.Utilities.Requester;
///
/// XFE客户端请求器构建器
///
[CreateImpl]
public abstract class XFEClientRequesterBuilder : XFEBuilderBase
{
private readonly XFEClientRequester _xFEClientRequester = new XFEClientRequesterImpl();
private readonly Dictionary> _requestServiceDictionary = [];
private readonly Dictionary> _standardRequestServiceDictionary = [];
private readonly Dictionary _standardClientInstanceRequestDictionary = [];
///
/// 创建构建器
///
///
public static XFEClientRequesterBuilder CreateBuilder()
{
var builder = new XFEClientRequesterBuilderImpl();
builder.AddParameter("XFEClientRequester", builder._xFEClientRequester);
return builder;
}
///
/// 添加请求
///
/// 请求服务泛型
/// 请求名称
///
public XFEClientRequesterBuilder AddOriginRequest(string serviceName) where T : IRequestService, new()
{
_requestServiceDictionary.Add(serviceName, () =>
{
var inst = new T();
ApplyParameter(inst);
return inst;
});
return this;
}
///
/// 注册标准请求服务(从RequestPoints/ResponsePoints/RequestRouteMap自动获取路由路径和名称)
///
/// 请求服务泛型
///
public XFEClientRequesterBuilder 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]标记方法");
// 为每个路径/名称注册服务工厂
foreach (var key in routeKeys)
{
_standardRequestServiceDictionary.Add(key, () =>
{
var inst = new T();
ApplyParameter(inst);
return inst;
});
}
return this;
}
///
/// 添加实例请求
///
/// 服务名称
/// 请求实例
///
public XFEClientRequesterBuilder AddRequest(string serviceName, StandardClientInstanceRequest xFEClientInstanceRequest)
{
_standardClientInstanceRequestDictionary.Add(serviceName, xFEClientInstanceRequest);
return this;
}
///
/// 添加实例请求
///
/// 请求服务名称
/// 构造请求体方法 ( session, deviceInfo, [] parameters)
/// 处理响应方法 ( response)
///
public XFEClientRequesterBuilder AddRequest(string serviceName, Func constructBody, Func? processResponse = null) => AddRequest(serviceName, new StandardClientInstanceRequest
{
ConstructBody = constructBody,
ProcessResponse = processResponse
});
///
/// 构建XFE客户端请求器
///
/// XFE客户端请求器
public XFEClientRequester Build(XFEClientRequesterOptions options)
{
_xFEClientRequester.RequestAddress = options.RequestAddress;
_xFEClientRequester.Session = options.Session;
_xFEClientRequester.DeviceInfo = options.DeviceInfo;
_xFEClientRequester.RequestServiceDictionary = _requestServiceDictionary;
_xFEClientRequester.StandardRequestServiceDictionary = _standardRequestServiceDictionary;
_xFEClientRequester.StandardClientInstanceRequestDictionary = _standardClientInstanceRequestDictionary;
return _xFEClientRequester;
}
///
/// 构建XFE客户端请求器
///
/// XFE客户端请求器
public XFEClientRequester Build(Action optionsBuilder)
{
var options = new XFEClientRequesterOptions();
optionsBuilder(options);
return Build(options);
}
}