Browse Source

feat(webhooks): add default http headers.

upt-7.0.1
cKey 3 years ago
parent
commit
7e4ac8691c
  1. 23
      aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/AbpWebhooksOptions.cs
  2. 19
      aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks/LINGYUN/Abp/Webhooks/DefaultWebhookSender.cs

23
aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/AbpWebhooksOptions.cs

@ -28,6 +28,10 @@ public class AbpWebhooksOptions
public HashSet<string> DeletedWebhooks { get; }
public HashSet<string> DeletedWebhookGroups { get; }
/// <summary>
/// 默认请求头
/// </summary>
public IDictionary<string, string> DefaultHttpHeaders { get; }
public AbpWebhooksOptions()
{
@ -39,5 +43,24 @@ public class AbpWebhooksOptions
DeletedWebhooks = new HashSet<string>();
DeletedWebhookGroups = new HashSet<string>();
DefaultHttpHeaders = new Dictionary<string, string>
{
// 取消响应内容包装
{ "_AbpDontWrapResult", "true" },
// TODO: 可能跨域影响
// { "X-Requested-With", "XMLHttpRequest" },
// 标识来源
{ "X-Requested-From", "abp-webhooks" },
};
}
public void AddHeader(string key, string value)
{
if (value.IsNullOrWhiteSpace())
{
return;
}
DefaultHttpHeaders[key] = value;
}
}

19
aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks/LINGYUN/Abp/Webhooks/DefaultWebhookSender.cs

@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
@ -15,6 +16,7 @@ namespace LINGYUN.Abp.Webhooks
{
public ILogger<DefaultWebhookSender> Logger { protected get; set; }
private readonly AbpWebhooksOptions _options;
private readonly IWebhookManager _webhookManager;
private readonly IHttpClientFactory _httpClientFactory;
@ -22,8 +24,10 @@ namespace LINGYUN.Abp.Webhooks
public DefaultWebhookSender(
IWebhookManager webhookManager,
IHttpClientFactory httpClientFactory)
IHttpClientFactory httpClientFactory,
IOptions<AbpWebhooksOptions> options)
{
_options = options.Value;
_webhookManager = webhookManager;
_httpClientFactory = httpClientFactory;
@ -110,6 +114,19 @@ namespace LINGYUN.Abp.Webhooks
protected virtual void AddAdditionalHeaders(HttpRequestMessage request, WebhookSenderArgs webhookSenderArgs)
{
foreach (var header in _options.DefaultHttpHeaders)
{
if (request.Headers.TryAddWithoutValidation(header.Key, header.Value))
{
continue;
}
if (request.Content.Headers.TryAddWithoutValidation(header.Key, header.Value))
{
continue;
}
}
foreach (var header in webhookSenderArgs.Headers)
{
if (request.Headers.TryAddWithoutValidation(header.Key, header.Value))

Loading…
Cancel
Save