using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AMWD.Net.Api.Cloudflare.Zones.Internals.Requests;
namespace AMWD.Net.Api.Cloudflare.Zones
{
///
/// Extensions for security center section of a zone.
///
public static class SecurityCenterExtensions
{
///
/// Delete security.txt
///
/// The .
/// The zone ID.
/// A cancellation token used to propagate notification that this operation should be canceled.
public static async Task DeleteSecurityTxt(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default)
{
zoneId.ValidateCloudflareId();
return await client.DeleteAsync($"zones/{zoneId}/security-center/securitytxt", cancellationToken: cancellationToken);
}
///
/// Get security.txt.
///
/// The .
/// The zone ID.
/// A cancellation token used to propagate notification that this operation should be canceled.
public static Task> GetSecurityTxt(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default)
{
zoneId.ValidateCloudflareId();
return client.GetAsync($"zones/{zoneId}/security-center/securitytxt", cancellationToken: cancellationToken);
}
///
/// Update security.txt
///
/// The .
/// The request information.
/// A cancellation token used to propagate notification that this operation should be canceled.
public static async Task UpdateSecurityTxt(this ICloudflareClient client, UpdateSecurityTxtRequest request, CancellationToken cancellationToken = default)
{
request.ZoneId.ValidateCloudflareId();
var req = new InternalUpdateSecurityTxtRequest();
if (request.Acknowledgements != null)
req.Acknowledgements = request.Acknowledgements.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
if (request.Canonical != null)
req.Canonical = request.Canonical.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
if (request.Contact != null)
req.Contact = request.Contact.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
req.Enabled = request.Enabled;
if (request.Encryption != null)
req.Encryption = request.Encryption.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
if (request.Expires.HasValue)
req.Expires = request.Expires.Value.ToUniversalTime();
if (request.Hiring != null)
req.Hiring = request.Hiring.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
if (request.Policy != null)
req.Policy = request.Policy.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
if (request.PreferredLanguages != null)
req.PreferredLanguages = string.Join(", ", request.PreferredLanguages.Where(s => !string.IsNullOrWhiteSpace(s)));
return await client.PutAsync($"zones/{request.ZoneId}/security-center/securitytxt", req, cancellationToken);
}
}
}