UmbracoFlare.Manager.CloudflareManager.PurgePages C# (CSharp) Method

PurgePages() public method

public PurgePages ( IEnumerable urls ) : List
urls IEnumerable
return List
        public List<StatusWithMessage> PurgePages(IEnumerable<string> urls)
        {
            //If the setting is turned off, then don't do anything.
            if (!CloudflareConfiguration.Instance.PurgeCacheOn) return new List<StatusWithMessage>(){new StatusWithMessage(false, CloudflareMessages.CLOULDFLARE_DISABLED)};

            urls = UmbracoFlareDomainManager.Instance.FilterToAllowedDomains(urls);

            //Separate all of these into individual groups where the domain is the same that way we save some cloudflare requests.
            IEnumerable<IGrouping<string, string>> groupings = urls.GroupBy(url => UrlHelper.GetDomainFromUrl(url,true));

            List<StatusWithMessage> results = new List<StatusWithMessage>();

            //Now loop through each group.
            foreach (IGrouping<string, string> domainUrlGroup in groupings)
            {

                //get the domain without the scheme or port.
                Uri domain = new UriBuilder(domainUrlGroup.Key).Uri;

                //Get the zone for the current website as configured by the "zoneUrl" config setting in the web.config.
                Zone websiteZone = GetZone(domain.DnsSafeHost);

                if (websiteZone == null)
                {
                    //this will already be logged in the GetZone method so just relay that it was bad.
                    results.Add(new StatusWithMessage(false, String.Format("Could not retrieve the zone from cloudflare with the domain(url) of {0}", domain)));
                    continue; //to the next domain group.
                }

                //Make the request to the api using the urls from this domain group.
                bool apiResult = this._api.PurgeCache(websiteZone.Id, domainUrlGroup);

                if (!apiResult)
                {
                    results.Add(new StatusWithMessage(false, CloudflareMessages.CLOUDFLARE_API_ERROR));
                }
                else
                {
                    foreach (string url in domainUrlGroup)
                    {
                        //We need to  add x number of statuswithmessages that are true where x is the number urls
                        results.Add(new StatusWithMessage(true, String.Format("Purged for url {0}", url)));
                    }
                }
            }
            //return the results of all of the api calls.
            return results;
        }