Api: webhooks retry
This commit is contained in:
parent
22213a25a7
commit
4870031c1a
@ -74,7 +74,7 @@ public class WebhooksGlobalFilterAttribute : ResultFilterAttribute, IDisposable
|
||||
|
||||
var resultContent = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
|
||||
await _webhookPublisher.Publish(method, routePattern, resultContent);
|
||||
await _webhookPublisher.PublishAsync(method, routePattern, resultContent);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
@ -29,5 +29,6 @@ namespace ASC.Webhooks.Core;
|
||||
[Scope]
|
||||
public interface IWebhookPublisher
|
||||
{
|
||||
public Task Publish(string method, string route, string requestPayload);
|
||||
public Task PublishAsync(string method, string route, string requestPayload);
|
||||
public Task<WebhooksLog> PublishAsync(string method, string route, string requestPayload, int configId);
|
||||
}
|
@ -40,7 +40,7 @@ public class WebhookPublisher : IWebhookPublisher
|
||||
_webhookNotify = webhookNotify;
|
||||
}
|
||||
|
||||
public async Task Publish(string method, string route, string requestPayload)
|
||||
public async Task PublishAsync(string method, string route, string requestPayload)
|
||||
{
|
||||
if (string.IsNullOrEmpty(requestPayload))
|
||||
{
|
||||
@ -51,23 +51,35 @@ public class WebhookPublisher : IWebhookPublisher
|
||||
|
||||
await foreach (var config in webhookConfigs.Where(r => r.Enabled))
|
||||
{
|
||||
var webhooksLog = new WebhooksLog
|
||||
{
|
||||
Method = method,
|
||||
Route = route,
|
||||
CreationTime = DateTime.UtcNow,
|
||||
RequestPayload = requestPayload,
|
||||
ConfigId = config.Id
|
||||
};
|
||||
|
||||
var webhook = await _dbWorker.WriteToJournal(webhooksLog);
|
||||
|
||||
var request = new WebhookRequest
|
||||
{
|
||||
Id = webhook.Id
|
||||
};
|
||||
|
||||
_webhookNotify.Publish(request, CacheNotifyAction.Update);
|
||||
_ = await PublishAsync(method, route, requestPayload, config.Id);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<WebhooksLog> PublishAsync(string method, string route, string requestPayload, int configId)
|
||||
{
|
||||
if (string.IsNullOrEmpty(requestPayload))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var webhooksLog = new WebhooksLog
|
||||
{
|
||||
Method = method,
|
||||
Route = route,
|
||||
CreationTime = DateTime.UtcNow,
|
||||
RequestPayload = requestPayload,
|
||||
ConfigId = configId
|
||||
};
|
||||
|
||||
var webhook = await _dbWorker.WriteToJournal(webhooksLog);
|
||||
|
||||
var request = new WebhookRequest
|
||||
{
|
||||
Id = webhook.Id
|
||||
};
|
||||
|
||||
_webhookNotify.Publish(request, CacheNotifyAction.Update);
|
||||
|
||||
return webhook;
|
||||
}
|
||||
}
|
||||
|
@ -32,6 +32,7 @@ public class WebhooksController : BaseSettingsController
|
||||
private readonly PermissionContext _permissionContext;
|
||||
private readonly DbWorker _webhookDbWorker;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly WebhookPublisher _webhookPublisher;
|
||||
|
||||
public WebhooksController(
|
||||
ApiContext context,
|
||||
@ -41,18 +42,18 @@ public class WebhooksController : BaseSettingsController
|
||||
IMemoryCache memoryCache,
|
||||
DbWorker dbWorker,
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
IMapper mapper)
|
||||
IMapper mapper,
|
||||
WebhookPublisher webhookPublisher)
|
||||
: base(apiContext, memoryCache, webItemManager, httpContextAccessor)
|
||||
{
|
||||
_context = context;
|
||||
_permissionContext = permissionContext;
|
||||
_webhookDbWorker = dbWorker;
|
||||
_mapper = mapper;
|
||||
_webhookPublisher = webhookPublisher;
|
||||
}
|
||||
/// <summary>
|
||||
/// Read Webhooks history for actual tenant
|
||||
/// </summary>
|
||||
[HttpGet("webhooks")]
|
||||
|
||||
[HttpGet("webhook")]
|
||||
public async IAsyncEnumerable<WebhooksConfigDto> GetTenantWebhooks()
|
||||
{
|
||||
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
|
||||
@ -63,9 +64,6 @@ public class WebhooksController : BaseSettingsController
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add new config for webhooks
|
||||
/// </summary>
|
||||
[HttpPost("webhook")]
|
||||
public async Task<WebhooksConfigDto> CreateWebhook(WebhooksConfigRequestsDto model)
|
||||
{
|
||||
@ -79,9 +77,6 @@ public class WebhooksController : BaseSettingsController
|
||||
return _mapper.Map<WebhooksConfig, WebhooksConfigDto>(webhook);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update config for webhooks
|
||||
/// </summary>
|
||||
[HttpPut("webhook")]
|
||||
public async Task<WebhooksConfigDto> UpdateWebhook(WebhooksConfigRequestsDto model)
|
||||
{
|
||||
@ -95,9 +90,6 @@ public class WebhooksController : BaseSettingsController
|
||||
return _mapper.Map<WebhooksConfig, WebhooksConfigDto>(webhook);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove config for webhooks
|
||||
/// </summary>
|
||||
[HttpDelete("webhook")]
|
||||
public async Task<WebhooksConfigDto> RemoveWebhook(int id)
|
||||
{
|
||||
@ -119,5 +111,52 @@ public class WebhooksController : BaseSettingsController
|
||||
{
|
||||
yield return _mapper.Map<WebhooksLog, WebhooksLogDto>(j);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPut("webhook/{id}/retry")]
|
||||
public async Task<WebhooksLogDto> RetryWebhook(int id)
|
||||
{
|
||||
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
|
||||
|
||||
if (id == 0)
|
||||
{
|
||||
throw new ArgumentException(nameof(id));
|
||||
}
|
||||
|
||||
var item = await _webhookDbWorker.ReadJournal(id);
|
||||
|
||||
if (item == null)
|
||||
{
|
||||
throw new ItemNotFoundException();
|
||||
}
|
||||
|
||||
if (item.Status >= 200 && item.Status <= 299 || item.Status == 0)
|
||||
{
|
||||
throw new HttpException(HttpStatusCode.Forbidden);
|
||||
}
|
||||
|
||||
var result = await _webhookPublisher.PublishAsync(item.Method, item.Route, item.RequestPayload, item.ConfigId);
|
||||
|
||||
return _mapper.Map<WebhooksLog, WebhooksLogDto>(result);
|
||||
}
|
||||
|
||||
[HttpPut("webhook/retry")]
|
||||
public async IAsyncEnumerable<WebhooksLogDto> RetryWebhooks(WebhookRetryRequestsDto model)
|
||||
{
|
||||
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
|
||||
|
||||
foreach (var id in model.Ids)
|
||||
{
|
||||
var item = await _webhookDbWorker.ReadJournal(id);
|
||||
|
||||
if (item == null || item.Status >= 200 && item.Status <= 299 || item.Status == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var result = await _webhookPublisher.PublishAsync(item.Method, item.Route, item.RequestPayload, item.ConfigId);
|
||||
|
||||
yield return _mapper.Map<WebhooksLog, WebhooksLogDto>(result);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
// (c) Copyright Ascensio System SIA 2010-2022
|
||||
//
|
||||
// This program is a free software product.
|
||||
// You can redistribute it and/or modify it under the terms
|
||||
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
|
||||
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
|
||||
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
|
||||
// any third-party rights.
|
||||
//
|
||||
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
|
||||
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
|
||||
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
||||
//
|
||||
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
|
||||
//
|
||||
// The interactive user interfaces in modified source and object code versions of the Program must
|
||||
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
|
||||
//
|
||||
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
|
||||
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
|
||||
// trademark law for use of our trademarks.
|
||||
//
|
||||
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
|
||||
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
|
||||
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
||||
|
||||
namespace ASC.Web.Api.ApiModels.RequestsDto;
|
||||
|
||||
public class WebhookRetryRequestsDto
|
||||
{
|
||||
public List<int> Ids { get; set; }
|
||||
}
|
@ -62,6 +62,7 @@ global using ASC.Common.Radicale.Core;
|
||||
global using ASC.Common.Security.Authorizing;
|
||||
global using ASC.Common.Threading;
|
||||
global using ASC.Common.Utils;
|
||||
global using ASC.Common.Web;
|
||||
global using ASC.Core;
|
||||
global using ASC.Core.Billing;
|
||||
global using ASC.Core.Common.Configuration;
|
||||
|
Loading…
Reference in New Issue
Block a user