Endpoint
Endpoint
Endpoint is a static class in the Contentstack.Core.Endpoints namespace. It resolves the URL of a Contentstack service for a region, from the Contentstack regions registry. Use this class to
- find the delivery host for a region before you create a client
- find the URL of another Contentstack service, such as Image Delivery or Content Management, in the same region
- list every service URL for a region
ContentstackClient calls Endpoint itself to resolve the delivery host when you set a region and no Host. You call it directly only when you need a URL for your own code.
GetContentstackEndpoint
GetContentstackEndpoint resolves the URL of one Contentstack service for a region, or every service URL for that region.
| Name | Type | Description |
|---|---|---|
| region (required) | string | Region ID or alias, such as eu. |
| service (required) | string | Service key, such as contentDelivery. |
| omitHttps | bool | Returns the host without the https:// scheme. |
Returns: string or Dictionary. The URL of one service, or every service URL for the region.
Overloads
- GetContentstackEndpoint(string region, string service, bool omitHttps = false): Returns a string with the URL of one service.
- GetContentstackEndpoint(string region, bool omitHttps = false): Returns a Dictionary<string, string> that maps each service key to its URL.
Validation
- Throws ArgumentException if region is null, empty, or only spaces, because the SDK has no region to look up.
- Throws KeyNotFoundException if region matches no registry ID or alias, or if service names a key the region does not have.
- Throws InvalidOperationException if the SDK can neither read nor download regions.json, or the file is not valid registry JSON.
- Throws ArgumentNullException if service is null, because the registry lookup needs a key name.
Behavior
- GetContentstackEndpoint reads the in-memory registry. Only the first call in a process reads regions.json from disk or downloads it. See the Endpoint class page.
- Region matching ignores case and surrounding spaces. "US", "na", and " aws-na " all resolve to the na region.
- Service keys match case exactly. "ContentDelivery" throws KeyNotFoundException.
- omitHttps: true returns the bare host, such as eu-cdn.contentstack.com. Pass that form to the host argument of a ContentstackClient constructor, which adds the scheme itself.
- The dictionary overload returns a new dictionary on each call, so changes to it do not reach the registry.
Example
Basic usage: resolve the delivery URL for a region
using System;
using System.Collections.Generic;
using Contentstack.Core.Endpoints;
try
{
string url = Endpoint.GetContentstackEndpoint("eu", "contentDelivery");
Console.WriteLine(url);
}
catch (KeyNotFoundException e)
{
// Thrown when the region or the service key is not in the registry
Console.WriteLine(e.Message);
}All parameters: a bare host for the client constructor
using System;
using System.Collections.Generic;
using Contentstack.Core;
using Contentstack.Core.Endpoints;
try
{
string host = Endpoint.GetContentstackEndpoint("azure-na", "contentDelivery", omitHttps: true);
ContentstackClient client = new ContentstackClient("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", host: host);
Console.WriteLine($"Client host: {host}");
}
catch (KeyNotFoundException e)
{
Console.WriteLine(e.Message);
}Dictionary overload: list every service for a region
using System;
using System.Collections.Generic;
using Contentstack.Core.Endpoints;
try
{
Dictionary<string, string> endpoints = Endpoint.GetContentstackEndpoint("gcp-eu", omitHttps: true);
foreach (KeyValuePair<string, string> endpoint in endpoints)
{
Console.WriteLine($"{endpoint.Key}: {endpoint.Value}");
}
}
catch (KeyNotFoundException e)
{
Console.WriteLine(e.Message);
}ResetCache
ResetCache clears the in-memory copy of the region registry, so that the next lookup reads regions.json again.
Returns: void. This method returns nothing.
Validation
- There is no client-side validation, because ResetCache takes no argument. It never throws.
Behavior
- Client-side only. ResetCache clears the cached registry and makes no request.
- The next call to the GetContentstackEndpoint method reads regions.json from disk, or downloads it when the file is missing.
- ResetCache does not delete the regions.json file on disk. To pick up a newer registry in a running process, replace the file first, then call ResetCache.
- Every ContentstackClient that resolves its host from a region reads the same cache on each request. After ResetCache, the next request from such a client reloads the registry.
Example
Basic usage: reload the registry after you replace regions.json
using System;
using Contentstack.Core.Endpoints;
// Call after you replace Assets/regions.json next to Contentstack.Core.dll.
Endpoint.ResetCache();
string url = Endpoint.GetContentstackEndpoint("na", "contentDelivery");
Console.WriteLine(url);