Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
Constant Contact V3: Add existing contacts to a list
See more Constant Contact Examples
Submit two existing contact IDs and one destination list ID. Constant Contact queues the change as a bulk activity. Save its ID, check the activity state and item errors, and resume checking that ID if the first few polls are inconclusive. Authorize the app first and use the same credential store.
Constant Contact developer guide
Chilkat Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Http,
Chilkat.Secrets,
Chilkat.HttpResponse,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
vault: TSecrets;
entry: TJsonObject;
tokens: TJsonObject;
loaded: Boolean;
http: THttp;
resp: THttpResponse;
body: TJsonObject;
job: TJsonObject;
activityId: string;
attempt: Integer;
state: string;
i: Integer;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
vault := TSecrets.Create;
vault.Location := 'local_manager';
vault.AutoSplit := True;
entry := TJsonObject.Create;
entry.UpdateString('appName','ConstantContactV3');
entry.UpdateString('service','OAuth2');
entry.UpdateString('username','my-account');
tokens := TJsonObject.Create;
loaded := vault.GetSecretJson(entry,tokens);
if (loaded <> True) then
begin
WriteLn('Run the desktop authorization example using this account and store.');
Exit;
end;
if (tokens.JsonTypeOf('access_token') <> 1) then
begin
WriteLn('The saved authorization has no access token.');
Exit;
end;
http := THttp.Create;
http.RequireSslCertVerify := True;
http.AuthToken := tokens.StringOf('access_token');
resp := THttpResponse.Create;
// Use contact IDs from email lookups and the list ID from the selected destination.
body := TJsonObject.Create;
body.UpdateString('source.contact_ids[0]','FIRST_CONTACT_ID');
body.UpdateString('source.contact_ids[1]','SECOND_CONTACT_ID');
body.UpdateString('list_ids[0]','DESTINATION_LIST_ID');
success := http.HttpJson('POST','https://api.cc.email/v3/activities/add_list_memberships',body,'application/json',resp);
if ((success <> True) or (resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
begin
WriteLn('Could not start the membership activity. HTTP ' + resp.StatusCode);
Exit;
end;
job := TJsonObject.Create;
success := job.Load(resp.BodyStr);
if (success <> True) then
begin
WriteLn('Expected a JSON response.');
Exit;
end;
if ((job.JsonTypeOf('activity_id') <> 1) or (job.StringOfEquals('activity_id','',True) = True)) then
begin
WriteLn('Missing activity_id in the response.');
Exit;
end;
activityId := job.StringOf('activity_id');
WriteLn('Submitted activity ID: ' + activityId);
http.SetUrlVar('job',activityId);
// Poll briefly. A longer-running job can be checked later using the same ID.
attempt := 0;
while (attempt < 5) do
begin
success := http.HttpNoBody('GET','https://api.cc.email/v3/activities/{$job}',resp);
if ((success <> True) or (resp.StatusCode <> 200)) then
begin
WriteLn('Status check returned HTTP ' + resp.StatusCode + '. Keep the activity ID and check again later.');
Exit;
end;
success := job.Load(resp.BodyStr);
if ((success <> True) or (job.JsonTypeOf('state') <> 1)) then
begin
WriteLn('Missing activity state. Keep the activity ID.');
Exit;
end;
state := job.StringOf('state');
if ((state <> 'initialized') and (state <> 'processing')) then
begin
WriteLn('Activity state: ' + state);
i := 0;
while (i < job.SizeOfArray('activity_errors')) do
begin
job.I := i;
WriteLn(job.StringOf('activity_errors[i]'));
i := i + 1;
end;
Exit;
end;
attempt := attempt + 1;
if (attempt < 5) then
begin
http.SleepMs(2000);
end;
end;
WriteLn('Still processing. Recheck the activity ID before submitting another request.');
vault.Free;
entry.Free;
tokens.Free;
http.Free;
resp.Free;
body.Free;
job.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.