2025-08-20 09:10:10 +08:00
|
|
|
|
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
|
|
|
|
|
|
|
|
|
|
|
namespace Admin.NET.Core;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Http远程服务扩展
|
|
|
|
|
|
/// </summary>
|
2025-08-28 02:42:37 +08:00
|
|
|
|
public static class HttpRemotesExtension
|
|
|
|
|
|
{
|
2025-08-20 09:10:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加Http远程服务
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="services"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static IServiceCollection AddHttpRemoteClientService(this IServiceCollection services)
|
|
|
|
|
|
{
|
2025-08-28 01:54:44 +08:00
|
|
|
|
var options = App.GetOptions<HttpRemotesOptions>() ?? throw new Exception("未正确配置HttpRemotes.json");
|
2025-08-20 09:10:10 +08:00
|
|
|
|
foreach (var prop in options.GetType().GetProperties())
|
|
|
|
|
|
{
|
|
|
|
|
|
if (prop.GetValue(options) is not HttpRemoteItem opt) continue;
|
|
|
|
|
|
services.AddHttpClient(opt.HttpName, client =>
|
2025-08-28 02:42:37 +08:00
|
|
|
|
{
|
|
|
|
|
|
client.BaseAddress = new Uri(opt.BaseAddress);
|
|
|
|
|
|
client.Timeout = TimeSpan.FromSeconds(opt.Timeout);
|
|
|
|
|
|
foreach (var kv in opt.Headers) client.DefaultRequestHeaders.Add(kv.Key, kv.Value);
|
|
|
|
|
|
})
|
|
|
|
|
|
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
|
2025-08-20 09:10:10 +08:00
|
|
|
|
{
|
|
|
|
|
|
UseCookies = opt.UseCookies
|
2025-08-28 01:54:44 +08:00
|
|
|
|
})
|
|
|
|
|
|
.AddHttpMessageHandler<HttpLoggingHandler>();
|
2025-08-20 09:10:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
return services;
|
|
|
|
|
|
}
|
2025-08-21 08:51:42 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-08-24 00:09:42 +08:00
|
|
|
|
/// 携带接口描述相关属性
|
2025-08-21 08:51:42 +08:00
|
|
|
|
/// </summary>
|
2025-08-24 00:09:42 +08:00
|
|
|
|
/// <param name="builder"></param>
|
2025-08-24 16:15:05 +08:00
|
|
|
|
/// <param name="attr"></param>
|
2025-08-28 01:54:44 +08:00
|
|
|
|
/// <param name="reqPlaintext"></param>
|
2025-08-24 00:09:42 +08:00
|
|
|
|
/// <returns></returns>
|
2025-08-28 01:54:44 +08:00
|
|
|
|
public static HttpRequestBuilder SetRemoteApiAttr(this HttpRequestBuilder builder, HttpRemoteApiAttribute attr, string reqPlaintext = null)
|
2025-08-21 08:51:42 +08:00
|
|
|
|
{
|
2025-08-28 01:54:44 +08:00
|
|
|
|
return HttpLoggingHandler.SetRemoteApiAttr(builder, attr, reqPlaintext);
|
2025-08-21 08:51:42 +08:00
|
|
|
|
}
|
2025-08-20 09:10:10 +08:00
|
|
|
|
}
|