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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoVault
    Variant vEntry
    Handle hoEntry
    Variant vTokens
    Handle hoTokens
    Boolean iLoaded
    Handle hoHttp
    Variant vResp
    Handle hoResp
    Variant vQuery
    Handle hoQuery
    Handle hoMatches
    Variant vBody
    Handle hoBody
    Handle hoJob
    String sActivityId
    Integer iAttempt
    String sState
    Integer i
    String sTemp1
    Integer iTemp1
    Integer iTemp2
    Boolean bTemp1

    Move False To iSuccess

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    Get Create (RefClass(cComChilkatSecrets)) To hoVault
    If (Not(IsComObjectCreated(hoVault))) Begin
        Send CreateComObject of hoVault
    End
    Set ComLocation Of hoVault To "local_manager"
    Set ComAutoSplit Of hoVault To True
    Get Create (RefClass(cComChilkatJsonObject)) To hoEntry
    If (Not(IsComObjectCreated(hoEntry))) Begin
        Send CreateComObject of hoEntry
    End
    Get ComUpdateString Of hoEntry "appName" "ConstantContactV3" To iSuccess
    Get ComUpdateString Of hoEntry "service" "OAuth2" To iSuccess
    Get ComUpdateString Of hoEntry "username" "my-account" To iSuccess
    Get Create (RefClass(cComChilkatJsonObject)) To hoTokens
    If (Not(IsComObjectCreated(hoTokens))) Begin
        Send CreateComObject of hoTokens
    End
    Get pvComObject of hoEntry to vEntry
    Get pvComObject of hoTokens to vTokens
    Get ComGetSecretJson Of hoVault vEntry vTokens To iLoaded
    If (iLoaded <> True) Begin
        Showln "Run the desktop authorization example using this account and store."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoTokens "access_token" To iTemp1
    If (iTemp1 <> 1) Begin
        Showln "The saved authorization has no access token."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Set ComRequireSslCertVerify Of hoHttp To True
    Get ComStringOf Of hoTokens "access_token" To sTemp1
    Set ComAuthToken Of hoHttp To sTemp1
    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End

    Get Create (RefClass(cComChilkatHttpRequest)) To hoQuery
    If (Not(IsComObjectCreated(hoQuery))) Begin
        Send CreateComObject of hoQuery
    End
    Set ComHttpVerb Of hoQuery To "GET"
    Send ComAddParam To hoQuery "email" "reader+newsletter@example.com"
    Send ComAddParam To hoQuery "status" "deleted"
    Get pvComObject of hoQuery to vQuery
    Get pvComObject of hoResp to vResp
    Get ComHttpReq Of hoHttp "https://api.cc.email/v3/contacts" vQuery vResp To iSuccess
    Get ComStatusCode Of hoResp To iTemp1
    If ((iSuccess <> True) Or (iTemp1 <> 200)) Begin
        Get ComStatusCode Of hoResp To iTemp1
        Showln "Expected HTTP 200. Received " iTemp1
        Get ComStatusCode Of hoResp To iTemp1
        If (iTemp1 = 401) Begin
            Showln "Refresh the saved access token or authorize again."
        End

        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoMatches
    If (Not(IsComObjectCreated(hoMatches))) Begin
        Send CreateComObject of hoMatches
    End
    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoad Of hoMatches sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Expected a JSON response."
        Procedure_Return
    End

    Get ComSizeOfArray Of hoMatches "contacts" To iTemp1
    Get ComJsonTypeOf Of hoMatches "contacts[0].deleted_at" To iTemp2
    If ((iTemp1 <> 1) Or (iTemp2 <> 1)) Begin
        Showln "Expected one deleted contact for this email. No restoration submitted."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoMatches "contacts[0].contact_id" To iTemp1
    Get ComStringOfEquals Of hoMatches "contacts[0].contact_id" "" True To bTemp1
    If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
        Showln "Missing contacts[0].contact_id in the response."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoBody
    If (Not(IsComObjectCreated(hoBody))) Begin
        Send CreateComObject of hoBody
    End
    Get ComStringOf Of hoMatches "contacts[0].contact_id" To sTemp1
    Get ComUpdateString Of hoBody "source.contact_ids[0]" sTemp1 To iSuccess
    Get ComUpdateString Of hoBody "list_ids[0]" "DESTINATION_LIST_ID" To iSuccess
    Get pvComObject of hoBody to vBody
    Get pvComObject of hoResp to vResp
    Get ComHttpJson Of hoHttp "POST" "https://api.cc.email/v3/activities/add_list_memberships" vBody "application/json" vResp To iSuccess
    Get ComStatusCode Of hoResp To iTemp1
    Get ComStatusCode Of hoResp To iTemp2
    If ((iSuccess <> True) Or (iTemp1 < 200) Or (iTemp2 >= 300)) Begin
        Get ComStatusCode Of hoResp To iTemp1
        Showln "Could not start the membership activity. HTTP " iTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJob
    If (Not(IsComObjectCreated(hoJob))) Begin
        Send CreateComObject of hoJob
    End
    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoad Of hoJob sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Expected a JSON response."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoJob "activity_id" To iTemp1
    Get ComStringOfEquals Of hoJob "activity_id" "" True To bTemp1
    If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
        Showln "Missing activity_id in the response."
        Procedure_Return
    End

    Get ComStringOf Of hoJob "activity_id" To sActivityId
    Showln "Submitted activity ID: " sActivityId
    Get ComSetUrlVar Of hoHttp "job" sActivityId To iSuccess
    //  Poll briefly. A longer-running job can be checked later using the same ID.
    Move 0 To iAttempt
    While (iAttempt < 5)
        Get pvComObject of hoResp to vResp
        Get ComHttpNoBody Of hoHttp "GET" "https://api.cc.email/v3/activities/{$job}" vResp To iSuccess
        Get ComStatusCode Of hoResp To iTemp1
        If ((iSuccess <> True) Or (iTemp1 <> 200)) Begin
            Get ComStatusCode Of hoResp To iTemp1
            Showln "Status check returned HTTP " iTemp1 ". Keep the activity ID and check again later."
            Procedure_Return
        End

        Get ComBodyStr Of hoResp To sTemp1
        Get ComLoad Of hoJob sTemp1 To iSuccess
        Get ComJsonTypeOf Of hoJob "state" To iTemp1
        If ((iSuccess <> True) Or (iTemp1 <> 1)) Begin
            Showln "Missing activity state. Keep the activity ID."
            Procedure_Return
        End

        Get ComStringOf Of hoJob "state" To sState
        If ((sState <> "initialized") And (sState <> "processing")) Begin
            Showln "Activity state: " sState
            Move 0 To i
            While (i < (ComSizeOfArray(hoJob, "activity_errors")))
                Set ComI Of hoJob To i
                Get ComStringOf Of hoJob "activity_errors[i]" To sTemp1
                Showln sTemp1
                Move (i + 1) To i
            Loop

            Procedure_Return
        End

        Move (iAttempt + 1) To iAttempt
        If (iAttempt < 5) Begin
            Send ComSleepMs To hoHttp 2000
        End

    Loop

    Showln "Still processing. Recheck the activity ID before submitting another request."


End_Procedure