Sample code for 30+ languages & platforms
Rust 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 Rust Downloads

Rust
let mut success = false;

// Assumes Chilkat is unlocked. See Global Unlock Sample.
let vault = chilkat::Secrets::new();
vault.set_location("local_manager");
vault.set_auto_split(true);
let entry = chilkat::JsonObject::new();
let _ = entry.update_string("appName", "ConstantContactV3");
let _ = entry.update_string("service", "OAuth2");
let _ = entry.update_string("username", "my-account");
let tokens = chilkat::JsonObject::new();
if vault.get_secret_json(&entry, &tokens).is_err() {
    println!("Run the desktop authorization example using this account and store.");
    return;
}

if tokens.json_type_of("access_token") != 1 {
    println!("The saved authorization has no access token.");
    return;
}

let http = chilkat::Http::new();
http.set_require_ssl_cert_verify(true);
http.set_auth_token(&tokens.string_of("access_token").unwrap_or_default());
let resp = chilkat::HttpResponse::new();

let query = chilkat::HttpRequest::new();
query.set_http_verb("GET");
query.add_param("email", "reader+newsletter@example.com");
query.add_param("status", "deleted");
success = http.http_req("https://api.cc.email/v3/contacts", &query, &resp).is_ok();
if (!success) || (resp.status_code() != 200) {
    println!("Expected HTTP 200. Received {}", resp.status_code());
    if resp.status_code() == 401 {
        println!("Refresh the saved access token or authorize again.");
    }

    return;
}

let matches = chilkat::JsonObject::new();
success = matches.load(&resp.body_str()).is_ok();
if !success {
    println!("Expected a JSON response.");
    return;
}

if (matches.size_of_array("contacts") != 1) || (matches.json_type_of("contacts[0].deleted_at") != 1) {
    println!("Expected one deleted contact for this email. No restoration submitted.");
    return;
}

if (matches.json_type_of("contacts[0].contact_id") != 1) || (matches.string_of_equals("contacts[0].contact_id", "", true)) {
    println!("Missing contacts[0].contact_id in the response.");
    return;
}

let body = chilkat::JsonObject::new();
let _ = body.update_string("source.contact_ids[0]", &matches.string_of("contacts[0].contact_id").unwrap_or_default());
let _ = body.update_string("list_ids[0]", "DESTINATION_LIST_ID");
success = http.http_json("POST", "https://api.cc.email/v3/activities/add_list_memberships", &body, "application/json", &resp).is_ok();
if (!success) || ((resp.status_code() < 200) || (resp.status_code() >= 300)) {
    println!("Could not start the membership activity. HTTP {}", resp.status_code());
    return;
}

let job = chilkat::JsonObject::new();
success = job.load(&resp.body_str()).is_ok();
if !success {
    println!("Expected a JSON response.");
    return;
}

if (job.json_type_of("activity_id") != 1) || (job.string_of_equals("activity_id", "", true)) {
    println!("Missing activity_id in the response.");
    return;
}

let activity_id = job.string_of("activity_id").unwrap_or_default();
println!("Submitted activity ID: {}", activity_id);
let _ = http.set_url_var("job", &activity_id);
// Poll briefly. A longer-running job can be checked later using the same ID.
let mut attempt = 0;
while attempt < 5 {
    success = http.http_no_body("GET", "https://api.cc.email/v3/activities/{$job}", &resp).is_ok();
    if (!success) || (resp.status_code() != 200) {
        println!("Status check returned HTTP {}. Keep the activity ID and check again later.", resp.status_code());
        return;
    }

    success = job.load(&resp.body_str()).is_ok();
    if (!success) || (job.json_type_of("state") != 1) {
        println!("Missing activity state. Keep the activity ID.");
        return;
    }

    let state = job.string_of("state").unwrap_or_default();
    if (state != "initialized") && (state != "processing") {
        println!("Activity state: {}", state);
        let mut i = 0;
        while i < job.size_of_array("activity_errors") {
            job.set_i(i);
            println!("{}", job.string_of("activity_errors[i]").unwrap_or_default());
            i = i + 1;
        }

        return;
    }

    attempt = attempt + 1;
    if attempt < 5 {
        http.sleep_ms(2000);
    }

}

println!("Still processing. Recheck the activity ID before submitting another request.");