Objective-C Requires Chilkat v11.0.0+
Objective-C
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 Objective-C Downloads
#import <CkoSecrets.h>
#import <CkoJsonObject.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <CkoHttpRequest.h>
#import <NSString.h>
BOOL success = NO;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkoSecrets *vault = [[CkoSecrets alloc] init];
vault.Location = @"local_manager";
vault.AutoSplit = YES;
CkoJsonObject *entry = [[CkoJsonObject alloc] init];
[entry UpdateString: @"appName" value: @"ConstantContactV3"];
[entry UpdateString: @"service" value: @"OAuth2"];
[entry UpdateString: @"username" value: @"my-account"];
CkoJsonObject *tokens = [[CkoJsonObject alloc] init];
BOOL loaded = [vault GetSecretJson: entry jsonOut: tokens];
if (loaded != YES) {
NSLog(@"%@",@"Run the desktop authorization example using this account and store.");
return;
}
if ([[tokens JsonTypeOf: @"access_token"] intValue] != 1) {
NSLog(@"%@",@"The saved authorization has no access token.");
return;
}
CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
http.AuthToken = [tokens StringOf: @"access_token"];
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
CkoHttpRequest *query = [[CkoHttpRequest alloc] init];
query.HttpVerb = @"GET";
[query AddParam: @"email" value: @"reader+newsletter@example.com"];
[query AddParam: @"status" value: @"deleted"];
success = [http HttpReq: @"https://api.cc.email/v3/contacts" request: query response: resp];
if ((success != YES) || ([resp.StatusCode intValue] != 200)) {
NSLog(@"%@%d",@"Expected HTTP 200. Received ",[resp.StatusCode intValue]);
if ([resp.StatusCode intValue] == 401) {
NSLog(@"%@",@"Refresh the saved access token or authorize again.");
}
return;
}
CkoJsonObject *matches = [[CkoJsonObject alloc] init];
success = [matches Load: resp.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Expected a JSON response.");
return;
}
if (([[matches SizeOfArray: @"contacts"] intValue] != 1) || ([[matches JsonTypeOf: @"contacts[0].deleted_at"] intValue] != 1)) {
NSLog(@"%@",@"Expected one deleted contact for this email. No restoration submitted.");
return;
}
if (([[matches JsonTypeOf: @"contacts[0].contact_id"] intValue] != 1) || ([matches StringOfEquals: @"contacts[0].contact_id" value: @"" caseSensitive: YES] == YES)) {
NSLog(@"%@",@"Missing contacts[0].contact_id in the response.");
return;
}
CkoJsonObject *body = [[CkoJsonObject alloc] init];
[body UpdateString: @"source.contact_ids[0]" value: [matches StringOf: @"contacts[0].contact_id"]];
[body UpdateString: @"list_ids[0]" value: @"DESTINATION_LIST_ID"];
success = [http HttpJson: @"POST" url: @"https://api.cc.email/v3/activities/add_list_memberships" json: body contentType: @"application/json" response: resp];
if ((success != YES) || ([resp.StatusCode intValue] < 200) || ([resp.StatusCode intValue] >= 300)) {
NSLog(@"%@%d",@"Could not start the membership activity. HTTP ",[resp.StatusCode intValue]);
return;
}
CkoJsonObject *job = [[CkoJsonObject alloc] init];
success = [job Load: resp.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Expected a JSON response.");
return;
}
if (([[job JsonTypeOf: @"activity_id"] intValue] != 1) || ([job StringOfEquals: @"activity_id" value: @"" caseSensitive: YES] == YES)) {
NSLog(@"%@",@"Missing activity_id in the response.");
return;
}
NSString *activityId = [job StringOf: @"activity_id"];
NSLog(@"%@%@",@"Submitted activity ID: ",activityId);
[http SetUrlVar: @"job" value: activityId];
// Poll briefly. A longer-running job can be checked later using the same ID.
int attempt = 0;
while ((attempt < 5)) {
success = [http HttpNoBody: @"GET" url: @"https://api.cc.email/v3/activities/{$job}" response: resp];
if ((success != YES) || ([resp.StatusCode intValue] != 200)) {
NSLog(@"%@%d%@",@"Status check returned HTTP ",[resp.StatusCode intValue],@". Keep the activity ID and check again later.");
return;
}
success = [job Load: resp.BodyStr];
if ((success != YES) || ([[job JsonTypeOf: @"state"] intValue] != 1)) {
NSLog(@"%@",@"Missing activity state. Keep the activity ID.");
return;
}
NSString *state = [job StringOf: @"state"];
if ((state != @"initialized") && (state != @"processing")) {
NSLog(@"%@%@",@"Activity state: ",state);
int i = 0;
while ((i < [[job SizeOfArray: @"activity_errors"] intValue])) {
job.I = [NSNumber numberWithInt: i];
NSLog(@"%@",[job StringOf: @"activity_errors[i]"]);
i = i + 1;
}
return;
}
attempt = attempt + 1;
if (attempt < 5) {
[http SleepMs: [NSNumber numberWithInt: 2000]];
}
}
NSLog(@"%@",@"Still processing. Recheck the activity ID before submitting another request.");