How to set up the Ezoic API for CDN Management

This example uses the Ezoic API to automatically purge pages from the ezoic CDN whenever your post or page is updated using c# code

You will need access the API Gateway in the settings area on your Ezoic dashboard, under the "API" tab, then copy your API key. If you do not see your API key you should enable Ezoic API.

Opening your Visual Studio. Next, create a console project, then right click on your project and select Nuget Packages

We will install RestSharp and Newtonsoft.Json libaries

After completing the library installation, open the Program.cs class

static string _apiKey = "your_api_key";

Creating a simple Ping method to call to verify that the server is responding appropriately.

////c# ezoic api example
static bool Ping()
{
    var client = new RestClient("https://api-gateway.ezoic.com/gateway/cdnservices/");
    var request = new RestRequest($"ping?developerKey={_apiKey}", Method.Post);
    request.AddHeader("Content-Type", "application/json");
    var response = client.Execute(request);
    if (response.ResponseStatus == ResponseStatus.Completed)
    {
        var result = JsonConvert.DeserializeObject<EzoicResult>(response.Content);
        return result.Success;
    }
    return false;
}

Creating a Clear method to help you clears the cache for a single url in all regions.

//c# ezoic api example
static bool ClearCache(string url)
{
    string jsonBody = JsonConvert.SerializeObject(new { url });
    var client = new RestClient("https://api-gateway.ezoic.com/gateway/cdnservices/");
    var request = new RestRequest($"clearcache?developerKey={_apiKey}", Method.Post);
    request.AddHeader("Content-Type", "application/json");
    request.AddJsonBody(jsonBody);
    var response = client.Execute(request);
    if (response.ResponseStatus == ResponseStatus.Completed)
    {
        var result = JsonConvert.DeserializeObject<EzoicResult>(response.Content);
        return result.Success;
    }
    return false;
}

Creating a BulkClearCache method to help you clears the cache based on an array of URLs, clears in batches of 100 URLs

//c# ezoic api example
static bool BulkClearCache(List<string> urls)
{
    string jsonBody = JsonConvert.SerializeObject(new { urls });
    var client = new RestClient("https://api-gateway.ezoic.com/gateway/cdnservices/");
    var request = new RestRequest($"bulkclearcache?developerKey={_apiKey}", Method.Post);
    request.AddHeader("Content-Type", "application/json");
    request.AddJsonBody(jsonBody);
    var response = client.Execute(request);
    if (response.ResponseStatus == ResponseStatus.Completed)
    {
        var result = JsonConvert.DeserializeObject<EzoicResult>(response.Content);
        return result.Success;
    }
    return false;
}

Creating a PurgeCache method to help you Purges entire CDN Cache for a domain

//c# ezoic api example
static bool PurgeCache(string domain)
{
    string jsonBody = JsonConvert.SerializeObject(new { domain });
    var client = new RestClient("https://api-gateway.ezoic.com/gateway/cdnservices/");
    var request = new RestRequest($"purgecache?developerKey={_apiKey}", Method.Post);
    request.AddHeader("Content-Type", "application/json");
    request.AddJsonBody(jsonBody);
    var response = client.Execute(request);
    if (response.ResponseStatus == ResponseStatus.Completed)
    {
        var result = JsonConvert.DeserializeObject<EzoicResult>(response.Content);
        return result.Success;
    }
    return false;
}

And don't forget to create a EzoicResult class to help you get result return when calling the ezoic api.

public class EzoicResult
{
    [JsonProperty("Success")]
    public bool Success { get; set; }

    [JsonProperty("Error")]
    public string Error { get; set; }
}

This example show you how to connect your asp.net core website with Ezoic API

Related