// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.ComponentModel.DataAnnotations;
using Squidex.Domain.Apps.Core.HandleRules;
using Squidex.Domain.Apps.Core.Rules.Deprecated;
using Squidex.Domain.Apps.Core.Rules.EnrichedEvents;
using Squidex.Flows;
using Squidex.Flows.Steps.Utils;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Reflection;
using Squidex.Infrastructure.Validation;
namespace Squidex.Extensions.Actions.Fastly;
[FlowStep(
Title = "Fastly",
IconImage = "",
IconColor = "#e23335",
Display = "Purge fastly cache",
Description = "Remove entries from the fastly CDN cache.",
ReadMore = "https://www.fastly.com/")]
#pragma warning disable CS0618 // Type or member is obsolete
public sealed record FastlyFlowStep : FlowStep, IConvertibleToAction
#pragma warning restore CS0618 // Type or member is obsolete
{
[LocalizedRequired]
[Display(Name = "Api Key", Description = "The API key to grant access to Squidex.")]
[Editor(FlowStepEditor.Text)]
public string ApiKey { get; set; }
[LocalizedRequired]
[Display(Name = "Service Id", Description = "The ID of the fastly service.")]
[Editor(FlowStepEditor.Text)]
public string ServiceId { get; set; }
public override async ValueTask ExecuteAsync(FlowExecutionContext executionContext,
CancellationToken ct)
{
var @event = ((FlowEventContext)executionContext.Context).Event;
var id = string.Empty;
if (@event is IEnrichedEntityEvent entityEvent)
{
id = DomainId.Combine(@event.AppId.Id, entityEvent.Id).ToString();
}
var httpClient =
executionContext.Resolve()
.CreateClient("FastlyAction");
var requestUrl = $"/service/{ServiceId}/purge/{id}";
var request = new HttpRequestMessage(HttpMethod.Post, requestUrl);
if (executionContext.IsSimulation)
{
executionContext.LogSkipSimulation(
HttpDumpFormatter.BuildDump(request, null, null));
return Next();
}
request.Headers.Add("Fastly-Key", ApiKey);
var (_, dump) = await httpClient.SendAsync(executionContext, request, ct: ct);
executionContext.Log("Cache invalidated", dump);
return Next();
}
#pragma warning disable CS0618 // Type or member is obsolete
public RuleAction ToAction()
{
return SimpleMapper.Map(this, new FastlyAction());
}
#pragma warning restore CS0618 // Type or member is obsolete
}