Browse Source

Instead of asynchronous IWebhookDefinitionManager interface

pull/765/head
cKey 3 years ago
parent
commit
ba73d7687c
  1. 13
      aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/IDynamicWebhookDefinitionStore.cs
  2. 13
      aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/IStaticWebhookDefinitionStore.cs
  3. 12
      aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/IWebhookDefinitionManager.cs
  4. 34
      aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/NullDynamicWebhookDefinitionStore.cs
  5. 101
      aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/StaticWebhookDefinitionStore.cs
  6. 106
      aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/WebhookDefinitionManager.cs
  7. 3
      aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/Authorization/WebhooksManagementPermissionDefinitionProvider.cs
  8. 2
      aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionAppService.cs
  9. 3
      aspnet-core/services/LY.MicroService.WebhooksManagement.HttpApi.Host/WebhooksManagementHttpApiHostModule.Configure.cs

13
aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/IDynamicWebhookDefinitionStore.cs

@ -0,0 +1,13 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace LINGYUN.Abp.Webhooks;
public interface IDynamicWebhookDefinitionStore
{
Task<WebhookDefinition> GetOrNullAsync(string name);
Task<IReadOnlyList<WebhookDefinition>> GetWebhooksAsync();
Task<IReadOnlyList<WebhookGroupDefinition>> GetGroupsAsync();
}

13
aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/IStaticWebhookDefinitionStore.cs

@ -0,0 +1,13 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace LINGYUN.Abp.Webhooks;
public interface IStaticWebhookDefinitionStore
{
Task<WebhookDefinition> GetOrNullAsync(string name);
Task<IReadOnlyList<WebhookDefinition>> GetWebhooksAsync();
Task<IReadOnlyList<WebhookGroupDefinition>> GetGroupsAsync();
}

12
aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/IWebhookDefinitionManager.cs

@ -1,4 +1,5 @@
using System;
using JetBrains.Annotations;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
@ -10,24 +11,25 @@ namespace LINGYUN.Abp.Webhooks
/// Gets a webhook definition by name.
/// Returns null if there is no webhook definition with given name.
/// </summary>
WebhookDefinition GetOrNull(string name);
[NotNull]
Task<WebhookDefinition> GetOrNullAsync(string name);
/// <summary>
/// Gets a webhook definition by name.
/// Throws exception if there is no webhook definition with given name.
/// </summary>
WebhookDefinition Get(string name);
Task<WebhookDefinition> GetAsync(string name);
/// <summary>
/// Gets all webhook definitions.
/// </summary>
IReadOnlyList<WebhookDefinition> GetAll();
Task<IReadOnlyList<WebhookDefinition>> GetWebhooksAsync();
/// <summary>
/// Gets all webhook group definitions.
/// </summary>
/// <returns></returns>
IReadOnlyList<WebhookGroupDefinition> GetGroups();
Task<IReadOnlyList<WebhookGroupDefinition>> GetGroupsAsync();
/// <summary>
/// Checks if given webhook name is available for given tenant.

34
aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/NullDynamicWebhookDefinitionStore.cs

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
namespace LINGYUN.Abp.Webhooks;
[Dependency(TryRegister = true)]
public class NullDynamicWebhookDefinitionStore : IDynamicWebhookDefinitionStore, ISingletonDependency
{
private readonly static Task<WebhookDefinition> CachedWebhookResult = Task.FromResult((WebhookDefinition)null);
private readonly static Task<IReadOnlyList<WebhookDefinition>> CachedWebhooksResult =
Task.FromResult((IReadOnlyList<WebhookDefinition>)Array.Empty<WebhookDefinition>().ToImmutableList());
private readonly static Task<IReadOnlyList<WebhookGroupDefinition>> CachedGroupsResult =
Task.FromResult((IReadOnlyList<WebhookGroupDefinition>)Array.Empty<WebhookGroupDefinition>().ToImmutableList());
public Task<WebhookDefinition> GetOrNullAsync(string name)
{
return CachedWebhookResult;
}
public Task<IReadOnlyList<WebhookDefinition>> GetWebhooksAsync()
{
return CachedWebhooksResult;
}
public Task<IReadOnlyList<WebhookGroupDefinition>> GetGroupsAsync()
{
return CachedGroupsResult;
}
}

101
aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/StaticWebhookDefinitionStore.cs

