Sample code for 30+ languages & platforms
Visual FoxPro Requires Chilkat v11.5.0+

Box: Poll User Events and Save a Checkpoint

See more Box Examples

Poll the authenticated user event stream in bounded batches. The local JSON checkpoint keeps processed event IDs and the next stream position. This single-process demonstration logs events; a production worker should commit its business action, deduplication record, and cursor transactionally and prune old IDs.

See the Box API guide for setup and the complete workflow.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loCurl
LOCAL loState
LOCAL loFiles
LOCAL lcStatePath
LOCAL loPage
LOCAL loSeenPath
LOCAL lcEventId
LOCAL lcNextPosition
LOCAL lnBatch
LOCAL i
LOCAL lnCount
LOCAL lnMore
LOCAL loAuthorizedCommand1
LOCAL lnHeaderReplacement1

lnSuccess = 0

*  Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
*  See Global Unlock Sample.
*  Guide: Box API Guide.

lnSuccess = 0

*  Supply an unexpired token obtained with one of the Box authentication examples.
*  A token represents a particular user or service account. IDs must be accessible to it.
loCurl = CreateObject('Chilkat.HttpCurl')
loCurl.SetVar("access_token","BOX_ACCESS_TOKEN")

loState = CreateObject('Chilkat.JsonObject')
loFiles = CreateObject('Chilkat.FileAccess')
lcStatePath = "qa_data/box_events_state.json"
IF (loFiles.FileExists(lcStatePath) = 1) THEN
    lnSuccess = loState.LoadFile(lcStatePath)
    IF (lnSuccess <> 1) THEN
        ? loState.LastErrorText
        RELEASE loCurl
        RELEASE loState
        RELEASE loFiles
        CANCEL
    ENDIF

    IF (loState.HasMember("position") <> 1) THEN
        ? "Invalid checkpoint: position missing."
        RELEASE loCurl
        RELEASE loState
        RELEASE loFiles
        CANCEL
    ENDIF

ELSE
    *  "now" starts a new subscription. It deliberately skips historical events.
    loState.UpdateString("position","now")
ENDIF

loCurl.SetVar("position",loState.StringOf("position"))
loPage = CreateObject('Chilkat.JsonObject')
loSeenPath = CreateObject('Chilkat.StringBuilder')

lnBatch = 0

lnMore = 1
DO WHILE (lnMore = 1) AND (lnBatch < 20)
    loCurl.ClearTargetOutput("*")
    loAuthorizedCommand1 = CreateObject('Chilkat.StringBuilder')
    loAuthorizedCommand1.SetString('curl -X GET "https://api.box.com/2.0/events?stream_type=all&stream_position={{position}}&limit=100" -H "Authorization: Bearer {{access_token}}"')
    lnHeaderReplacement1 = loAuthorizedCommand1.Replace("{{access_token}}",loCurl.GetVar("access_token"))
    lnSuccess = loCurl.DoYourThing(loAuthorizedCommand1.GetAsString())
    IF ((lnSuccess <> 1) OR (loCurl.StatusCode <> 200)) THEN
        ? "Box request failed. HTTP status: " + STR(loCurl.StatusCode)
        ? "Failure reason: " + STR(loCurl.FailReason)
        ? loCurl.ResponseBodyStr
        RELEASE loCurl
        RELEASE loState
        RELEASE loFiles
        RELEASE loPage
        RELEASE loSeenPath
        RELEASE loAuthorizedCommand1
        CANCEL
    ENDIF

    lnSuccess = loCurl.GetResponseJson(loPage)
    IF (lnSuccess <> 1) THEN
        ? loCurl.LastErrorText
        RELEASE loCurl
        RELEASE loState
        RELEASE loFiles
        RELEASE loPage
        RELEASE loSeenPath
        RELEASE loAuthorizedCommand1
        CANCEL
    ENDIF

    lnCount = loPage.SizeOfArray("entries")
    i = 0
    DO WHILE i < lnCount
        loPage.I = i
        lcEventId = loPage.StringOf("entries[i].event_id")
        IF (lcEventId = "") THEN
            ? "Event without an event_id; checkpoint not advanced."
            RELEASE loCurl
            RELEASE loState
            RELEASE loFiles
            RELEASE loPage
            RELEASE loSeenPath
            RELEASE loAuthorizedCommand1
            CANCEL
        ENDIF

        loSeenPath.Clear()
        loSeenPath.Append("seen.")
        loSeenPath.Append(lcEventId)
        IF (loState.HasMember(loSeenPath.GetAsString()) = 0) THEN
            *  Replace this logging with your idempotent business operation.
            *  Record success only after that operation succeeds.
            ? lcEventId + " " + loPage.StringOf("entries[i].event_type") + " " + loPage.StringOf("entries[i].source.id")
            loState.UpdateBool(loSeenPath.GetAsString(),1)
        ENDIF

        i = i + 1
    ENDDO
    lcNextPosition = loPage.StringOf("next_stream_position")
    IF (lcNextPosition = "") THEN
        ? "Missing next_stream_position; checkpoint not advanced."
        RELEASE loCurl
        RELEASE loState
        RELEASE loFiles
        RELEASE loPage
        RELEASE loSeenPath
        RELEASE loAuthorizedCommand1
        CANCEL
    ENDIF

    lnMore = (lnCount > 0) AND (lcNextPosition <> loCurl.GetVar("position"))
    loState.UpdateString("position",lcNextPosition)
    lnSuccess = loState.WriteFile(lcStatePath)
    IF (lnSuccess <> 1) THEN
        ? loState.LastErrorText
        RELEASE loCurl
        RELEASE loState
        RELEASE loFiles
        RELEASE loPage
        RELEASE loSeenPath
        RELEASE loAuthorizedCommand1
        CANCEL
    ENDIF

    loCurl.SetVar("position",lcNextPosition)
    lnBatch = lnBatch + 1
ENDDO
? "Checkpoint saved. Run again later to continue polling."

RELEASE loCurl
RELEASE loState
RELEASE loFiles
RELEASE loPage
RELEASE loSeenPath
RELEASE loAuthorizedCommand1