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

// Use contact IDs from email lookups and the list ID from the selected destination.
let body = chilkat::JsonObject::new();
let _ = body.update_string("source.contact_ids[0]", "FIRST_CONTACT_ID");
let _ = body.update_string("source.contact_ids[1]", "SECOND_CONTACT_ID");
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.");