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 Client when new Entry and Asset objects are created from data being sent over the network. Currently, these messages are only sent during the response hadling for client.sync calls. See a CoreData persistence integration at https://github.com/contentful/contentful-persistence.swift.

    Declaration

    Swift

    public var persistenceIntegration: PersistenceIntegration? { get set }
  • 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

    spaceId

    The identifier of the space to perform requests against.

    environmentId

    The identifier of the space environment to perform requests against. Defaults to “master”.

    accessToken

    The access token used for authorization.

    host

    The domain host to perform requests against. Defaults to Host.delivery i.e. "cdn.contentful.com".

    clientConfiguration

    Custom Configuration of the Client. Uses ClientConfiguration.default if omitted.

    sessionConfiguration

    The configuration for the URLSession. Note that HTTP headers will be overwritten internally by the SDK so that requests can be authorized correctly.

    persistenceIntegration

    An object conforming to the PersistenceIntegration protocol which will receive messages about created/deleted Resources when calling sync methods.

    contentTypeClasses

    An array of EntryDecodable classes 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) -> URL

    Parameters

    endpoint

    The delivery/preview API endpoint.

    parameters

    A 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 Data objects and bypass the JSON parsing provided by the SDK.

    Declaration

    Swift

    @discardableResult
    func fetch(
        url: URL,
        then completion: @escaping ResultsHandler<Data>
    ) -> URLSessionDataTask

    Parameters

    url

    The URL representing the endpoint with query parameters.

    completion

    The 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>
    ) -> URLSessionDataTask

    Parameters

    url

    The URL representing the endpoint with query parameters.

    completion

    The completion handler wrapping DecodableType to 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 to EntryDecodable or AssetDecodable.

    Declaration

    Swift

    @discardableResult
    func fetch<ResourceType>(
        _ resourceType: ResourceType.Type,
        id: String,
        include includesLevel: UInt? = nil,
        then completion: @escaping ResultsHandler<ResourceType>
    ) -> URLSessionDataTask where ResourceType: Decodable & EndpointAccessible

    Parameters

    resourceType

    A reference to the Swift type which conforms to Decodable & EndpointAccessible.

    id

    The identifier of the resource.

    include

    The level of includes to be resolved. Default value when nil. See more: Retrieval of linked items.

    completion

    The completion handler to call when the request is complete.

  • Fetches collections of ContentType, Entry, and Asset types.

    Declaration

    Swift

    @discardableResult
    func fetchArray<ResourceType, QueryType>(
        of resourceType: ResourceType.Type,
        matching query: QueryType? = nil,
        then completion: @escaping ResultsHandler<HomogeneousArrayResponse<ResourceType>>
    ) -> URLSessionDataTask where ResourceType: ResourceQueryable, QueryType == ResourceType.QueryType

    Parameters

    resourceType

    A reference to concrete resource class which conforms to Decodable & EndpointAccessible & ResourceQueryable.

    query

    Query to match results against.

    completion

    The completion handler with ArrayResponse to call when the request is complete.

  • Fetches collections of EntryDecodable of your own definition.

    Declaration

    Swift

    @discardableResult
    func fetchArray<EntryType>(
        of entryType: EntryType.Type,
        matching query: QueryOn<EntryType> = QueryOn<EntryType>(),
        then completion: @escaping ResultsHandler<HomogeneousArrayResponse<EntryType>>
    ) -> URLSessionDataTask

    Parameters

    entryType

    A reference to a concrete Swift class conforming to EntryDecodable that will be fetched.

    query

    Query to match results against.

    completion

    The completion handler with ArrayResponse to 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>
    ) -> URLSessionDataTask

    Parameters

    query

    Query to match results against.

    completion

    The completion handler to call when the request is complete.

  • Fetches data associated with AssetProtocol object.

    Declaration

    Swift

    @discardableResult
    func fetchData(
        for asset: AssetProtocol,
        with imageOptions: [ImageOption] = [],
        then completion: @escaping ResultsHandler<Data>
    ) -> URLSessionDataTask?

    Parameters

    asset

    Instance that has the URL for media file.

    imageOptions

    Options for server-side manipulations of image files.

    completion

    The 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

    completion

    The 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>>) -> URLSessionDataTask

    Parameters

    completion

    The completion handler to call when the request is complete.

  • Performs a synchronization operation, updating the passed in SyncSpace instance with latest content from the server.

    If passed in SyncSpace is 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

    syncSpace

    Instance to perform subsqeuent sync on. Empty instance by default.

    syncableTypes

    The types that can be synchronized.

    completion

    The completion handler to call when the operation is complete.

  • Fetch the underlying media file as UIImage.

    Declaration

    Swift

    @discardableResult
    public func fetchImage(for asset: Asset,
                           with imageOptions: [ImageOption] = [],
                           then completion: @escaping ResultsHandler<UIImage>) -> URLSessionDataTask?

    Parameters

    asset

    The asset which has the url for the underlying image file.

    imageOptions

    The image options to transform the image on the server-side.

    completion

    The completion handler which takes a Result wrapping the Data returned by the API.

    Return Value

    Returns the URLSessionDataTask of the request which can be used for request cancellation.