Config

View as Markdown

Config

Config holds the settings that a Stack uses to build its requests. Create one with [[Config alloc] init] in Objective-C or Config() in Swift, set its properties, and pass it to the stackWithAPIKey:accessToken:environmentName:config: method of Contentstack. Use this class to

  • choose the region of your stack
  • send requests to a custom delivery host
  • read content from a branch
  • answer TLS authentication challenges with your own delegate
  • request early access features

A stack created without a Config uses a new Config with the defaults in the table below.

NameTypeDescription
host NSString *

Host name of the delivery API. Setting region replaces it.

region ContentstackRegion

Region of the stack. Setting it sets host to the host of that region.

version NSString *

Read-only. API version in the path of every request.

branch NSString *

Branch UID that every request sends in the branch header.

delegate id<CSURLSessionDelegate>

Object that answers TLS authentication challenges.

[setEarlyAccess](methods/setEarlyAccess.md) NSArray<NSString *> *

Early access feature names for the x-header-ea header.

earlyAccessHeaders

earlyAccessHeaders returns the x-header-ea header that the names in setEarlyAccess produce.

Returns: NSDictionary. The early access header, or an empty dictionary.

Validation

  • There is no client-side validation. earlyAccessHeaders returns no error.

Behavior

  • Client-side only. earlyAccessHeaders makes no request.
  • When setEarlyAccess contains at least one name, the dictionary has one key, x-header-ea. Its value is the names joined with commas and no spaces, for example FEATURE_ONE,FEATURE_TWO.
  • When setEarlyAccess is nil or empty, the dictionary is empty.
  • The stack calls earlyAccessHeaders once, when you create it, and adds the result to its headers. Calling it yourself changes nothing about the requests of a stack.
  • Use it to check the header before you create a stack. The getHeaders method of Stack returns the headers a stack actually sends.

Example

Basic usage: check the header before you create a stack (Objective-C)

#import <Contentstack/Contentstack.h>

Config *config = [[Config alloc] init];
config.setEarlyAccess = @[@"FEATURE_ONE", @"FEATURE_TWO"];
NSDictionary<NSString *, NSString *> *headers = [config earlyAccessHeaders];
NSLog(@"x-header-ea: %@", headers[@"x-header-ea"]);

Basic usage: check the header before you create a stack (Swift)

import Contentstack

let config = Config()
config.setEarlyAccess = ["FEATURE_ONE", "FEATURE_TWO"]
let headers = config.earlyAccessHeaders() ?? [:]
print("x-header-ea: \(headers["x-header-ea"] ?? "")")

setEarlyAccess

setEarlyAccess stores the names of the early access features that a stack requests in the x-header-ea header of every request.

NameTypeDescription
setEarlyAccessNSArray<NSString *> *

Early access feature names to request.

Default: nil

Returns: NSArray. The early access feature names, or nil.

Validation

  • There is no client-side validation. The SDK sends each name as given and does not check it against a list of features.
  • A nil value or an empty array sends no x-header-ea header.
  • setEarlyAccess is a property, not a method. Assign the array with dot syntax in Objective-C or with = in Swift. The call [config setEarlyAccess:@[...]] does not compile, because the Objective-C setter of this property is setSetEarlyAccess:.

Behavior

  • Client-side only. setEarlyAccess makes no request.
  • When you create a stack with this Config, the SDK joins the names with commas and no spaces. It adds the result to the stack headers as x-header-ea. For example, @[@"FEATURE_ONE", @"FEATURE_TWO"] sends x-header-ea: FEATURE_ONE,FEATURE_TWO.
  • The stack reads setEarlyAccess only when you create it. To change the header later, create a new stack, or change the header with the setHeader:forKey: method of Stack.
  • The getHeaders method of Stack returns the header, and the removeHeaderForKey: method removes it.
  • The earlyAccessHeaders method returns the header that the current value produces.

Example

Basic usage: request early access features for a stack (Objective-C)

#import <Contentstack/Contentstack.h>

Config *config = [[Config alloc] init];
config.setEarlyAccess = @[@"FEATURE_ONE", @"FEATURE_TWO"];
Stack *stack = [Contentstack stackWithAPIKey:@"API_KEY" accessToken:@"DELIVERY_TOKEN" environmentName:@"ENVIRONMENT" config:config];
Entry *entry = [[stack contentTypeWithName:@"CONTENT_TYPE_UID"] entryWithUID:@"ENTRY_UID"];
[entry fetch:^(ResponseType type, NSError *error) {
    if (error) {
        NSLog(@"Error %ld: %@", (long)error.code, error.userInfo[@"error_message"]);
        return;
    }
    NSLog(@"%@", entry.title);
}];

Basic usage: request early access features for a stack (Swift)

import Contentstack

let config = Config()
config.setEarlyAccess = ["FEATURE_ONE", "FEATURE_TWO"]
let stack = Contentstack.stack(withAPIKey: "API_KEY", accessToken: "DELIVERY_TOKEN", environmentName: "ENVIRONMENT", config: config)
let entry = stack.contentType(withName: "CONTENT_TYPE_UID").entry(withUID: "ENTRY_UID")
entry.fetch { (type, error) in
    if let error = error as NSError? {
        print("Error \(error.code): \(error.userInfo["error_message"] ?? "")")
        return
    }
    print(entry.title)
}