Client
open class Client
Client object for performing requests against the Contentful Delivery and Preview APIs.
-
The configuration for this instance of the client.
Declaration
Swift
public let clientConfiguration: ClientConfiguration -
The identifier of the space this Client is set to interface with.
Declaration
Swift
public let spaceId: String -
The identifier of the environment within the space that this Client is set to interface with.
Declaration
Swift
public let environmentId: String -
Available Locales for this environment
Declaration
Swift
public var locales: [Contentful.Locale]? -
Context for holding information about the fallback chain of locales for the Space.
Declaration
Swift
public private(set) var localizationContext: LocalizationContext! { get set } -
The base domain that all URIs have for each request the client makes.
Declaration
Swift
public let host: String -
Undocumented
Declaration
Swift
public var jsonDecoder: JSONDecoder { get } -
The persistence integration which will receive delegate messages from the
Clientwhen newEntryandAssetobjects are created from data being sent over the network. Currently, these messages are only sent during the response hadling forclient.synccalls. See a CoreData persistence integration at https://github.com/contentful/contentful-persistence.swift.Declaration
Swift
public var persistenceIntegration: PersistenceIntegration? { get set } -
init(spaceId:environmentId: accessToken: host: clientConfiguration: sessionConfiguration: persistenceIntegration: contentTypeClasses: ) Initializes a new Contentful client instance
Declaration
Swift
public init(spaceId: String, environmentId: String = "master", accessToken: String, host: String = Host.delivery, clientConfiguration: ClientConfiguration = .default, sessionConfiguration: URLSessionConfiguration = .default, persistenceIntegration: PersistenceIntegration? = nil, contentTypeClasses: [EntryDecodable.Type]? = nil)Parameters
spaceIdThe identifier of the space to perform requests against.
environmentIdThe identifier of the space environment to perform requests against. Defaults to “master”.
accessTokenThe access token used for authorization.
hostThe domain host to perform requests against. Defaults to
Host.deliveryi.e."cdn.contentful.com".clientConfigurationCustom Configuration of the Client. Uses
ClientConfiguration.defaultif omitted.sessionConfigurationThe configuration for the URLSession. Note that HTTP headers will be overwritten internally by the SDK so that requests can be authorized correctly.
persistenceIntegrationAn object conforming to the
PersistenceIntegrationprotocol which will receive messages about created/deleted Resources when callingsyncmethods.contentTypeClassesAn array of
EntryDecodableclasses to map Contentful entries to when using the relevant fetch methods. -
Returns an optional URL for the specified endpoint with its query paramaters.
Declaration
Swift
public func url(endpoint: Endpoint, parameters: [String : String]? = nil) -> URLParameters
endpointThe delivery/preview API endpoint.
parametersA dictionary of query parameters which be appended at the end of the URL in a URL safe format.
Return Value
A valid URL for the Content Delivery or Preview API, or nil if the URL could not be constructed.
-
Fetches the raw
Dataobjects and bypass the JSON parsing provided by the SDK.Declaration
Swift
@discardableResult func fetch( url: URL, then completion: @escaping ResultsHandler<Data> ) -> URLSessionDataTaskParameters
urlThe URL representing the endpoint with query parameters.
completionThe completion handler to call when the request is complete.
-
Fetches the JSON data at the specified URL and decoding it.
Declaration
Swift
@discardableResult func fetch<DecodableType: Decodable>( url: URL, then completion: @escaping ResultsHandler<DecodableType> ) -> URLSessionDataTaskParameters
urlThe URL representing the endpoint with query parameters.
completionThe completion handler wrapping
DecodableTypeto call when the request is complete. -
Fetches a resource by id.
Available resource types that match this function’s constraints are:
Space,Asset,ContentType,Entry, or any of custom types conforming toEntryDecodableorAssetDecodable.Declaration
Swift
@discardableResult func fetch<ResourceType>( _ resourceType: ResourceType.Type, id: String, include includesLevel: UInt? = nil, then completion: @escaping ResultsHandler<ResourceType> ) -> URLSessionDataTask where ResourceType: Decodable & EndpointAccessibleParameters
resourceTypeA reference to the Swift type which conforms to
Decodable & EndpointAccessible.idThe identifier of the resource.
includeThe level of includes to be resolved. Default value when nil. See more: Retrieval of linked items.
completionThe completion handler to call when the request is complete.
-
Fetches collections of
ContentType,Entry, andAssettypes.Declaration
Swift
@discardableResult func fetchArray<ResourceType, QueryType>( of _: ResourceType.Type, matching query: QueryType? = nil, then completion: @escaping ResultsHandler<HomogeneousArrayResponse<ResourceType>> ) -> URLSessionDataTask where ResourceType: ResourceQueryable, QueryType == ResourceType.QueryTypeParameters
resourceTypeA reference to concrete resource class which conforms to
Decodable & EndpointAccessible & ResourceQueryable.queryQuery to match results against.
completionThe completion handler with
ArrayResponseto call when the request is complete. -
Fetches collections of
EntryDecodableof your own definition.Declaration
Swift
@discardableResult func fetchArray<EntryType>( of _: EntryType.Type, matching query: QueryOn<EntryType> = QueryOn<EntryType>(), then completion: @escaping ResultsHandler<HomogeneousArrayResponse<EntryType>> ) -> URLSessionDataTaskParameters
entryTypeA reference to a concrete Swift class conforming to
EntryDecodablethat will be fetched.queryQuery to match results against.
completionThe completion handler with
ArrayResponseto call when the request is complete. -
Fetches heterogenous collections of types conforming to
EntryDecodable.Declaration
Swift
@discardableResult func fetchArray( matching query: Query? = nil, then completion: @escaping ResultsHandler<HeterogeneousArrayResponse> ) -> URLSessionDataTaskParameters
queryQuery to match results against.
completionThe completion handler to call when the request is complete.
-
Fetches data associated with
AssetProtocolobject.Declaration
Swift
@discardableResult func fetchData( for asset: AssetProtocol, with imageOptions: [ImageOption] = [], then completion: @escaping ResultsHandler<Data> ) -> URLSessionDataTask?Parameters
assetInstance that has the URL for media file.
imageOptionsOptions for server-side manipulations of image files.
completionThe completion handler to call when the request is complete.
-
Fetches the space this client is configured to interface with.
Declaration
Swift
@discardableResult func fetchSpace(then completion: @escaping ResultsHandler<Space>) -> URLSessionDataTask?Parameters
completionThe completion handler to call when the reqeust is complete.
-
Fetches all
Locales belonging to the current space the client is configured to interface with.Declaration
Swift
@discardableResult func fetchLocales(then completion: @escaping ResultsHandler<HomogeneousArrayResponse<Contentful.Locale>>) -> URLSessionDataTaskParameters
completionThe completion handler to call when the request is complete.
-
Performs a synchronization operation, updating the passed in
SyncSpaceinstance with latest content from the server.If passed in
SyncSpaceis an instance with empty sync token, full synchronization will be done.Calling this will mutate passed in
SyncSpaceand also pass back a reference to it in the completion handler in order to allow chaining of operations.Declaration
Swift
@discardableResult public func sync( for syncSpace: SyncSpace = SyncSpace(), syncableTypes: SyncSpace.SyncableTypes = .all, then completion: @escaping ResultsHandler<SyncSpace> ) -> URLSessionDataTask?Parameters
syncSpaceInstance to perform subsequent sync on. Empty instance by default.
syncableTypesThe types that can be synchronized.
completionThe completion handler to call when the operation is complete.
-
Fetch the underlying media file as
UIImage.Declaration
Swift
@discardableResult func fetchImage(for asset: Asset, with imageOptions: [ImageOption] = [], then completion: @escaping ResultsHandler<UIImage>) -> URLSessionDataTask?Parameters
assetThe asset which has the url for the underlying image file.
imageOptionsThe image options to transform the image on the server-side.
completionThe completion handler which takes a
Resultwrapping theDatareturned by the API.Return Value
Returns the
URLSessionDataTaskof the request which can be used for request cancellation.
View on GitHub
Client Class Reference