From 2d6e40889ee135b113c8f6c4aa26e83820d05dfe Mon Sep 17 00:00:00 2001 From: cKey <35512826+colinin@users.noreply.github.com> Date: Wed, 13 Apr 2022 17:18:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=AE=B0=E5=BD=95=E5=8F=91=E9=80=81?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E4=B8=8E=E5=93=8D=E5=BA=94=E6=A0=87=E5=A4=B4?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E7=8E=AF=E5=A2=83=E8=B0=83=E8=AF=95.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Abp/Webhooks/DefaultWebhookSender.cs | 42 ++++- .../LINGYUN/Abp/Webhooks/IWebhookManager.cs | 9 +- .../LINGYUN/Abp/Webhooks/WebhookManager.cs | 9 +- .../WebhookSendRecordConsts.cs | 2 + .../DefaultWebhookManager.cs | 25 ++- .../WebhooksManagement/WebhookSendRecord.cs | 14 +- ...agementDbContextModelCreatingExtensions.cs | 6 + ...onse-Headers-With-SendAttempts.Designer.cs | 172 ++++++++++++++++++ ...-And-Response-Headers-With-SendAttempts.cs | 39 ++++ ...agementMigrationsDbContextModelSnapshot.cs | 12 +- 10 files changed, 313 insertions(+), 17 deletions(-) create mode 100644 aspnet-core/services/LY.MicroService.WebhooksManagement.HttpApi.Host/Migrations/20220413084629_Add-Field-Request-And-Response-Headers-With-SendAttempts.Designer.cs create mode 100644 aspnet-core/services/LY.MicroService.WebhooksManagement.HttpApi.Host/Migrations/20220413084629_Add-Field-Request-And-Response-Headers-With-SendAttempts.cs diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks/LINGYUN/Abp/Webhooks/DefaultWebhookSender.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks/LINGYUN/Abp/Webhooks/DefaultWebhookSender.cs index 19982a68e..04f84dcd6 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks/LINGYUN/Abp/Webhooks/DefaultWebhookSender.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks/LINGYUN/Abp/Webhooks/DefaultWebhookSender.cs @@ -1,8 +1,11 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using System; +using System.Collections.Generic; +using System.Linq; using System.Net; using System.Net.Http; +using System.Net.Http.Headers; using System.Threading.Tasks; using Volo.Abp.DependencyInjection; @@ -52,13 +55,17 @@ namespace LINGYUN.Abp.Webhooks var isSucceed = false; HttpStatusCode? statusCode = null; var content = FailedRequestDefaultContent; + var reqHeaders = GetHeaders(request.Headers); + IDictionary resHeaders = null; try { var response = await SendHttpRequest(request); - isSucceed = response.isSucceed; - statusCode = response.statusCode; - content = response.content; + + isSucceed = response.IsSuccessStatusCode; + statusCode = response.StatusCode; + resHeaders = GetHeaders(response.Headers); + content = await response.Content.ReadAsStringAsync(); } catch (TaskCanceledException) { @@ -75,7 +82,13 @@ namespace LINGYUN.Abp.Webhooks } finally { - await _webhookManager.StoreResponseOnWebhookSendAttemptAsync(webhookSendAttemptId, webhookSenderArgs.TenantId, statusCode, content); + await _webhookManager.StoreResponseOnWebhookSendAttemptAsync( + webhookSendAttemptId, + webhookSenderArgs.TenantId, + statusCode, + content, + reqHeaders, + resHeaders); } if (!isSucceed) @@ -113,17 +126,26 @@ namespace LINGYUN.Abp.Webhooks } } - protected virtual async Task<(bool isSucceed, HttpStatusCode statusCode, string content)> SendHttpRequest(HttpRequestMessage request) + protected virtual async Task SendHttpRequest(HttpRequestMessage request) { var client = _httpClientFactory.CreateClient(AbpWebhooksModule.WebhooksClient); - var response = await client.SendAsync(request); + return await client.SendAsync(request); + } - var isSucceed = response.IsSuccessStatusCode; - var statusCode = response.StatusCode; - var content = await response.Content.ReadAsStringAsync(); + private IDictionary GetHeaders(HttpHeaders headers) + { + var res = new Dictionary(); - return (isSucceed, statusCode, content); + if (headers != null && headers.Any()) + { + foreach (var header in headers) + { + res.Add(header.Key, header.Value.JoinAsString(";")); + } + } + + return res; } } } diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks/LINGYUN/Abp/Webhooks/IWebhookManager.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks/LINGYUN/Abp/Webhooks/IWebhookManager.cs index 1e8e5fd0c..aa5fa026c 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks/LINGYUN/Abp/Webhooks/IWebhookManager.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks/LINGYUN/Abp/Webhooks/IWebhookManager.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Net; using System.Net.Http; using System.Threading.Tasks; @@ -16,7 +17,11 @@ namespace LINGYUN.Abp.Webhooks Task InsertAndGetIdWebhookSendAttemptAsync(WebhookSenderArgs webhookSenderArgs); Task StoreResponseOnWebhookSendAttemptAsync( - Guid webhookSendAttemptId, Guid? tenantId, - HttpStatusCode? statusCode, string content); + Guid webhookSendAttemptId, + Guid? tenantId, + HttpStatusCode? statusCode, + string content, + IDictionary requestHeaders = null, + IDictionary responseHeaders = null); } } diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks/LINGYUN/Abp/Webhooks/WebhookManager.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks/LINGYUN/Abp/Webhooks/WebhookManager.cs index 448c302c4..7816fd807 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks/LINGYUN/Abp/Webhooks/WebhookManager.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks/LINGYUN/Abp/Webhooks/WebhookManager.cs @@ -1,5 +1,6 @@ using Newtonsoft.Json; using System; +using System.Collections.Generic; using System.Globalization; using System.Net; using System.Net.Http; @@ -75,6 +76,12 @@ namespace LINGYUN.Abp.Webhooks public abstract Task InsertAndGetIdWebhookSendAttemptAsync(WebhookSenderArgs webhookSenderArgs); - public abstract Task StoreResponseOnWebhookSendAttemptAsync(Guid webhookSendAttemptId, Guid? tenantId, HttpStatusCode? statusCode, string content); + public abstract Task StoreResponseOnWebhookSendAttemptAsync( + Guid webhookSendAttemptId, + Guid? tenantId, + HttpStatusCode? statusCode, + string content, + IDictionary requestHeaders = null, + IDictionary responseHeaders = null); } } diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain.Shared/LINGYUN/Abp/WebhooksManagement/WebhookSendRecordConsts.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain.Shared/LINGYUN/Abp/WebhooksManagement/WebhookSendRecordConsts.cs index 464658f04..6c3df0ce0 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain.Shared/LINGYUN/Abp/WebhooksManagement/WebhookSendRecordConsts.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain.Shared/LINGYUN/Abp/WebhooksManagement/WebhookSendRecordConsts.cs @@ -3,4 +3,6 @@ public static class WebhookSendRecordConsts { public static int MaxResponseLength { get; set; } = int.MaxValue; + + public static int MaxHeadersLength { get; set; } = int.MaxValue; } diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/DefaultWebhookManager.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/DefaultWebhookManager.cs index c0ab03de1..659677104 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/DefaultWebhookManager.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/DefaultWebhookManager.cs @@ -1,5 +1,7 @@ using LINGYUN.Abp.Webhooks; +using Newtonsoft.Json; using System; +using System.Collections.Generic; using System.Net; using System.Threading.Tasks; using Volo.Abp.DependencyInjection; @@ -47,12 +49,31 @@ public class DefaultWebhookManager : WebhookManager, ITransientDependency } [UnitOfWork] - public async override Task StoreResponseOnWebhookSendAttemptAsync(Guid webhookSendAttemptId, Guid? tenantId, HttpStatusCode? statusCode, string content) + public async override Task StoreResponseOnWebhookSendAttemptAsync( + Guid webhookSendAttemptId, + Guid? tenantId, + HttpStatusCode? statusCode, + string content, + IDictionary requestHeaders = null, + IDictionary responseHeaders = null) { using (CurrentTenant.Change(tenantId)) { + var reqHeaders = "{}"; + var resHeaders = "{}"; + if (requestHeaders != null) + { + reqHeaders = JsonConvert.SerializeObject(requestHeaders); + } + if (responseHeaders != null) + { + resHeaders = JsonConvert.SerializeObject(responseHeaders); + } + var record = await WebhookSendAttemptRepository.GetAsync(webhookSendAttemptId); - record.SetResponse(content, statusCode); + record.SetResponse(content, statusCode, resHeaders); + // 加入标头信息,便于维护人员调试 + record.SetRequestHeaders(reqHeaders); await WebhookSendAttemptRepository.UpdateAsync(record); } diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/WebhookSendRecord.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/WebhookSendRecord.cs index 2508ce840..7b59f356e 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/WebhookSendRecord.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/WebhookSendRecord.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Net; using Volo.Abp; using Volo.Abp.Auditing; @@ -18,6 +19,10 @@ public class WebhookSendRecord : Entity, IHasCreationTime, IHasModificatio public virtual HttpStatusCode? ResponseStatusCode { get; set; } + public virtual string RequestHeaders { get; protected set; } + + public virtual string ResponseHeaders { get; protected set; } + public virtual DateTime CreationTime { get; set; } public virtual DateTime? LastModificationTime { get; set; } @@ -42,9 +47,16 @@ public class WebhookSendRecord : Entity, IHasCreationTime, IHasModificatio public void SetResponse( string response, - HttpStatusCode? statusCode = null) + HttpStatusCode? statusCode = null, + string responseHeaders = null) { Response = Check.Length(response, nameof(response), WebhookSendRecordConsts.MaxResponseLength); ResponseStatusCode = statusCode; + ResponseHeaders = Check.Length(responseHeaders, nameof(responseHeaders), WebhookSendRecordConsts.MaxHeadersLength); + } + + public void SetRequestHeaders(string requestHeaders = null) + { + RequestHeaders = Check.Length(requestHeaders, nameof(requestHeaders), WebhookSendRecordConsts.MaxHeadersLength); } } diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.EntityFrameworkCore/LINGYUN/Abp/WebhooksManagement/EntityFrameworkCore/WebhooksManagementDbContextModelCreatingExtensions.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.EntityFrameworkCore/LINGYUN/Abp/WebhooksManagement/EntityFrameworkCore/WebhooksManagementDbContextModelCreatingExtensions.cs index 9df84fefd..6d63d6278 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.EntityFrameworkCore/LINGYUN/Abp/WebhooksManagement/EntityFrameworkCore/WebhooksManagementDbContextModelCreatingExtensions.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.EntityFrameworkCore/LINGYUN/Abp/WebhooksManagement/EntityFrameworkCore/WebhooksManagementDbContextModelCreatingExtensions.cs @@ -41,6 +41,12 @@ public static class WebhooksManagementDbContextModelCreatingExtensions b.Property(p => p.Response) .HasColumnName(nameof(WebhookSendRecord.Response)) .HasMaxLength(WebhookSendRecordConsts.MaxResponseLength); + b.Property(p => p.RequestHeaders) + .HasColumnName(nameof(WebhookSendRecord.RequestHeaders)) + .HasMaxLength(WebhookSendRecordConsts.MaxHeadersLength); + b.Property(p => p.ResponseHeaders) + .HasColumnName(nameof(WebhookSendRecord.ResponseHeaders)) + .HasMaxLength(WebhookSendRecordConsts.MaxHeadersLength); b.ConfigureByConvention(); diff --git a/aspnet-core/services/LY.MicroService.WebhooksManagement.HttpApi.Host/Migrations/20220413084629_Add-Field-Request-And-Response-Headers-With-SendAttempts.Designer.cs b/aspnet-core/services/LY.MicroService.WebhooksManagement.HttpApi.Host/Migrations/20220413084629_Add-Field-Request-And-Response-Headers-With-SendAttempts.Designer.cs new file mode 100644 index 000000000..813c4b91e --- /dev/null +++ b/aspnet-core/services/LY.MicroService.WebhooksManagement.HttpApi.Host/Migrations/20220413084629_Add-Field-Request-And-Response-Headers-With-SendAttempts.Designer.cs @@ -0,0 +1,172 @@ +// +using System; +using LY.MicroService.WebhooksManagement.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Volo.Abp.EntityFrameworkCore; + +#nullable disable + +namespace LY.MicroService.WebhooksManagement.Migrations +{ + [DbContext(typeof(WebhooksManagementMigrationsDbContext))] + [Migration("20220413084629_Add-Field-Request-And-Response-Headers-With-SendAttempts")] + partial class AddFieldRequestAndResponseHeadersWithSendAttempts + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("_Abp_DatabaseProvider", EfCoreDatabaseProvider.MySql) + .HasAnnotation("ProductVersion", "6.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 64); + + modelBuilder.Entity("LINGYUN.Abp.WebhooksManagement.WebhookEventRecord", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("CreationTime") + .HasColumnType("datetime(6)") + .HasColumnName("CreationTime"); + + b.Property("Data") + .HasMaxLength(2147483647) + .HasColumnType("longtext") + .HasColumnName("Data"); + + b.Property("DeletionTime") + .HasColumnType("datetime(6)") + .HasColumnName("DeletionTime"); + + b.Property("IsDeleted") + .ValueGeneratedOnAdd() + .HasColumnType("tinyint(1)") + .HasDefaultValue(false) + .HasColumnName("IsDeleted"); + + b.Property("TenantId") + .HasColumnType("char(36)"); + + b.Property("WebhookName") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("varchar(100)") + .HasColumnName("WebhookName"); + + b.HasKey("Id"); + + b.ToTable("AbpWebhooksEvents", (string)null); + }); + + modelBuilder.Entity("LINGYUN.Abp.WebhooksManagement.WebhookSendRecord", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("CreationTime") + .HasColumnType("datetime(6)") + .HasColumnName("CreationTime"); + + b.Property("LastModificationTime") + .HasColumnType("datetime(6)") + .HasColumnName("LastModificationTime"); + + b.Property("RequestHeaders") + .HasMaxLength(2147483647) + .HasColumnType("longtext") + .HasColumnName("RequestHeaders"); + + b.Property("Response") + .HasMaxLength(2147483647) + .HasColumnType("longtext") + .HasColumnName("Response"); + + b.Property("ResponseHeaders") + .HasMaxLength(2147483647) + .HasColumnType("longtext") + .HasColumnName("ResponseHeaders"); + + b.Property("ResponseStatusCode") + .HasColumnType("int"); + + b.Property("TenantId") + .HasColumnType("char(36)"); + + b.Property("WebhookEventId") + .HasColumnType("char(36)"); + + b.Property("WebhookSubscriptionId") + .HasColumnType("char(36)"); + + b.HasKey("Id"); + + b.HasIndex("WebhookEventId"); + + b.ToTable("AbpWebhooksSendAttempts", (string)null); + }); + + modelBuilder.Entity("LINGYUN.Abp.WebhooksManagement.WebhookSubscription", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("CreationTime") + .HasColumnType("datetime(6)") + .HasColumnName("CreationTime"); + + b.Property("CreatorId") + .HasColumnType("char(36)") + .HasColumnName("CreatorId"); + + b.Property("Headers") + .HasMaxLength(2147483647) + .HasColumnType("longtext") + .HasColumnName("Headers"); + + b.Property("IsActive") + .HasColumnType("tinyint(1)"); + + b.Property("Secret") + .HasMaxLength(128) + .HasColumnType("varchar(128)") + .HasColumnName("Secret"); + + b.Property("TenantId") + .HasColumnType("char(36)"); + + b.Property("WebhookUri") + .IsRequired() + .HasMaxLength(255) + .HasColumnType("varchar(255)") + .HasColumnName("WebhookUri"); + + b.Property("Webhooks") + .HasMaxLength(2147483647) + .HasColumnType("longtext") + .HasColumnName("Webhooks"); + + b.HasKey("Id"); + + b.ToTable("AbpWebhooksSubscriptions", (string)null); + }); + + modelBuilder.Entity("LINGYUN.Abp.WebhooksManagement.WebhookSendRecord", b => + { + b.HasOne("LINGYUN.Abp.WebhooksManagement.WebhookEventRecord", "WebhookEvent") + .WithOne() + .HasForeignKey("LINGYUN.Abp.WebhooksManagement.WebhookSendRecord", "WebhookEventId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("WebhookEvent"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/aspnet-core/services/LY.MicroService.WebhooksManagement.HttpApi.Host/Migrations/20220413084629_Add-Field-Request-And-Response-Headers-With-SendAttempts.cs b/aspnet-core/services/LY.MicroService.WebhooksManagement.HttpApi.Host/Migrations/20220413084629_Add-Field-Request-And-Response-Headers-With-SendAttempts.cs new file mode 100644 index 000000000..8b63103b0 --- /dev/null +++ b/aspnet-core/services/LY.MicroService.WebhooksManagement.HttpApi.Host/Migrations/20220413084629_Add-Field-Request-And-Response-Headers-With-SendAttempts.cs @@ -0,0 +1,39 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace LY.MicroService.WebhooksManagement.Migrations +{ + public partial class AddFieldRequestAndResponseHeadersWithSendAttempts : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "RequestHeaders", + table: "AbpWebhooksSendAttempts", + type: "longtext", + maxLength: 2147483647, + nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"); + + migrationBuilder.AddColumn( + name: "ResponseHeaders", + table: "AbpWebhooksSendAttempts", + type: "longtext", + maxLength: 2147483647, + nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "RequestHeaders", + table: "AbpWebhooksSendAttempts"); + + migrationBuilder.DropColumn( + name: "ResponseHeaders", + table: "AbpWebhooksSendAttempts"); + } + } +} diff --git a/aspnet-core/services/LY.MicroService.WebhooksManagement.HttpApi.Host/Migrations/WebhooksManagementMigrationsDbContextModelSnapshot.cs b/aspnet-core/services/LY.MicroService.WebhooksManagement.HttpApi.Host/Migrations/WebhooksManagementMigrationsDbContextModelSnapshot.cs index 39e0c86bd..1b8d69039 100644 --- a/aspnet-core/services/LY.MicroService.WebhooksManagement.HttpApi.Host/Migrations/WebhooksManagementMigrationsDbContextModelSnapshot.cs +++ b/aspnet-core/services/LY.MicroService.WebhooksManagement.HttpApi.Host/Migrations/WebhooksManagementMigrationsDbContextModelSnapshot.cs @@ -18,7 +18,7 @@ namespace LY.MicroService.WebhooksManagement.Migrations #pragma warning disable 612, 618 modelBuilder .HasAnnotation("_Abp_DatabaseProvider", EfCoreDatabaseProvider.MySql) - .HasAnnotation("ProductVersion", "6.0.3") + .HasAnnotation("ProductVersion", "6.0.4") .HasAnnotation("Relational:MaxIdentifierLength", 64); modelBuilder.Entity("LINGYUN.Abp.WebhooksManagement.WebhookEventRecord", b => @@ -74,11 +74,21 @@ namespace LY.MicroService.WebhooksManagement.Migrations .HasColumnType("datetime(6)") .HasColumnName("LastModificationTime"); + b.Property("RequestHeaders") + .HasMaxLength(2147483647) + .HasColumnType("longtext") + .HasColumnName("RequestHeaders"); + b.Property("Response") .HasMaxLength(2147483647) .HasColumnType("longtext") .HasColumnName("Response"); + b.Property("ResponseHeaders") + .HasMaxLength(2147483647) + .HasColumnType("longtext") + .HasColumnName("ResponseHeaders"); + b.Property("ResponseStatusCode") .HasColumnType("int");