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

Constant Contact V3: Add existing contacts to a list

See more Constant Contact Examples

Submit two existing contact IDs and one destination list ID. Constant Contact queues the change as a bulk activity. Save its ID, check the activity state and item errors, and resume checking that ID if the first few polls are inconclusive. 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()!

    //  Use contact IDs from email lookups and the list ID from the selected destination.
    let body = CkoJsonObject()!
    body.updateString(jsonPath: "source.contact_ids[0]", value: "FIRST_CONTACT_ID")
    body.updateString(jsonPath: "source.contact_ids[1]", value: "SECOND_CONTACT_ID")
    body.updateString(jsonPath: "list_ids[0]", value: "DESTINATION_LIST_ID")
    success = http.httpJson(verb: "POST", url: "https://api.cc.email/v3/activities/add_list_memberships", json: body, contentType: "application/json", response: resp)
    if (success != true) || (resp.statusCode.intValue < 200) || (resp.statusCode.intValue >= 300) {
        print("Could not start the membership activity. HTTP \(resp.statusCode.intValue)")
        return
    }

    let job = CkoJsonObject()!
    success = job.load(json: resp.bodyStr)
    if success != true {
        print("Expected a JSON response.")
        return
    }

    if (job.jsonType(of: "activity_id").intValue != 1) || (job.string(ofEquals: "activity_id", value: "", caseSensitive: true) == true) {
        print("Missing activity_id in the response.")
        return
    }

    var activityId: String? = job.string(of: "activity_id")
    print("Submitted activity ID: \(activityId!)")
    http.setUrlVar(name: "job", value: activityId)
    //  Poll briefly. A longer-running job can be checked later using the same ID.
    var attempt: Int = 0
    while (attempt < 5) {
        success = http.httpNoBody(verb: "GET", url: "https://api.cc.email/v3/activities/{$job}", response: resp)
        if (success != true) || (resp.statusCode.intValue != 200) {
            print("Status check returned HTTP \(resp.statusCode.intValue). Keep the activity ID and check again later.")
            return
        }

        success = job.load(json: resp.bodyStr)
        if (success != true) || (job.jsonType(of: "state").intValue != 1) {
            print("Missing activity state. Keep the activity ID.")
            return
        }

        var state: String? = job.string(of: "state")
        if (state != "initialized") && (state != "processing") {
            print("Activity state: \(state!)")
            var i: Int = 0
            while (i < job.size(ofArray: "activity_errors").intValue) {
                job.i = i
                print("\(job.string(of: "activity_errors[i]")!)")
                i = i + 1
            }

            return
        }

        attempt = attempt + 1
        if attempt < 5 {
            http.sleepMs(millisec: 2000)
        }

    }

    print("Still processing. Recheck the activity ID before submitting another request.")

}