@ -0,0 +1,101 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.DependencyInjection;
namespace LINGYUN.Abp.Webhooks;
public class StaticWebhookDefinitionStore : IStaticWebhookDefinitionStore, ISingletonDependency
{
protected IDictionary<string, WebhookGroupDefinition> WebhookGroupDefinitions => _lazyWebhookGroupDefinitions.Value;
private readonly Lazy<Dictionary<string, WebhookGroupDefinition>> _lazyWebhookGroupDefinitions;
protected IDictionary<string, WebhookDefinition> WebhookDefinitions => _lazyWebhookDefinitions.Value;
private readonly Lazy<Dictionary<string, WebhookDefinition>> _lazyWebhookDefinitions;
protected AbpWebhooksOptions Options { get; }
private readonly IServiceScopeFactory _serviceScopeFactory;
public StaticWebhookDefinitionStore(
IServiceScopeFactory serviceScopeFactory,
IOptions<AbpWebhooksOptions> options)
{
_serviceScopeFactory = serviceScopeFactory;
Options = options.Value;
_lazyWebhookDefinitions = new Lazy<Dictionary<string, WebhookDefinition>>(
CreateWebhookDefinitions,
isThreadSafe: true
);
_lazyWebhookGroupDefinitions = new Lazy<Dictionary<string, WebhookGroupDefinition>>(
CreateWebhookGroupDefinitions,
isThreadSafe: true
);
}
protected virtual Dictionary<string, WebhookDefinition> CreateWebhookDefinitions()
{
var Webhooks = new Dictionary<string, WebhookDefinition>();
foreach (var groupDefinition in WebhookGroupDefinitions.Values)
{
foreach (var Webhook in groupDefinition.Webhooks)
{
if (Webhooks.ContainsKey(Webhook.Name))
{
throw new AbpException("Duplicate webhook name: " + Webhook.Name);
}
Webhooks[Webhook.Name] = Webhook;
}
}
return Webhooks;
}
protected virtual Dictionary<string, WebhookGroupDefinition> CreateWebhookGroupDefinitions()
{
var definitions = new Dictionary<string, WebhookGroupDefinition>();
using (var scope = _serviceScopeFactory.CreateScope())
{
var providers = Options
.DefinitionProviders
.Select(p => scope.ServiceProvider.GetRequiredService(p) as WebhookDefinitionProvider)
.ToList();
foreach (var provider in providers)
{
provider.Define(new WebhookDefinitionContext(definitions));
}
}
return definitions;
}
public Task<WebhookDefinition> GetOrNullAsync(string name)
{
return Task.FromResult(WebhookDefinitions.GetOrDefault(name));
}
public virtual Task<IReadOnlyList<WebhookDefinition>> GetWebhooksAsync()
{
return Task.FromResult<IReadOnlyList<WebhookDefinition>>(
WebhookDefinitions.Values.ToImmutableList()
);
}
public Task<IReadOnlyList<WebhookGroupDefinition>> GetGroupsAsync()
{
return Task.FromResult<IReadOnlyList<WebhookGroupDefinition>>(
WebhookGroupDefinitions.Values.ToImmutableList()
);
}
}

106
aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/WebhookDefinitionManager.cs

