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

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  var success = false;

  // Assumes Chilkat is unlocked. See Global Unlock Sample.
  final vault = CkSecrets();
  vault.location = 'local_manager';
  vault.autoSplit = true;
  final entry = CkJsonObject();
  entry.updateString('appName', 'ConstantContactV3');
  entry.updateString('service', 'OAuth2');
  entry.updateString('username', 'my-account');
  final tokens = CkJsonObject();
  try {
    vault.getSecretJson(entry, tokens);
  } on ChilkatException {
    print('Run the desktop authorization example using this account and store.');
    return;
  }

  if (tokens.jsonTypeOf('access_token') != 1) {
    print('The saved authorization has no access token.');
    return;
  }

  final http = CkHttp();
  http.requireSslCertVerify = true;
  http.authToken = tokens.stringOf('access_token');
  final resp = CkHttpResponse();

  final query = CkHttpRequest();
  query.httpVerb = 'GET';
  query.addParam('email', 'reader+newsletter@example.com');
  query.addParam('status', 'deleted');
  success = true;
  try {
    http.httpReq('https://api.cc.email/v3/contacts', query, resp);
  } on ChilkatException {
    success = false;
  }
  if ((!success) || (resp.statusCode != 200)) {
    print('Expected HTTP 200. Received ${resp.statusCode}');
    if (resp.statusCode == 401) {
      print('Refresh the saved access token or authorize again.');
    }

    return;
  }

  final matches = CkJsonObject();
  success = true;
  try {
    matches.load(resp.bodyStr);
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print('Expected a JSON response.');
    return;
  }

  if ((matches.sizeOfArray('contacts') != 1) || (matches.jsonTypeOf('contacts[0].deleted_at') != 1)) {
    print('Expected one deleted contact for this email. No restoration submitted.');
    return;
  }

  if ((matches.jsonTypeOf('contacts[0].contact_id') != 1) || (matches.stringOfEquals('contacts[0].contact_id', '', true))) {
    print('Missing contacts[0].contact_id in the response.');
    return;
  }

  final body = CkJsonObject();
  body.updateString('source.contact_ids[0]', matches.stringOf('contacts[0].contact_id'));
  body.updateString('list_ids[0]', 'DESTINATION_LIST_ID');
  success = true;
  try {
    http.httpJson('POST', 'https://api.cc.email/v3/activities/add_list_memberships', body, 'application/json', resp);
  } on ChilkatException {
    success = false;
  }
  if ((!success) || ((resp.statusCode < 200) || (resp.statusCode >= 300))) {
    print('Could not start the membership activity. HTTP ${resp.statusCode}');
    return;
  }

  final job = CkJsonObject();
  success = true;
  try {
    job.load(resp.bodyStr);
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print('Expected a JSON response.');
    return;
  }

  if ((job.jsonTypeOf('activity_id') != 1) || (job.stringOfEquals('activity_id', '', true))) {
    print('Missing activity_id in the response.');
    return;
  }

  final activityId = job.stringOf('activity_id');
  print('Submitted activity ID: $activityId');
  http.setUrlVar('job', activityId);
  // Poll briefly. A longer-running job can be checked later using the same ID.
  var attempt = 0;
  while (attempt < 5) {
    success = true;
    try {
      http.httpNoBody('GET', 'https://api.cc.email/v3/activities/{\$job}', resp);
    } on ChilkatException {
      success = false;
    }
    if ((!success) || (resp.statusCode != 200)) {
      print('Status check returned HTTP ${resp.statusCode}. Keep the activity ID and check again later.');
      return;
    }

    success = true;
    try {
      job.load(resp.bodyStr);
    } on ChilkatException {
      success = false;
    }
    if ((!success) || (job.jsonTypeOf('state') != 1)) {
      print('Missing activity state. Keep the activity ID.');
      return;
    }

    final state = job.stringOf('state');
    if ((state != 'initialized') && (state != 'processing')) {
      print('Activity state: $state');
      var i = 0;
      while (i < job.sizeOfArray('activity_errors')) {
        job.i = i;
        print(job.stringOf('activity_errors[i]'));
        i++;
      }

      return;
    }

    attempt++;
    if (attempt < 5) {
      http.sleepMs(2000);
    }
  }

  print('Still processing. Recheck the activity ID before submitting another request.');
}