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

Unicode C++
#include <CkSecretsW.h>
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkHttpRequestW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    CkSecretsW vault;
    vault.put_Location(L"local_manager");
    vault.put_AutoSplit(true);
    CkJsonObjectW entry;
    entry.UpdateString(L"appName",L"ConstantContactV3");
    entry.UpdateString(L"service",L"OAuth2");
    entry.UpdateString(L"username",L"my-account");
    CkJsonObjectW tokens;
    bool loaded = vault.GetSecretJson(entry,tokens);
    if (loaded != true) {
        wprintf(L"Run the desktop authorization example using this account and store.\n");
        return;
    }

    if (tokens.JsonTypeOf(L"access_token") != 1) {
        wprintf(L"The saved authorization has no access token.\n");
        return;
    }

    CkHttpW http;
    http.put_RequireSslCertVerify(true);
    http.put_AuthToken(tokens.stringOf(L"access_token"));
    CkHttpResponseW resp;

    CkHttpRequestW query;
    query.put_HttpVerb(L"GET");
    query.AddParam(L"email",L"reader+newsletter@example.com");
    query.AddParam(L"status",L"deleted");
    success = http.HttpReq(L"https://api.cc.email/v3/contacts",query,resp);
    if ((success != true) || (resp.get_StatusCode() != 200)) {
        wprintf(L"Expected HTTP 200. Received %d\n",resp.get_StatusCode());
        if (resp.get_StatusCode() == 401) {
            wprintf(L"Refresh the saved access token or authorize again.\n");
        }

        return;
    }

    CkJsonObjectW matches;
    success = matches.Load(resp.bodyStr());
    if (success != true) {
        wprintf(L"Expected a JSON response.\n");
        return;
    }

    if ((matches.SizeOfArray(L"contacts") != 1) || (matches.JsonTypeOf(L"contacts[0].deleted_at") != 1)) {
        wprintf(L"Expected one deleted contact for this email. No restoration submitted.\n");
        return;
    }

    if ((matches.JsonTypeOf(L"contacts[0].contact_id") != 1) || (matches.StringOfEquals(L"contacts[0].contact_id",L"",true) == true)) {
        wprintf(L"Missing contacts[0].contact_id in the response.\n");
        return;
    }

    CkJsonObjectW body;
    body.UpdateString(L"source.contact_ids[0]",matches.stringOf(L"contacts[0].contact_id"));
    body.UpdateString(L"list_ids[0]",L"DESTINATION_LIST_ID");
    success = http.HttpJson(L"POST",L"https://api.cc.email/v3/activities/add_list_memberships",body,L"application/json",resp);
    if ((success != true) || (resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
        wprintf(L"Could not start the membership activity. HTTP %d\n",resp.get_StatusCode());
        return;
    }

    CkJsonObjectW job;
    success = job.Load(resp.bodyStr());
    if (success != true) {
        wprintf(L"Expected a JSON response.\n");
        return;
    }

    if ((job.JsonTypeOf(L"activity_id") != 1) || (job.StringOfEquals(L"activity_id",L"",true) == true)) {
        wprintf(L"Missing activity_id in the response.\n");
        return;
    }

    const wchar_t *activityId = job.stringOf(L"activity_id");
    wprintf(L"Submitted activity ID: %s\n",activityId);
    http.SetUrlVar(L"job",activityId);
    //  Poll briefly. A longer-running job can be checked later using the same ID.
    int attempt = 0;
    while ((attempt < 5)) {
        success = http.HttpNoBody(L"GET",L"https://api.cc.email/v3/activities/{$job}",resp);
        if ((success != true) || (resp.get_StatusCode() != 200)) {
            wprintf(L"Status check returned HTTP %d. Keep the activity ID and check again later.\n",resp.get_StatusCode());
            return;
        }

        success = job.Load(resp.bodyStr());
        if ((success != true) || (job.JsonTypeOf(L"state") != 1)) {
            wprintf(L"Missing activity state. Keep the activity ID.\n");
            return;
        }

        const wchar_t *state = job.stringOf(L"state");
        if ((state != L"initialized") && (state != L"processing")) {
            wprintf(L"Activity state: %s\n",state);
            int i = 0;
            while ((i < job.SizeOfArray(L"activity_errors"))) {
                job.put_I(i);
                wprintf(L"%s\n",job.stringOf(L"activity_errors[i]"));
                i = i + 1;
            }

            return;
        }

        attempt = attempt + 1;
        if (attempt < 5) {
            http.SleepMs(2000);
        }

    }

    wprintf(L"Still processing. Recheck the activity ID before submitting another request.\n");
    }