Class ContentfulClient
Main class for interaction with the contentful delivery and preview APIs
To use it you need to provide an HttpClient
as well as your delivery api key and space id.
These can be provided through any of the constructors below.
var httpClient = new HttpClient();
var client = new ContentfulClient(httpClient, "<delivery_api_key>", "<space_id>"); //provide your delivery api token and space id directly in the constructor.
var clientWithPreviewApi = new ContentfulClient(httpClient, "<preview_api_key>", "<space_id>", true); //If you wish to use the preview api, make sure to use your preview api key.
var options = new ContentfulOptions() {
DeliveryApiKey = "<delivery_api_key>",
ManagementApiKey = "<management_api_key>",
SpaceId = "<space_id>",
UsePreviewApi = false,
MaxNumberOfRateLimitRetries = 2
}
var clientWithOptions = new ContentfulClient(httpClient, options); //Using ContentfulOptions is the prefered approach as it allows you to specify both the management and the delivery api key as well as further options.
For ASP.NET Core applications the ContentfulClient
also supports the IOptions
pattern where you can supply the constructor with an IOptions<ContentfulOptions>
. This potentially lets
you keep your authorization token in your application settings, in environment variables or your own custom Microsoft.Extensions.Configuration.IConfigurationSource
provider.
Inherited Members
Namespace:Contentful.Core
Assembly:cs.temp.dll.dll
Syntax
public class ContentfulClient : ContentfulClientBase, IContentfulClient
Constructors
ContentfulClient(HttpClient, ContentfulOptions)
Initializes a new instance of the ContentfulClient class. The main class for interaction with the contentful deliver and preview APIs.
Declaration
public ContentfulClient(HttpClient httpClient, ContentfulOptions options)
Parameters
Type | Name | Description |
---|---|---|
HttpClient | httpClient | The HttpClient of your application. |
ContentfulOptions | options | The ContentfulOptions used for this client. |
Exceptions
Type | Condition |
---|---|
System.ArgumentException | The |
ContentfulClient(HttpClient, String, String, String, Boolean)
Initializes a new instance of the ContentfulClient class.
Declaration
public ContentfulClient(HttpClient httpClient, string deliveryApiKey, string previewApiKey, string spaceId, bool usePreviewApi = false)
Parameters
Type | Name | Description |
---|---|---|
HttpClient | httpClient | The HttpClient of your application. |
System.String | deliveryApiKey | The delivery API key used when communicating with the Contentful API. |
System.String | previewApiKey | The preview API key used when communicating with the Contentful Preview API. |
System.String | spaceId | The ID of the space to fetch content from. |
System.Boolean | usePreviewApi | Whether or not to use the Preview API for requests.
If this is set to true the preview API key needs to be used for |
Properties
ContentTypeResolver
Gets or sets the resolver used when resolving entries to typed objects.
Declaration
public IContentTypeResolver ContentTypeResolver { get; set; }
Property Value
Type | Description |
---|---|
IContentTypeResolver |
Implements
IsPreviewClient
Returns whether or not the client is using the preview API.
Note that to use the preview API you must specify a preview API key in your settings, which is different from your normal API delivery key.
Declaration
public bool IsPreviewClient { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
Implements
ResolveEntriesSelectively
If set the GetEntries methods will evaluate the class to serialize into and only serialize the parts that are part of the class structure.
Declaration
public bool ResolveEntriesSelectively { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean |
Implements
Methods
GetAsset(String, QueryBuilder<Asset>, CancellationToken)
Gets a single Asset by the specified ID.
Declaration
public Task<Asset> GetAsset(string assetId, QueryBuilder<Asset> queryBuilder, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System.String | assetId | The ID of the asset. |
QueryBuilder<Asset> | queryBuilder | The optional QueryBuilder<T> to add additional filtering to the query. |
System.Threading.CancellationToken | cancellationToken | The optional cancellation token to cancel the operation. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<Asset> | The response from the API serialized into an Asset |
Implements
Exceptions
Type | Condition |
---|---|
ContentfulException | There was an error when communicating with the Contentful API. |
System.ArgumentException | The |
GetAsset(String, String, CancellationToken)
Gets a single Asset by the specified ID.
Declaration
public Task<Asset> GetAsset(string assetId, string queryString = null, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System.String | assetId | The ID of the asset. |
System.String | queryString | The optional querystring to add additional filtering to the query. |
System.Threading.CancellationToken | cancellationToken | The optional cancellation token to cancel the operation. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<Asset> | The response from the API serialized into an Asset |
Implements
Exceptions
Type | Condition |
---|---|
ContentfulException | There was an error when communicating with the Contentful API. |
System.ArgumentException | The |
GetAssets(QueryBuilder<Asset>, CancellationToken)
Gets all assets of a space, filtered by an optional QueryBuilder<T>.
Declaration
public Task<ContentfulCollection<Asset>> GetAssets(QueryBuilder<Asset> queryBuilder, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
QueryBuilder<Asset> | queryBuilder | The optional QueryBuilder<T> to add additional filtering to the query. |
System.Threading.CancellationToken | cancellationToken | The optional cancellation token to cancel the operation. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<ContentfulCollection<Asset>> | A ContentfulCollection<T> of Asset. |
Implements
Exceptions
Type | Condition |
---|---|
ContentfulException | There was an error when communicating with the Contentful API. |
GetAssets(String, CancellationToken)
Gets all assets of a space, filtered by an optional queryString. A simpler approach than to construct a query manually is to use the QueryBuilder<T> class.
Declaration
public Task<ContentfulCollection<Asset>> GetAssets(string queryString = null, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System.String | queryString | The optional querystring to add additional filtering to the query. |
System.Threading.CancellationToken | cancellationToken | The optional cancellation token to cancel the operation. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<ContentfulCollection<Asset>> | A ContentfulCollection<T> of Asset. |
Implements
Exceptions
Type | Condition |
---|---|
ContentfulException | There was an error when communicating with the Contentful API. |
GetContentType(String, CancellationToken)
Gets a ContentType by the specified ID.
Declaration
public Task<ContentType> GetContentType(string contentTypeId, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System.String | contentTypeId | The ID of the content type. |
System.Threading.CancellationToken | cancellationToken | The optional cancellation token to cancel the operation. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<ContentType> | The response from the API serialized into a ContentType. |
Implements
Exceptions
Type | Condition |
---|---|
ContentfulException | There was an error when communicating with the Contentful API. |
System.ArgumentException | The |
GetContentTypes(CancellationToken)
Get all content types of a space.
Declaration
public Task<IEnumerable<ContentType>> GetContentTypes(CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System.Threading.CancellationToken | cancellationToken | The optional cancellation token to cancel the operation. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<ContentType>> | An System.Collections.Generic.IEnumerable<T> of ContentType. |
Implements
GetEntries<T>(QueryBuilder<T>, CancellationToken)
Gets all the entries of a space, filtered by an optional QueryBuilder<T>.
Declaration
public Task<ContentfulCollection<T>> GetEntries<T>(QueryBuilder<T> queryBuilder, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
QueryBuilder<T> | queryBuilder | The optional QueryBuilder<T> to add additional filtering to the query. |
System.Threading.CancellationToken | cancellationToken | The optional cancellation token to cancel the operation. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<ContentfulCollection<T>> | A ContentfulCollection<T> of items. |
Type Parameters
Name | Description |
---|---|
T | The class to serialize the response into. If you want the metadata to be included in the serialized response use the Entry<T> class as a type parameter. |
Implements
Exceptions
Type | Condition |
---|---|
ContentfulException | There was an error when communicating with the Contentful API. |
GetEntries<T>(String, CancellationToken)
Gets all the entries of a space, filtered by an optional querystring. A simpler approach than to construct a query manually is to use the QueryBuilder<T> class.
Declaration
public Task<ContentfulCollection<T>> GetEntries<T>(string queryString = null, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System.String | queryString | The optional querystring to add additional filtering to the query. |
System.Threading.CancellationToken | cancellationToken | The optional cancellation token to cancel the operation. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<ContentfulCollection<T>> | A ContentfulCollection<T> of items. |
Type Parameters
Name | Description |
---|---|
T | The class to serialize the response into. If you want the metadata to be included in the serialized response use the Entry<T> class as a type parameter. |
Implements
Exceptions
Type | Condition |
---|---|
ContentfulException | There was an error when communicating with the Contentful API. |
GetEntriesByType<T>(String, QueryBuilder<T>, CancellationToken)
Gets all the entries with the specified content type.
Declaration
public Task<ContentfulCollection<T>> GetEntriesByType<T>(string contentTypeId, QueryBuilder<T> queryBuilder = null, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System.String | contentTypeId | The ID of the content type to get entries for. |
QueryBuilder<T> | queryBuilder | The optional QueryBuilder<T> to add additional filtering to the query. |
System.Threading.CancellationToken | cancellationToken | The optional cancellation token to cancel the operation. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<ContentfulCollection<T>> | A ContentfulCollection<T> of items. |
Type Parameters
Name | Description |
---|---|
T | The class to serialize the response into. If you want the metadata to be included in the serialized response use the Entry<T> class as a type parameter. |
Implements
Exceptions
Type | Condition |
---|---|
ContentfulException | There was an error when communicating with the Contentful API. |
GetEntry<T>(String, QueryBuilder<T>, CancellationToken)
Get a single entry by the specified ID.
Declaration
public Task<T> GetEntry<T>(string entryId, QueryBuilder<T> queryBuilder, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System.String | entryId | The ID of the entry. |
QueryBuilder<T> | queryBuilder | The optional QueryBuilder<T> to add additional filtering to the query. |
System.Threading.CancellationToken | cancellationToken | The optional cancellation token to cancel the operation. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<T> | The response from the API serialized into |
Type Parameters
Name | Description |
---|---|
T | The type to serialize this entry into. If you want the metadata to be included in the serialized response use the Entry<T> class as a type parameter. |
Implements
Exceptions
Type | Condition |
---|---|
ContentfulException | There was an error when communicating with the Contentful API. |
System.ArgumentException | The entryId parameter was null or empty. |
GetEntry<T>(String, String, CancellationToken)
Get a single entry by the specified ID.
Declaration
public Task<T> GetEntry<T>(string entryId, string queryString = null, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System.String | entryId | The ID of the entry. |
System.String | queryString | The optional querystring to add additional filtering to the query. |
System.Threading.CancellationToken | cancellationToken | The optional cancellation token to cancel the operation. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<T> | The response from the API serialized into |
Type Parameters
Name | Description |
---|---|
T | The type to serialize this entry into. If you want the metadata to be included in the serialized response use the Entry<T> class as a type parameter. |
Implements
Exceptions
Type | Condition |
---|---|
ContentfulException | There was an error when communicating with the Contentful API. |
System.ArgumentException | The entryId parameter was null or empty. |
GetLocales(CancellationToken)
Get all locales of an environment.
Declaration
public Task<IEnumerable<Locale>> GetLocales(CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System.Threading.CancellationToken | cancellationToken | The optional cancellation token to cancel the operation. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<Locale>> | An System.Collections.Generic.IEnumerable<T> of Locale. |
Implements
GetSpace(CancellationToken)
Gets the Space for this client.
Declaration
public Task<Space> GetSpace(CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System.Threading.CancellationToken | cancellationToken | The optional cancellation token to cancel the operation. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<Space> | The Space. |
Implements
Exceptions
Type | Condition |
---|---|
ContentfulException | There was an error when communicating with the Contentful API. |
SyncInitial(SyncType, String, CancellationToken)
Fetches an initial sync result of content. Note that this sync might not contain the entire result. If the SyncResult returned contains a NextPageUrl that means there are more resources to fetch. See also the SyncInitialRecursive(SyncType, String, CancellationToken) method.
Declaration
public Task<SyncResult> SyncInitial(SyncType syncType = SyncType.All, string contentTypeId = "", CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
SyncType | syncType | The optional type of items that should be synced. |
System.String | contentTypeId | The content type ID to filter entries by. Only applicable when the syncType is Entry. |
System.Threading.CancellationToken | cancellationToken | The optional cancellation token to cancel the operation. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<SyncResult> | A SyncResult containing all synced resources. |
Implements
Exceptions
Type | Condition |
---|---|
ContentfulException | There was an error when communicating with the Contentful API. |
SyncInitialRecursive(SyncType, String, CancellationToken)
Fetches an inital sync result of content and then recursively calls the api for any further content available using the NextPageUrl. Note that this might result in multiple outgoing calls to the Contentful API. If you have a large amount of entries to sync consider using the SyncInitial(SyncType, String, CancellationToken) method in conjunction with the SyncNextResult(String, CancellationToken) method and handling each response separately.
Declaration
public Task<SyncResult> SyncInitialRecursive(SyncType syncType = SyncType.All, string contentTypeId = "", CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
SyncType | syncType | The optional type of items that should be synced. |
System.String | contentTypeId | The content type ID to filter entries by. Only applicable when the syncType is Entry. |
System.Threading.CancellationToken | cancellationToken | The optional cancellation token to cancel the operation. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<SyncResult> | A SyncResult containing all synced resources. |
Implements
Exceptions
Type | Condition |
---|---|
ContentfulException | There was an error when communicating with the Contentful API. |
SyncNextResult(String, CancellationToken)
Syncs the delta changes since the last sync or the next page of an incomplete sync.
Declaration
public Task<SyncResult> SyncNextResult(string nextSyncOrPageUrl, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System.String | nextSyncOrPageUrl | The next page or next sync url from another SyncResult, you can either pass the entire URL or only the syncToken query string parameter. |
System.Threading.CancellationToken | cancellationToken | The optional cancellation token to cancel the operation. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<SyncResult> | A SyncResult containing all synced resources. |
Implements
Exceptions
Type | Condition |
---|---|
System.ArgumentException | The |
ContentfulException | There was an error when communicating with the Contentful API. |