Rust Requires Chilkat v11.0.0+
Rust
Constant Contact V3: Find a campaign and its primary email activity
See more Constant Contact Examples
Enter the unique campaign name from the account. Follow the campaign collection’s cursor until the name matches, then fetch that campaign’s details. Select the activity whose role is primary_email. A permalink or resend can appear first.
The example writes the two IDs to newsletter-selection.json, the same file produced by the draft example. Run this lookup when selecting an existing newsletter for review or scheduling.
Authorize the application using the same account and credential store.
Constant Contact developer guide
Chilkat Rust Downloads
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 campaign_name = "Workshop newsletter".to_string();
let mut campaign_id = String::new();
let url = chilkat::StringBuilder::new();
let _ = url.append("https://api.cc.email/v3/emails?limit=50");
let page = chilkat::JsonObject::new();
while (url.length() > 0) && (campaign_id == "") {
success = http.http_no_body("GET", &url.get_as_string().unwrap_or_default(), &resp).is_ok();
if (!success) && (resp.status_code() <= 0) {
println!("Read campaigns: HTTP request failed.");
return;
}
if (!success) || (resp.status_code() != 200) {
println!("Read campaigns: HTTP {} {}", resp.status_code(), resp.body_str());
return;
}
success = page.load(&resp.body_str()).is_ok();
let count = page.size_of_array("campaigns");
if (!success) || (count < 0) {
println!("Expected a page of campaigns.");
return;
}
let mut i = 0;
while (i < count) && (campaign_id == "") {
page.set_i(i);
if page.string_of_equals("campaigns[i].name", &campaign_name, true) {
campaign_id = page.string_of("campaigns[i].campaign_id").unwrap_or_default();
}
i = i + 1;
}
url.clear();
if (campaign_id == "") && (page.json_type_of("_links.next.href") == 1) {
let _ = url.append(&page.string_of("_links.next.href").unwrap_or_default());
if !url.starts_with("/v3/emails?", true) {
println!("Unexpected campaign pagination link.");
return;
}
let _ = url.prepend("https://api.cc.email");
}
}
if campaign_id == "" {
println!("No campaign named {}", campaign_name);
return;
}
let _ = http.set_url_var("campaign", &campaign_id);
success = http.http_no_body("GET", "https://api.cc.email/v3/emails/{$campaign}", &resp).is_ok();
if (!success) && (resp.status_code() <= 0) {
println!("Read campaign: HTTP request failed.");
return;
}
if (!success) || (resp.status_code() != 200) {
println!("Read campaign: HTTP {} {}", resp.status_code(), resp.body_str());
return;
}
let campaign = chilkat::JsonObject::new();
success = campaign.load(&resp.body_str()).is_ok();
if !success {
println!("Expected a JSON object in the response.");
return;
}
// The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
let primary = chilkat::JsonObject::new();
success = campaign.find_record2("campaign_activities", "role", "primary_email", true, &primary).is_ok();
if (!success) || (primary.json_type_of("campaign_activity_id") != 1) {
println!("No primary email activity ID in the response.");
return;
}
let selected_campaign = campaign.string_of("campaign_id").unwrap_or_default();
let selected_activity = primary.string_of("campaign_activity_id").unwrap_or_default();
if (selected_campaign == "") || (selected_activity == "") {
println!("Missing campaign or activity ID.");
return;
}
println!("Campaign ID: {}", selected_campaign);
println!("Primary activity ID: {}", selected_activity);
let selection = chilkat::JsonObject::new();
let _ = selection.update_string("name", &campaign.string_of("name").unwrap_or_default());
let _ = selection.update_string("campaign_id", &selected_campaign);
let _ = selection.update_string("campaign_activity_id", &selected_activity);
if selection.write_file("newsletter-selection.json").is_err() {
println!("Could not save newsletter-selection.json. Keep the IDs printed above.");
return;
}
println!("Saved newsletter-selection.json.");