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

Constant Contact V3: Restore a deleted contact to a list

See more Constant Contact Examples

Look up the deleted contact by email, then restore it by adding its existing ID to a chosen list. Constant Contact documents this membership activity as a restoration method. It changes only membership, preserves the saved profile, and makes no email-permission claim. Select the intended destination list explicitly; previous memberships were removed by deletion. 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()!

    let query = CkoHttpRequest()!
    query.httpVerb = "GET"
    query.addParam(name: "email", value: "reader+newsletter@example.com")
    query.addParam(name: "status", value: "deleted")
    success = http.httpReq(url: "https://api.cc.email/v3/contacts", request: query, 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
    }

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

    if (matches.size(ofArray: "contacts").intValue != 1) || (matches.jsonType(of: "contacts[0].deleted_at").intValue != 1) {
        print("Expected one deleted contact for this email. No restoration submitted.")
        return
    }

    if (matches.jsonType(of: "contacts[0].contact_id").intValue != 1) || (matches.string(ofEquals: "contacts[0].contact_id", value: "", caseSensitive: true) == true) {
        print("Missing contacts[0].contact_id in the response.")
        return
    }

    let body = CkoJsonObject()!
    body.updateString(jsonPath: "source.contact_ids[0]", value: matches.string(of: "contacts[0].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.")

}