Example #1
0
        /// <summary>
        /// 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.
        /// </summary>
        /// <param name="name">The domain name of the zone that you wish to get the info about. If you want all of them, leave it blank.</param>
        /// <returns>A list of zones.</returns>
        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>());
                }
            }
        }
Example #2
0
        public List <Dns.Result> GetAllDnsRecordsByZone()
        {
            var allDnsEntries       = new List <Dns.Result>();
            ListZonesResponse zones = ListZones();

            foreach (var zone in zones.result)
            {
                var dnsRecords = ListDnsRecords(zone.id);
                allDnsEntries.AddRange(dnsRecords.result);
            }

            return(allDnsEntries);
        }