Swift Requires Chilkat v11.0.0+
Swift
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 Swift Downloads
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()!
var campaignName: String? = "Workshop newsletter"
var campaignId: String? = ""
let url = CkoStringBuilder()!
url.append(value: "https://api.cc.email/v3/emails?limit=50")
let page = CkoJsonObject()!
while ((url.length.intValue > 0) && (campaignId == "")) {
success = http.httpNoBody(verb: "GET", url: url.getAsString(), response: resp)
if (success != true) && (resp.statusCode.intValue <= 0) {
print("Read campaigns: HTTP request failed.")
return
}
if (success != true) || (resp.statusCode.intValue != 200) {
print("Read campaigns: HTTP \(resp.statusCode.intValue) \(resp.bodyStr!)")
return
}
success = page.load(json: resp.bodyStr)
var count: Int = page.size(ofArray: "campaigns").intValue
if (success != true) || (count < 0) {
print("Expected a page of campaigns.")
return
}
var i: Int = 0
while ((i < count) && (campaignId == "")) {
page.i = i
if page.string(ofEquals: "campaigns[i].name", value: campaignName, caseSensitive: true) == true {
campaignId = page.string(of: "campaigns[i].campaign_id")
}
i = i + 1
}
url.clear()
if (campaignId == "") && (page.jsonType(of: "_links.next.href").intValue == 1) {
url.append(value: page.string(of: "_links.next.href"))
if url.starts(with: "/v3/emails?", caseSensitive: true) != true {
print("Unexpected campaign pagination link.")
return
}
url.prepend(value: "https://api.cc.email")
}
}
if campaignId == "" {
print("No campaign named \(campaignName!)")
return
}
http.setUrlVar(name: "campaign", value: campaignId)
success = http.httpNoBody(verb: "GET", url: "https://api.cc.email/v3/emails/{$campaign}", response: resp)
if (success != true) && (resp.statusCode.intValue <= 0) {
print("Read campaign: HTTP request failed.")
return
}
if (success != true) || (resp.statusCode.intValue != 200) {
print("Read campaign: HTTP \(resp.statusCode.intValue) \(resp.bodyStr!)")
return
}
let campaign = CkoJsonObject()!
success = campaign.load(json: resp.bodyStr)
if success != true {
print("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 = CkoJsonObject()!
success = campaign.findRecord2(arrayPath: "campaign_activities", relPath: "role", value: "primary_email", caseSensitive: true, jsonObj: primary)
if (success != true) || (primary.jsonType(of: "campaign_activity_id").intValue != 1) {
print("No primary email activity ID in the response.")
return
}
var selectedCampaign: String? = campaign.string(of: "campaign_id")
var selectedActivity: String? = primary.string(of: "campaign_activity_id")
if (selectedCampaign == "") || (selectedActivity == "") {
print("Missing campaign or activity ID.")
return
}
print("Campaign ID: \(selectedCampaign!)")
print("Primary activity ID: \(selectedActivity!)")
let selection = CkoJsonObject()!
selection.updateString(jsonPath: "name", value: campaign.string(of: "name"))
selection.updateString(jsonPath: "campaign_id", value: selectedCampaign)
selection.updateString(jsonPath: "campaign_activity_id", value: selectedActivity)
success = selection.writeFile(path: "newsletter-selection.json")
if success != true {
print("Could not save newsletter-selection.json. Keep the IDs printed above.")
return
}
print("Saved newsletter-selection.json.")
}