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>();
}
}
}