using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using AMWD.Net.Api.Cloudflare.Zones.Internals.Requests; namespace AMWD.Net.Api.Cloudflare.Zones { /// /// Extends the with methods for working with zones. /// public static class ZoneCacheExtensions { /// /// Purges all cached contents for a zone. /// /// /// Removes ALL files from Cloudflare's cache. All tiers can purge everything. /// /// The . /// The zone ID. /// A cancellation token used to propagate notification that this operation should be canceled. public static Task> PurgeCachedContent(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default) { zoneId.ValidateCloudflareId(); var req = new InternalPurgeCacheRequest { PurgeEverything = true }; return client.PostAsync($"zones/{zoneId}/purge_cache", req, cancellationToken: cancellationToken); } /// /// Purges cached contents by URLs. ///
/// Granularly removes one or more files from Cloudflare's cache by specifying URLs. /// All tiers can purge by URL. ///
/// /// /// To purge files with custom cache keys, include the headers used to compute the cache key as in the example. /// If you have a device type or geo in your cache key, you will need to include the CF-Device-Type or CF-IPCountry headers. /// If you have lang in your cache key, you will need to include the Accept-Language header. /// /// /// NB: When including the Origin header, be sure to include the scheme and hostname. /// The port number can be omitted if it is the default port (80 for http, 443 for https), but must be included otherwise. /// /// /// NB: For Zones on Free/Pro/Business plan, you may purge up to 30 URLs in one API call. /// For Zones on Enterprise plan, you may purge up to 500 URLs in one API call. /// /// /// The . /// The zone ID. /// List of URLs to purge. /// A cancellation token used to propagate notification that this operation should be canceled. public static Task> PurgeCachedContentByUrl(this ICloudflareClient client, string zoneId, IReadOnlyList urls, CancellationToken cancellationToken = default) { zoneId.ValidateCloudflareId(); var req = new InternalPurgeCacheRequest(); if (urls.Any(u => u.Headers.Count > 0)) { req.UrlsWithHeaders = urls.Where(u => !string.IsNullOrWhiteSpace(u.Url)) .Select(u => new PurgeUrlWithHeaders { Url = u.Url, Headers = u.Headers .Where(kvp => !string.IsNullOrWhiteSpace(kvp.Key)) .ToDictionary(kvp => kvp.Key, kvp => kvp.Value) }).ToList(); } else { req.Urls = urls.Where(u => !string.IsNullOrWhiteSpace(u.Url)).Select(u => u.Url).ToList(); } return client.PostAsync($"zones/{zoneId}/purge_cache", req, cancellationToken: cancellationToken); } /// /// Purges cached contents by cache-tags. /// /// /// /// For more information on cache tags and purging by tags, please refer to /// purge by cache-tags documentation page. /// /// /// Cache-Tag purging has a rate limit of 30_000 purge API calls in every 24 hour period. /// You may purge up to 30 tags in one API call. /// /// /// This rate limit can be raised for customers who need to purge at higher volume. /// /// /// The . /// The zone ID. /// List of tags to purge. /// A cancellation token used to propagate notification that this operation should be canceled. public static Task> PurgeCachedContentByTag(this ICloudflareClient client, string zoneId, IReadOnlyList tags, CancellationToken cancellationToken = default) { zoneId.ValidateCloudflareId(); if (tags == null) throw new ArgumentNullException(nameof(tags)); var req = new InternalPurgeCacheRequest { Tags = tags.Where(t => !string.IsNullOrWhiteSpace(t)).ToList() }; return client.PostAsync($"zones/{zoneId}/purge_cache", req, cancellationToken: cancellationToken); } /// /// Purges cached contents by hosts. /// /// /// /// For more information purging by hostnames, please refer to /// purge by hostname documentation page. /// /// /// Host purging has a rate limit of 30_000 purge API calls in every 24 hour period. /// You may purge up to 30 hosts in one API call. /// /// /// This rate limit can be raised for customers who need to purge at higher volume. /// /// /// The . /// The zone ID. /// List of hostnames to purge. /// A cancellation token used to propagate notification that this operation should be canceled. public static Task> PurgeCachedContentByHost(this ICloudflareClient client, string zoneId, IReadOnlyList hosts, CancellationToken cancellationToken = default) { zoneId.ValidateCloudflareId(); if (hosts == null) throw new ArgumentNullException(nameof(hosts)); var req = new InternalPurgeCacheRequest { Hostnames = hosts.Where(h => !string.IsNullOrWhiteSpace(h)).ToList() }; return client.PostAsync($"zones/{zoneId}/purge_cache", req, cancellationToken: cancellationToken); } /// /// Purges cached contents by prefixes. /// /// /// /// For more information purging by prefixes, please refer to /// purge by prefix documentation page. /// /// /// Prefix purging has a rate limit of 30_000 purge API calls in every 24 hour period. /// You may purge up to 30 prefixes in one API call. /// /// /// This rate limit can be raised for customers who need to purge at higher volume. /// /// /// The . /// The zone ID. /// List of prefixes to purge. /// A cancellation token used to propagate notification that this operation should be canceled. public static Task> PurgeCachedContentByPrefix(this ICloudflareClient client, string zoneId, IReadOnlyList prefixes, CancellationToken cancellationToken = default) { zoneId.ValidateCloudflareId(); if (prefixes == null) throw new ArgumentNullException(nameof(prefixes)); var req = new InternalPurgeCacheRequest { Prefixes = prefixes.Where(h => !string.IsNullOrWhiteSpace(h)).ToList() }; return client.PostAsync($"zones/{zoneId}/purge_cache", req, cancellationToken: cancellationToken); } } }