private HttpRequestMessage GetInvalidationMessage(CloudflareInvalidationRequest payload)
        {
            var json = JsonSerializer.Serialize(payload, JsonOptions);
            var body = new StringContent(json, Encoding.UTF8, "application/json");

            return(new HttpRequestMessage(HttpMethod.Post, $"https://api.cloudflare.com/client/v4/zones/{_config.ZoneID}/purge_cache")
            {
                Content = body
            });
        }
        public async Task InvalidateBranches(IEnumerable <string> branches)
        {
            if (!_enabled)
            {
                return;
            }

            var page  = 0;
            var paged = GetPage(branches, page);

            while (paged.Any())
            {
                _logger.LogTrace($"Invalidating page {page} with {paged.Count()} ids");
                var files = paged
                            .SelectMany(x => new[]
                {
                    $"{_config.BaseUrl}/update/{x}",
                    $"{_config.BaseUrl}/update/{x}/changes",
                    $"{_config.BaseUrl}/update/{x}/updatefile"
                })
                            .ToList();

                var payload = new CloudflareInvalidationRequest
                {
                    Files = files
                };

                _logger.LogTrace($"Sending to cloudflare:\n{JsonSerializer.Serialize(payload, JsonOptions)}");

                var message = GetInvalidationMessage(payload);
                var retries = 2;

                while (retries > 0)
                {
                    try
                    {
                        // Can't send same message twice
                        message = GetInvalidationMessage(payload);
                        var result = await _httpClient.SendAsync(message).ConfigureAwait(false);

                        var content = await result.Content.ReadAsStringAsync();

                        var resource = JsonSerializer.Deserialize <CloudflareResponse>(content, JsonOptions);

                        if (resource.Success)
                        {
                            break;
                        }
                    }
                    catch (Exception e)
                    {
                        _logger.LogError(e, "Invalidation failed");
                    }

                    _logger.LogTrace($"Invalidation failed, retrying");

                    retries--;
                }

                paged = GetPage(branches, ++page);
            }
        }