UmbracoFlare.ApiControllers.CloudflareApiController.ListZones C# (CSharp) Method

ListZones() public method

Call the cloudflare api to get a list of zones associated with this api key / account email combo. If you pass in a name (domain name), it will return that zone.
public ListZones ( string name = null, bool throwExceptionOnFail = false ) : List
name string The domain name of the zone that you wish to get the info about. If you want all of them, leave it blank.
throwExceptionOnFail bool
return List
        public List<Zone> ListZones(string name = null, bool throwExceptionOnFail = false)
        {
            try
            {
                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    string url = CLOUDFLARE_API_BASE_URL + "zones";

                    if(!String.IsNullOrEmpty(name))
                    {
                        url += "?name="+ HttpUtility.UrlEncode(name);
                    }

                    HttpRequestMessage request = new HttpRequestMessage()
                    {
                        RequestUri = new Uri(url),
                        Method = HttpMethod.Get,
                    };

                    //Add in the ApiKey and AccountEmail
                    AddRequestHeaders(request);

                    var responseContent = client.SendAsync(request).Result.Content;

                    ListZonesResponse response = responseContent.ReadAsAsync<ListZonesResponse>().Result;

                    if (!response.Success)
                    {
                        //Something went wrong log the response
                        Log.Error(String.Format("Could not get the list of zones for name {0} because of {1}", name, response.Messages.ToString()));
                        return new List<Zone>();
                    }

                    //Return the zones from the response.
                    return response.Zones;
                }
            }
            catch(Exception e)
            {
                Log.Error(String.Format("Could not get the List of zones for name {0}", name), e);
                if(throwExceptionOnFail)
                {
                    throw e;
                }
                else
                {
                    //We didn't want to throw an exception so just return an empty list
                    return new List<Zone>();
                }
            }
        }