@ -1,5 +1,4 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
@ -14,54 +13,65 @@ namespace LINGYUN.Abp.Webhooks
{
internal class WebhookDefinitionManager : IWebhookDefinitionManager, ISingletonDependency
{
protected IDictionary<string, WebhookGroupDefinition> WebhookGroupDefinitions => _lazyWebhookGroupDefinitions.Value;
private readonly Lazy<Dictionary<string, WebhookGroupDefinition>> _lazyWebhookGroupDefinitions;
protected IDictionary<string, WebhookDefinition> WebhookDefinitions => _lazyWebhookDefinitions.Value;
private readonly Lazy<Dictionary<string, WebhookDefinition>> _lazyWebhookDefinitions;
private readonly IServiceProvider _serviceProvider;
private readonly AbpWebhooksOptions _options;
private readonly IStaticWebhookDefinitionStore _staticStore;
private readonly IDynamicWebhookDefinitionStore _dynamicStore;
public WebhookDefinitionManager(
IServiceProvider serviceProvider,
IOptions<AbpWebhooksOptions> options)
IStaticWebhookDefinitionStore staticStore,
IDynamicWebhookDefinitionStore dynamicStore)
{
_serviceProvider = serviceProvider;
_options = options.Value;
_lazyWebhookGroupDefinitions = new Lazy<Dictionary<string, WebhookGroupDefinition>>(CreateWebhookGroupDefinitions);
_lazyWebhookDefinitions = new Lazy<Dictionary<string, WebhookDefinition>>(CreateWebhookDefinitions);
_staticStore = staticStore;
_dynamicStore = dynamicStore;
}
public WebhookDefinition GetOrNull(string name)
public async virtual Task<WebhookDefinition> GetOrNullAsync(string name)
{
if (!WebhookDefinitions.ContainsKey(name))
{
return null;
}
Check.NotNull(name, nameof(name));
return WebhookDefinitions[name];
return await _staticStore.GetOrNullAsync(name) ??
await _dynamicStore.GetOrNullAsync(name);
}
public WebhookDefinition Get(string name)
public async virtual Task<WebhookDefinition> GetAsync(string name)
{
if (!WebhookDefinitions.ContainsKey(name))
var webhook = await GetOrNullAsync(name);
if (webhook == null)
{
throw new KeyNotFoundException($"Webhook definitions does not contain a definition with the key \"{name}\".");
throw new AbpException("Undefined webhook: " + name);
}
return WebhookDefinitions[name];
return webhook;
}
public IReadOnlyList<WebhookDefinition> GetAll()
public async virtual Task<IReadOnlyList<WebhookDefinition>> GetWebhooksAsync()
{
return WebhookDefinitions.Values.ToImmutableList();
var staticWebhooks = await _staticStore.GetWebhooksAsync();
var staticWebhookNames = staticWebhooks
.Select(p => p.Name)
.ToImmutableHashSet();
var dynamicWebhooks = await _dynamicStore.GetWebhooksAsync();
return staticWebhooks
.Concat(dynamicWebhooks.Where(d => !staticWebhookNames.Contains(d.Name)))
.ToImmutableList();
}
public IReadOnlyList<WebhookGroupDefinition> GetGroups()
public async virtual Task<IReadOnlyList<WebhookGroupDefinition>> GetGroupsAsync()
{
return WebhookGroupDefinitions.Values.ToImmutableList();
var staticGroups = await _staticStore.GetGroupsAsync();
var staticGroupNames = staticGroups
.Select(p => p.Name)
.ToImmutableHashSet();
var dynamicGroups = await _dynamicStore.GetGroupsAsync();
return staticGroups
.Concat(dynamicGroups.Where(d => !staticGroupNames.Contains(d.Name)))
.ToImmutableList();
}
public async Task<bool> IsAvailableAsync(Guid? tenantId, string name)
@ -71,7 +81,7 @@ namespace LINGYUN.Abp.Webhooks
return true;
}
var webhookDefinition = GetOrNull(name);
var webhookDefinition = await GetOrNullAsync(name);
if (webhookDefinition == null)
{
@ -95,45 +105,5 @@ namespace LINGYUN.Abp.Webhooks
return true;
}
protected virtual Dictionary<string, WebhookDefinition> CreateWebhookDefinitions()
{
var definitions = new Dictionary<string, WebhookDefinition>();
foreach (var groupDefinition in WebhookGroupDefinitions.Values)
{
foreach (var webhook in groupDefinition.Webhooks)
{
if (definitions.ContainsKey(webhook.Name))
{
throw new AbpException("Duplicate webhook name: " + webhook.Name);
}
definitions[webhook.Name] = webhook;
}
}
return definitions;
}
protected virtual Dictionary<string, WebhookGroupDefinition> CreateWebhookGroupDefinitions()
{
var definitions = new Dictionary<string, WebhookGroupDefinition>();
using (var scope = _serviceProvider.CreateScope())
{
var providers = _options
.DefinitionProviders
.Select(p => scope.ServiceProvider.GetRequiredService(p) as WebhookDefinitionProvider)
.ToList();
foreach (var provider in providers)
{
provider.Define(new WebhookDefinitionContext(definitions));
}
}
return definitions;
}
}
}

3
aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/Authorization/WebhooksManagementPermissionDefinitionProvider.cs

@ -11,8 +11,7 @@ public class WebhooksManagementPermissionDefinitionProvider : PermissionDefiniti
{
var group = context.AddGroup(
WebhooksManagementPermissions.GroupName,
L("Permission:WebhooksManagement"),
MultiTenancySides.Host);
L("Permission:WebhooksManagement"));
var subscription = group.AddPermission(
WebhooksManagementPermissions.WebhookSubscription.Default,

2
aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionAppService.cs

@ -109,7 +109,7 @@ public class WebhookSubscriptionAppService : WebhooksManagementAppServiceBase, I
public async virtual Task<ListResultDto<WebhookAvailableGroupDto>> GetAllAvailableWebhooksAsync()
{
var groups = WebhookDefinitionManager.GetGroups();
var groups = await WebhookDefinitionManager.GetGroupsAsync();
var definitions = new List<WebhookAvailableGroupDto>();
foreach (var groupDefinition in groups)

3
aspnet-core/services/LY.MicroService.WebhooksManagement.HttpApi.Host/WebhooksManagementHttpApiHostModule.Configure.cs

@ -1,6 +1,5 @@
using DotNetCore.CAP;
using LINGYUN.Abp.BackgroundTasks;
using LINGYUN.Abp.Dapr.Client.DynamicProxying;
using LINGYUN.Abp.ExceptionHandling;
using LINGYUN.Abp.ExceptionHandling.Emailing;
using LINGYUN.Abp.Serilog.Enrichers.Application;
@ -302,8 +301,6 @@ public partial class WebhooksManagementHttpApiHostModule
{
options.Languages.Add(new LanguageInfo("en", "en", "English"));
options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文"));
// 动态语言支持
options.Resources.AddDynamic();
});
}

Loading…
Cancel
Save