Sample code for 30+ languages & platforms
Swift Requires Chilkat v11.0.0+

Constant Contact V3: Read every page of list members

See more Constant Contact Examples

Filter the contacts collection with lists and a list ID. To query multiple lists together, supply their comma-separated IDs. Follow the returned cursor links and report the total after the last page. The default contacts query excludes deleted records. A list member can still have unsubscribed email permission. Authorize the app first and use the same credential store.

Constant Contact developer guide

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    let vault = CkoSecrets()!
    vault.location = "local_manager"
    vault.autoSplit = true
    let entry = CkoJsonObject()!
    entry.updateString(jsonPath: "appName", value: "ConstantContactV3")
    entry.updateString(jsonPath: "service", value: "OAuth2")
    entry.updateString(jsonPath: "username", value: "my-account")
    let tokens = CkoJsonObject()!
    var loaded: Bool = vault.getSecretJson(jsonId: entry, jsonOut: tokens)
    if loaded != true {
        print("Run the desktop authorization example using this account and store.")
        return
    }

    if tokens.jsonType(of: "access_token").intValue != 1 {
        print("The saved authorization has no access token.")
        return
    }

    let http = CkoHttp()!
    http.requireSslCertVerify = true
    http.authToken = tokens.string(of: "access_token")
    let resp = CkoHttpResponse()!

    http.setUrlVar(name: "list", value: "LIST_ID")
    let url = CkoStringBuilder()!
    url.append(value: "https://api.cc.email/v3/contacts?lists={$list}&limit=50")
    let page = CkoJsonObject()!
    var total: Int = 0
    while (url.length.intValue > 0) {
        success = http.httpNoBody(verb: "GET", url: url.getAsString(), response: resp)
        if (success != true) || (resp.statusCode.intValue != 200) {
            print("Expected HTTP 200. Received \(resp.statusCode.intValue)")
            if resp.statusCode.intValue == 401 {
                print("Refresh the saved access token or authorize again.")
            }

            return
        }

        success = page.load(json: resp.bodyStr)
        var count: Int = page.size(ofArray: "contacts").intValue
        if (success != true) || (count < 0) {
            print("Expected a page of contacts.")
            return
        }

        var i: Int = 0
        while (i < count) {
            page.i = i
            print("\(page.string(of: "contacts[i].contact_id")!)  \(page.string(of: "contacts[i].email_address.address")!)")
            i = i + 1
        }

        total = total + count
        url.clear()
        if page.jsonType(of: "_links.next.href").intValue == 1 {
            url.append(value: page.string(of: "_links.next.href"))
            //  The documented cursor link is relative to api.cc.email.
            if url.starts(with: "/v3/contacts?", caseSensitive: true) != true {
                print("Unexpected pagination link. Stopped before following it.")
                return
            }

            url.prepend(value: "https://api.cc.email")
        }

    }

    print("List members read: \(total)")

}